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 编写vba代码实现数据录入工作
  • ¥15 做过TCL海信电视小米电视相关影视会员软件私我
  • ¥15 Mapreduce是正常的,在运行其他jar包时并没有任何问题,只是在做LogCount.jar 时出的问题。如图所示
  • ¥15 ImportError: DLL load failed while importing _iterative: 找不到指定的模块。
  • ¥15 如何通过交互分析得出某高危患者对放疗获益更多
  • ¥15 相关性分析中,p<0.05, r=0.29,怎么评价相关性呢
  • ¥15 docker部署Mongodb后输入命令报错?
  • ¥15 将下列流程图转变成python程序代码
  • ¥15 我需要全国每个城市的最新小区名字等数据。
  • ¥15 开发一个小区生态的小程序