drpmazn9021 2014-03-18 01:44
浏览 28
已采纳

PHP插入命令不起作用

Hi guys can you please tell me if there is an error on this code. this is not working. it didn't add any on my database. thanks you!

$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>
  • 写回答

2条回答 默认 最新

  • doutiaosu2310 2014-03-18 01:51
    关注

    The column name(s) should be wrapped in backticks and not quotes

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

    or remove the quotes

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

    I suggest you move to mysqli_* functions with prepared statements or PDO.

    and that you change $acc_class = $_POST['acc_class']; to

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

    for the time being.

    mysql_* functions are deprecated and will be removed from future PHP releases.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程