我目前正在测试一个 html 表单,它通过 php 将数据发送到 sql 数据库。我面临的问题是特殊字符破坏了表单并且不更新数据库。我没有测试所有特殊字符,但主要是 `
和'
都是罪魁祸首。我尝试过 mysql_escape_string、preg_replace 和 add_slashes 但没有成功。我错过了什么?
$description = preg_replace('/[^A-Za-z0-9\ %&$=+*?()!.-]/', ' ', $_POST['description']);
$description = preg_replace("/[\n\r]/"," ",$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]');
";
}
提前致谢!
最佳答案
你可以尝试这个(有点脏),但它应该允许保存这两个字符
$sqlItems .= '
INSERT INTO `quote_items` (quote_id, item, description, amount, gst)
VALUES ("'.$last_id.'", "'.$item[$index].'", "'.$description[$index].'", "'.$amount[$index].'", "'.$gst[$index].'");
';
编辑:抱歉,引号颠倒了
关于php - 使用 PHP 为 SQL 数据库准备 HTML 表单输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40118678/