0

I have a class which is maintain guest information. So in that class I have following method to insert guest data into a guest_info table

require_once "BaseModel.php";

class GuestModel extends BaseModel
{
    public static $table = "guest_info";

    public static function guestInfoFormData(){
         $title = $_POST['title'];
         $firstname = $_POST['firstname'];
         $lastname = $_POST['lastname'];
         $nicpassport = $_POST['nicpassport'];
         $contactnumber = $_POST['contactnumber'];
         $addressline1 = $_POST['addressline1'];
         $addressline2 = $_POST['addressline2'];
         $addressline3 = $_POST['addressline3'];
         $country = $_POST['country'];
         $guestinfo = array($title,$firstname,$firstname,$nicpassport,$addressline1,$addressline2,$addressline3,$country);
         insertInto($guestinfo);
    }
}

How can I call this method from my HTML form and pass the guest data to it?

2 Answers 2

1
<?php GuestModel::guestInfoFormData(); ?>

ensure the GuestModel class is required / included before that.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use GuestModel::guestInfoFormData(), but you can't do it in a html file, it has to be a php file.

Don't forget to escape all input values, now you pass all user input to insertInto, so you have to escape the data before using them in a sql query to prevent sql injections.

Btw: Why you create a class, when you call a global function from it? Maybe it's a good idea to move the global function insertInto into this (or another, better) class :)

3 Comments

I put insertInto method inside my BaseModel class, so i can use it for my other data insertions, Is it a bad idea?
No, that sounds like a reasonable model, but then you can't use it as a global function, you need to call it like: self::inertInto(), and it has to be a static method, too. If it is a non-static method, you'll get a php strict standards error, but it will not interrupt the script. Is there a reason why you use this function statically? Maybe you want to create a new instance of the class and use the function in a context? :)
Yes insertInto is a static method.I missed the class name thanks for remind it. and I put GuestModel::guestInfoFormData() but now i cannot access $_POST['title']; inside the method :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.