php var 值包含引号
所以...我有一个 mysql_fetch_array 并且当某些 mysql 数据包含单引号或双引号时我遇到了问题。这是我的代码的简化版本:
while($row=mysql_fetch_array($list)) {
echo "<tr>";
echo "<td onclick='edit_form(\"" . $row['item'] . "\");'>" . $row['item'];
echo "</td></tr>";
}
edit_form() 函数用于将当前项目的值发送回表单中输入的值,以便用户可以轻松编辑其条目,然后发送 UPDATE 命令到 mysql 以及正确的主键 ID(由于不相关,我省略了)。我遇到的唯一问题是,如果用户将单引号或双引号放入表单中,则会弄乱 onclick 属性。请帮忙!!我对 php 很陌生,无法弄清楚这一点。我已经搞乱了 htmlentites() 和 html_entity_decode() 但仍然一无所获。太感谢了!
So... I have a mysql_fetch_array and I'm running into an issue when some of the mysql data contains single or double quotes. This is the dumbed down version of my code:
while($row=mysql_fetch_array($list)) {
echo "<tr>";
echo "<td onclick='edit_form(\"" . $row['item'] . "\");'>" . $row['item'];
echo "</td></tr>";
}
The edit_form() function is used to send the value of the current item back to the value of the input in the form so the user can then easily edit their entry and then sends an UPDATE command to mysql along with the proper primary key id (which I left out because of irrelevancy). The only issue I have is if a user puts single or double quotes into the form then it messes up the onclick attribute. Please help!! I am pretty new to php and can't figure this out. I've messed around with htmlentites() and html_entity_decode() but am still getting no where. Thank you so much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在
$row['item']
之前使用htmlspecialchars
将其插入您的文档中。所以你的“简化”代码应该是:
Use
htmlspecialchars
on$row['item']
before inserting it in your document.So your "dumbed-down" code should be:
尝试使用下面的行而不是代码中的原始行,看看它是否有效:
并且在您想要显示 $item 字段的位置,如果您得到“e; 而不是 ' " ',只需将其反向替换即可。例如:
如果您在 javascript 中使用它,则该函数可以如下所示:
Try the below line instead of the original in your code and see if it works:
And where you would like to display the $item field, just replace it in reverse if you get "e; instead of ' " '. For example:
If you are using it in javascript, the function can be as below:
我建议使用 json_encode。这样您就不必担心 htmlspecialchars 可能会错过的特殊情况(例如换行符)。
I advise using json_encode. That way you don't have to worry about special cases htmlspecialchars might miss (such as newlines).
我只是将这些值放回到我的
$list
是我在文档顶部运行的mysql_query
$item
是我在 MySQL 中的一个专栏的标题 < /p>现在,当
I'm just putting the values back into my
<form>
(when they click on a<td>
) and when they hit SUBMIT it directs the data to a different .php file that uses a MySQL UPDATE instead of an INSERT. I finally found some code that works!$list
is amysql_query
I ran at the top of the document$item
is the title of one of my columns in MySQLNow it display's correctly in the
<form>
when<td>
is clicked (by jQueryinput.val($item)
) and it shows up in the table correctly via$row['item']
. I don't really understand exactly how it's working (how the encode is making it work) but I am glad it is. Crazyness!!!! Thanks for responding! and thanks for your effort!!