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

标签 php html mysql

我目前正在测试一个 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/

相关文章:

javascript - 修改/更新 HTML 中的元素样式

html - 如何使用 CSS3 限制输入文本的长度?

mysql数据库规范化问题

php - 删除按钮未返回删除查询的正确值

php - 如何在不清除网站其他部分的情况下清除 session

php - 这段 Ajax/Javascript 代码有什么问题?

php - 处理文件请求和可用性

php - 在多个服务器上扩展 cronjobs

html - 垂直对齐内联、内联 block 和 block 元素

mysql - 尝试连接本地 mysql 数据库时,docker 容器上的 Django 出现错误