I'm working on a table that has 4 columns and the first one is an auto incrementing integer called id.
If I'm going to insert into this table using mysqli prepared statements I keep having trouble inserting a query that works. Using phpMyAdmin It tells me to give it NULL. I've tried this:
$query = "INSERT INTO tbl (id, col2, col3, col4) VALUES ('NULL', ?, ?, ?)";
$stmt -> bind_param("ssi", $col2, $col3, $col4)
And this
$query = "INSERT INTO tbl (id, col2, col3, col4) VALUES (NULL, ?, ?, ?)";
$stmt -> bind_param("ssi", $col2, $col3, $col4)
And only give bind_param 3 arguments (the last 3). Neither of those work. I also tried this:
$null = NULL;
$query = "INSERT INTO tbl (id, col2, col3, col4) VALUES (?, ?, ?, ?)";
$stmt -> bind_param("issi", $null, $col2, $col3, $col4);
None of these work. Is there a standardized way of inserting into this type of table?
Just skip the id
field, MySQL will fill it automatically:
$query = "INSERT INTO tbl (col2, col3, col4) VALUES (?, ?, ?)";
$stmt->bind_param("ssi", $col2, $col3, $col4)
If the id
field is the auto_increment
, then just don't specify it in your insert
query :
$query = "INSERT INTO tbl (col2, col3, col4) VALUES (?, ?, ?)";
And, of course, don't try to bind any parameter to it ;-)
As it's generated by MySQL, there is no need to pass that column.
This should work, because id is added automatically (incremented for this reason) by mysql:
$query = "INSERT INTO tbl (col2, col3, col4) VALUES (?, ?, ?)";
In some cases, you have to insert auto_incremtnt field explicitly, if this is the case then you can use the INSERT IGNORE
statement, see mysql manual for more info about it.
This one
$query = "INSERT INTO tbl (id, col2, col3, col4) VALUES (NULL, ?, ?, ?)";
$stmt -> bind_param("ssi", $col2, $col3, $col4)
should surely work. What is the exact error you get?
Now I look better, you have $query and $stmt. What do you have in between? Probably you are missing some part.
It should be
$stmt = $mysqli->prepare("INSERT INTO tbl (id, col2, col3, col4) VALUES (NULL, ?, ?, ?)");
$stmt -> bind_param("ssi", $col2, $col3, $col4);
$stmt ->execute();
$sql=("SELECT max(id) id FROM tbl_uploads where username='{$_SESSION['username']}'");
$result_set=mysqli_query($conn,$sql) or die ("<b>Error:</b> Problem on Retrieving Image From upload folder.<br/>" . mysqli_error($con));
while($row=mysqli_fetch_array($result_set))
{
$id=$row['id']+1; // used it for increament that is not working
your code
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.