我有一个包含3列的简单表格:
testID是INT(11)自动递增主索引
name是VARCHAR文本字段
position是INT(11)字段
<?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();
连接有效。
UPDATE有效。
然而,INSERT没有。它确实使用正确的testID字段创建了一条新记录,但name2和position2字段都设置为0。
非常感谢一些帮助。
答案 0 :(得分:1)
更改
$sqlinsert = "INSERT INTO test (name, position) VALUES (name = ?, position = ?)";
到
$sqlinsert = "INSERT INTO test (name, position) VALUES (?, ?)";
答案 1 :(得分:0)
修改此行
$sqlinsert = "INSERT INTO test (name, position) VALUES (?, ?)";