PHP mysqli 准备好的语句 INSERT 错误

问题描述 投票:0回答:1

我目前正在尝试设置一个准备好的声明,以允许用户注册我的网页。我的 POST 信息从表单正确传递到我的提交页面,如果删除准备好的语句,我可以在提交时成功插入 ?,但当前代码出现错误。

<?php 

if(isset($_POST['submit'])){

$uid = 'NULL';   
$fn = $_POST['fn'];
$ln = $_POST['ln'];
$u = $_POST['u'];
$p = $_POST['p'];
$dob = $_POST['dob'];
$sx = $_POST['sx'];
$pn = $_POST['pn'];
$a = $_POST['a'];
$up = $_POST['CURRENT_TIMESTAMP'];
$c = $_POST['cn'];
$s = $_POST['s'];
$z = $_POST['z'];

require_once('../mysqli_connect.php');

$query = "INSERT INTO u (userid, fn, ln, username, p, dob, sx, pn, em, a, up) 
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

$stmt = mysqli_prepare($mysqli, $query);
if($stmt){
$stmt->bind_param('isssssssssi', $uid, $fn, $ln, $u, $p, $dob, $sx, $pn, $em, $a, $up);

$stmt->execute();

$stmt->close();

}

if (mysqli_query($mysqli, $query)) {
  $userid = mysqli_insert_id($mysqli);
  echo "Your user ID is ". $userid;
} else {
echo "Error: " . $query . "<br>" . mysqli_error($mysqli);
}
// display error if occurs
var_dump($mysqli);

mysqli_close($mysqli);

?>

这是我收到的错误代码:

Error: INSERT INTO u (userid, fn, ln, username, p, dob, sx, pn, em, a, up) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' at line 1

我尝试过更改 php 的版本,我当前运行的是 5.3,但是当我切换到其他版本时,我会收到 mysqli 类的错误。我尝试过重新勾选并引用 ? ,但这似乎也不起作用。我希望有人能够扩展有关准备好的声明中已有的内容,因为我已经进行了多次搜索,但无法找到我的问题根源。所以,我想我的问题是,如何通过准备好的语句正确传递变量,以及我需要在 附近使用什么语法?占位符?

php mysqli prepared-statement sql-insert
1个回答
1
投票

您已经使用了准备好的语句,因此您不需要再使用

mysqli_query()

$query = "INSERT INTO u (userid, fn, ln, username, p, dob, sx, pn, em, a, up) 
    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = mysqli_prepare($mysqli, $query);

$stmt->bind_param('isssssssssi', $uid, $fn, $ln, $u, $p, $dob, $sx, $pn, $em, $a, $up);
$stmt->execute();
$userid = $stmt->insert_id;
echo "Your user ID is ". $userid;
$stmt->close();
© www.soinside.com 2019 - 2024. All rights reserved.