gpt4 book ai didi

PHP SQL语法错误设置变量

转载 作者:行者123 更新时间:2023-11-30 22:40:39 26 4
gpt4 key购买 nike

我一直在尝试解决我没有经验的 php 问题。我正在尝试执行一个非常简单的 mysql 命令。该命令在 mysql workbench 上运行良好,但由于某种原因,如果我声明一个名为 ID 的变量,它会在 php 中出错。

我测试了 sql 查询并且它有效..

我们将不胜感激

这是PHP代码:

$Date=date("Y/m/d");
$KonuBaslik=$_POST["baslik"];
$KonuCoverURL=$_POST["kapak"];
$icerik=htmlentities($_POST["konu_icerik"]);
$KonuIcerik=htmlentities($icerik);

$query="use blogdb;"."Start Transaction;".
"begin;"."set @ID=(select ifNull(MAX(konuID),0)+1 from konu);".
"INSERT INTO konu VALUES (?,?,?,'".$Date."',@ID);".
"commit;";
$mysqli = new mysqli($servername,$username, $password, $dbname);

if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

$stmt = $mysqli->prepare($query);
echo $mysqli->error;
$stmt->bind_param('sss', $KonuBaslik,$KonuIcerik,$KonuCoverURL);

$stmt->execute();

感谢您的帮助!

最佳答案

为表 konu 添加列名

试试这个..

$Date=date("Y/m/d");
$KonuBaslik=$_POST["baslik"];
$KonuCoverURL=$_POST["kapak"];
$icerik=htmlentities($_POST["konu_icerik"]);
$KonuIcerik=htmlentities($icerik);
$mysqli = new mysqli($servername,$username, $password, $dbname);
$konuID = $mysqli->query("select MAX(konuID) from konu")->fetch_array()[0] + 1;

$query = "INSERT INTO konu(`column names`,``,``,``,``)VALUES (?,?,?,?,?)";

if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

$stmt = $mysqli->prepare($query);
echo $mysqli->error;
$stmt->bind_param('sssss',$KonuBaslik,$KonuIcerik,$KonuCoverURL,$Date,$konuID);

$stmt->execute();

关于PHP SQL语法错误设置变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31212106/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com