gpt4 book ai didi

PHP 插入命令不起作用

转载 作者:行者123 更新时间:2023-11-29 13:07:51 25 4
gpt4 key购买 nike

大家好,请告诉我这段代码是否有错误。这是行不通的。它没有在我的数据库中添加任何内容。感谢您!

$con = mysql_connect("localhost","root","pass");
if (!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db("mytable",$con);



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

// Variables

$acc_class = $_POST['acc_class'];

$AddQuery = "INSERT INTO mytable ('acc_class') VALUES ('$acc_class')";

mysql_query($AddQuery, $con);

echo "Record Successfully Added!!";
};


mysql_close($con);

?>
<form action="add.php" method="post">

Account Classification:
<input required="required" placeholder="e.g Hotel, Restaurant" type="text" name='acc_class' size=15 />

<input type="submit" name='add' Value='&nbsp;Add Record&nbsp;'/>
</form>

最佳答案

列名应该用反引号而不是引号括起来

$AddQuery = "INSERT INTO mytable (`acc_class`) VALUES ('$acc_class')";

或删除引号

$AddQuery = "INSERT INTO mytable (acc_class) VALUES ('$acc_class')";

我建议您转到mysqli_*功能与 prepared statementsPDO .

并且您将 $acc_class = $_POST['acc_class']; 更改为

$acc_class = mysql_real_escape_string($_POST['acc_class']);

暂时。

mysql_* 函数已弃用,并将从 future 的 PHP 版本中删除。

关于PHP 插入命令不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22468899/

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