Search code examples
phpmysqlmysqliprepared-statement

Issue On Inserting Auto Incremented ID in MySQL Using Prepared Statement


I have table in MySQL database called MyGuests which has 4 fields as : id (PK and Auto Increment), name,age and email. I am using following code to insert data from user input form to the database:

<?php
$sql = mysqli('localhost','user','password','database');
$name  = $_POST['name'];
$age   = $_POST['age'];
$email = $_POST['email'];

$query = $sql->prepare("INSERT INTO MyGuests ( id, name, age, email) VALUES (?, ?, ?, ?)");

$query->bind_param("isis",$name,$age,$email);

$query->execute();
?>

now I am confused how to insert value for id which is auto incremented field using the Prepared statement! As you can see I passed 4 parameters as (?, ?, ?, ?) for data entry and used the "isis" for bind_param(); but not sure what must put in $name,$age,$email for id?

Can you please help me to figure this out?

Thanks


Solution

  • Just omit the id in the query i.e.

    INSERT INTO MyGuests ( name, age, email) VALUES (?, ?, ?)
    

    It will automatically add the incremented id, hence the name :)