MySQL 插入双引号 PHP
MySQL Insert Double Quotes PHP
我正在使用下面准备的语句插入 MySQL。如果我尝试插入包含 "(双引号)的 $descrip
内容,插入将在此时停止。
示例:
尝试插入 16" Solid Steel Tube
table 行的条目仅显示 16
并在 "(双引号)处停止并且不会显示 $descrip
.
的其余部分
我做错了什么?
$stmt = $db->prepare("INSERT INTO line_items (quote_id, part, descrip, qty, price, net, notes, datasheet) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("ssssssss", $quote_id, $part, $descrip, $qty, $price, $net, $notes, $datasheet);
foreach($_POST['part'] as $i => $item) {
$part = $item;
$descrip = $_POST['descrip'][$i]; //This wont enter something with double qutoes such as 16" Solid Steel Tube
$qty = $_POST['qty'][$i];
$price = $_POST['price'][$i];
$net = $_POST['net'][$i];
$notes = $_POST['notes'][$i];
$datasheet = $_POST['datasheet'][$i];
$stmt->execute();
}
编辑:
仅供参考-我从数据库中的另一个 table 中选择 $descrip
,它在行中正确地将其作为 16" Solid Steel Tube
。当我尝试通过我准备好的语句将此项目复制到另一个 table 时,它无法正确插入。
根据您的提示...
I am selecting $descrip
from another table in the database which correctly has this in the row as '16" Solid Steel Tube'. When i try and copy this item into another table via my prepared statement that is when it wont insert properly.
我想你正在以这样的形式使用这个值(但可能不完全是)
<input type="text" value="<?= $descrip ?>" name="descrip[]">
这里的问题是呈现的 HTML 看起来像
<input type="text" value="16" Solid Steel Tube" name="descrip[]">
请注意 value
属性现在在 "16"?
之后有一个结束引号
您需要对您的值进行编码以便在 HTML 中安全使用。尝试...
<input type="text" value="<?= htmlspecialchars($descrip) ?>" name="descrip[]">
这将呈现为
<input type="text" value="16" Solid Steel Tube" name="descrip[]">
您的 PHP 脚本在提交时仍会收到它作为未编码的字符串。
我正在使用下面准备的语句插入 MySQL。如果我尝试插入包含 "(双引号)的 $descrip
内容,插入将在此时停止。
示例:
尝试插入 16" Solid Steel Tube
table 行的条目仅显示 16
并在 "(双引号)处停止并且不会显示 $descrip
.
我做错了什么?
$stmt = $db->prepare("INSERT INTO line_items (quote_id, part, descrip, qty, price, net, notes, datasheet) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("ssssssss", $quote_id, $part, $descrip, $qty, $price, $net, $notes, $datasheet);
foreach($_POST['part'] as $i => $item) {
$part = $item;
$descrip = $_POST['descrip'][$i]; //This wont enter something with double qutoes such as 16" Solid Steel Tube
$qty = $_POST['qty'][$i];
$price = $_POST['price'][$i];
$net = $_POST['net'][$i];
$notes = $_POST['notes'][$i];
$datasheet = $_POST['datasheet'][$i];
$stmt->execute();
}
编辑:
仅供参考-我从数据库中的另一个 table 中选择 $descrip
,它在行中正确地将其作为 16" Solid Steel Tube
。当我尝试通过我准备好的语句将此项目复制到另一个 table 时,它无法正确插入。
根据您的提示...
I am selecting
$descrip
from another table in the database which correctly has this in the row as '16" Solid Steel Tube'. When i try and copy this item into another table via my prepared statement that is when it wont insert properly.
我想你正在以这样的形式使用这个值(但可能不完全是)
<input type="text" value="<?= $descrip ?>" name="descrip[]">
这里的问题是呈现的 HTML 看起来像
<input type="text" value="16" Solid Steel Tube" name="descrip[]">
请注意 value
属性现在在 "16"?
您需要对您的值进行编码以便在 HTML 中安全使用。尝试...
<input type="text" value="<?= htmlspecialchars($descrip) ?>" name="descrip[]">
这将呈现为
<input type="text" value="16" Solid Steel Tube" name="descrip[]">
您的 PHP 脚本在提交时仍会收到它作为未编码的字符串。