mysqli update works but insert doesn't
I have a simple table with 3 columns:
testID is an INT(11) auto-incrementing primary index
name is a VARCHAR text field
position is an INT(11) field
<?php
error_reporting(E_ALL);
require("mysqli.test.inc");
$name1="Rhonda Hotop"; $position1=456;
$name2="Perry Shafran"; $position2 = 789;
$sqlupdate = "UPDATE test SET name = ?, position =?";
$sqlinsert = "INSERT INTO test (name, position) VALUES (name = ?, position = ?)";
//create the connection to the database
$con = new mysqli($hostname,$username,$password,$database);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
//test an UPDATE
$statement = $con->prepare($sqlupdate);
$statement->bind_param("si", $name1, $position1);
$statement->execute();
$statement->close();
//test an INSERT
$statement = $con->prepare($sqlinsert);
$statement->bind_param('si', $name2, $position2);
$statement->execute();
$statement->close();
exit();
The connection works.
The UPDATE works.
However, the INSERT doesn't. It does create a new record with the correct testID field, but the name2 and position2 fields are both set to 0.
Some help would be very much appreciated.
I have a simple table with 3 columns:
testID is an INT(11) auto-incrementing primary index
name is a VARCHAR text field
position is an INT(11) field
<?php
error_reporting(E_ALL);
require("mysqli.test.inc");
$name1="Rhonda Hotop"; $position1=456;
$name2="Perry Shafran"; $position2 = 789;
$sqlupdate = "UPDATE test SET name = ?, position =?";
$sqlinsert = "INSERT INTO test (name, position) VALUES (name = ?, position = ?)";
//create the connection to the database
$con = new mysqli($hostname,$username,$password,$database);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
//test an UPDATE
$statement = $con->prepare($sqlupdate);
$statement->bind_param("si", $name1, $position1);
$statement->execute();
$statement->close();
//test an INSERT
$statement = $con->prepare($sqlinsert);
$statement->bind_param('si', $name2, $position2);
$statement->execute();
$statement->close();
exit();
The connection works.
The UPDATE works.
However, the INSERT doesn't. It does create a new record with the correct testID field, but the name2 and position2 fields are both set to 0.
Some help would be very much appreciated.
No comments:
Post a Comment