dongnius85154 2016-10-18 21:48
浏览 24
已采纳

使用PHP为SQL数据库准备HTML表单输入

I'm currently testing a html form which sends the data through php to the sql database. The problem I'm facing is special characters break the form and don't update the database. I haven't tested all the special characters but mainly ` and ' are the culprits. I've tried mysql_escape_string, preg_replace and add_slashes with no success. What am I missing?

$description = preg_replace('/[^A-Za-z0-9\ %&$=+*?()!.-]/', ' ', $_POST['description']);
$description = preg_replace("/[
]/"," ",$description);

// Items Insert
foreach ($item as $index=>$value) {
$sqlItems .= "
    INSERT INTO quote_items (quote_id, item, description, amount, gst)
    VALUES ('$last_id', '$item[$index]', '$description[$index]', '$amount[$index]', '$gst[$index]');
";
}

Thanks in advance!

  • 写回答

2条回答 默认 最新

  • duanbairan4235 2016-10-18 22:46
    关注

    you can try this (a little dirty) but it should allow those 2 characters to be saved

    $sqlItems .= '
        INSERT INTO `quote_items` (quote_id, item, description, amount, gst)
        VALUES ("'.$last_id.'", "'.$item[$index].'", "'.$description[$index].'", "'.$amount[$index].'", "'.$gst[$index].'");
    ';
    

    EDIT: sorry had the quotes reversed

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?