skip to Main Content

I try to do the booking form and in the PHP I type the $stmt->bind_param part come out syntax error. However, I don’t know where I did wrong. Here is my phpMyAdmin setting phpmyadmin table structure:
phpmyadmin table structure. Below is my code:

$conn = mysqli_connect($servername, $username, $password,$database);

// Check connection

if($conn->connect_error){
        
        die("Connection Failed : ". $conn->connect_error);
    } else {
        $stmt = $conn->prepare("insert into event_and_inquiry_form (Name,Mail,Phone_Number,Date,Propose,Person,Theme,Event_Package,Remarks)VALUES (Name, Mail, Phone_Number,Date,Propose,Person,Theme,Event_Package,Remarks);
        
        $stmt->bind_param("sssisisss", $Name,$Mail,$Phone_Number,$Date,$Propose,$Person,$Theme,$Event_Package,$Remarks);
        $execval = $stmt->execute();
        echo $execval;
        $stmt
        $stmt->close();
        $conn->close();
    }
    

2

Answers


  1. You do happen to have a few issues.

    1. When you prepare your mysqli statement the values to be inserted are to be held by a question mark ?. I believe you can hold them with :name :secondname as well but that’s a story for another question.
    2. You have not closed your quotes or bracket on the prepare function.
    3. You have a random $stmt variable at the end of your script.

    I corrected your code with what I noticed and posted it below:

    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    $conn = mysqli_connect($servername, $username, $password,$database);
    
    $stmt = $conn->prepare("INSERT INTO `event_and_inquiry_form` 
    (`Name`,`Mail`,`Phone_Number`,`Date`,`Propose`,`Person`,`Theme`,`Event_Package`,`Remarks`)
    VALUES
    ( ? , ? , ? , ? , ? , ? , ? , ? , ?)");
    
    $stmt->bind_param("sssisisss", $Name,$Mail,$Phone_Number,$Date,$Propose,$Person,$Theme,$Event_Package,$Remarks);
    $execval = $stmt->execute();
    $stmt->close();
    $conn->close();
    
    Login or Signup to reply.
  2. It seems you’re getting confused between mysqli and PDO – although there are syntax issues either way!

    mysqli

    With mysqli the short answer is that you need to replace all of the variables in VALUES( ... ) with ?.

    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);           // Enable error reporting
    $mysqli = new mysqli($servername, $username, $password, $database);  // Create connection to database
    
    $sql = "
        INSERT INTO event_and_inquiry_form 
            (Name,Mail,Phone_Number,Date,Propose,Person,Theme,Event_Package,Remarks)
        VALUES (?,?,?,?,?,?,?,?,?)
    ";
    
    $query = $mysqli->prepare($sql);
    $query->bind_param("sssisisss", $Name, $Mail, $Phone_Number, $Date, $Propose, $Person, $Theme, $Event_Package, $Remarks);
    $query->execute();
    

    PDO

    You can do it the same way in PDO.

    Of course the connection method is different and we’ll pass an array of the values to the execute function instead.

    // Connect to the database
    $pdo = new pdo(
        "mysql:host={$servername};dbname={$database}",
        $username,
        $password,
        [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_EMULATE_PREPARES => FALSE
        ]
    );
    
    $sql = "
        INSERT INTO event_and_inquiry_form 
            (Name,Mail,Phone_Number,Date,Propose,Person,Theme,Event_Package,Remarks)
        VALUES (?,?,?,?,?,?,?,?,?)";
    
    $query = $pdo->prepare($sql);
    $query->execute([$Name, $Mail, $Phone_Number, $Date, $Propose, $Person, $Theme, $Event_Package, $Remarks]);
    

    Finally, in some cases you may prefer to name your placeholders. In this case the name will be :some_name and the array will need to be associative ["some_name"=> "Some value"].

    // Connect to the database
    $pdo = new pdo(
        "mysql:host={$servername};dbname={$database}",
        $username,
        $password,
        [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_EMULATE_PREPARES => FALSE
        ]
    );
    
    $sql = "
        INSERT INTO event_and_inquiry_form 
            (Name,Mail,Phone_Number,Date,Propose,Person,Theme,Event_Package,Remarks)
        VALUES
            (:name, :mail, :phone_number, :date, :propose, :person, :theme, :event_package, :remarks)";
    
    $query = $pdo->prepare($sql);
    $query->execute([
        "name"          => $Name
        "mail"          => $Mail
        "phone_number"  => $Phone_Number
        "date"          => $Date
        "propose"       => $Propose
        "person"        => $Person
        "theme"         => $Theme
        "event_package" => $Event_Package
        "remarks"       => $Remarks
    ]);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search