使用 while 循环从数据库中返回斜杠
我可以循环遍历数据库结果并删除斜杠吗?
(每次编辑文本并将其放回数据库时,我开始得到更多斜杠)
$db = new connection();
$query = "SELECT * FROM ITEMS, ALPACA_SALES WHERE ITEMS.uid = '$id' AND ALPACA_SALES.uid = ITEMS.uid";
$results = $db->query($query);
$data = array();
while($info = mysql_fetch_array($results))
{
$data[] = stripslashes($info);
}
但是我收到以下错误:
Notice: Array to string conversion in /home/content/myfile.php on line 78
当我将数据添加到数据库时,我执行以下操作:
if (!empty($_POST['more_text']))
{
$more_text = addslashes($_POST['more_text']);
}
else
{
$error = false;
$error_message = "Please add a description.";
}
然后我使用 UPDATE 插入到数据库:
$query2 = "UPDATE ALPACA_SALES SET more_text = '$more_text' WHERE uid = '$id'";
$result = $db->query($query2);
Can I loop through DB results and remove Slashes?
(I started getting more slashes everytime I edited the text and put it back into the DB)
$db = new connection();
$query = "SELECT * FROM ITEMS, ALPACA_SALES WHERE ITEMS.uid = '$id' AND ALPACA_SALES.uid = ITEMS.uid";
$results = $db->query($query);
$data = array();
while($info = mysql_fetch_array($results))
{
$data[] = stripslashes($info);
}
But I am getting the following error:
Notice: Array to string conversion in /home/content/myfile.php on line 78
When I add the data to the database I do the following:
if (!empty($_POST['more_text']))
{
$more_text = addslashes($_POST['more_text']);
}
else
{
$error = false;
$error_message = "Please add a description.";
}
And then I use UPDATE for the insert into the DB:
$query2 = "UPDATE ALPACA_SALES SET more_text = '$more_text' WHERE uid = '$id'";
$result = $db->query($query2);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能在数组上运行 stripslashes(),这就是 $info 存储 mysql_fetch_array($results) 的内容。您可以迭代 $info 并去掉每个条目上的斜杠,或者如果不需要执行所有这些操作,则可以显式地去掉斜杠。
编辑:您还可以创建一个小函数来去除数组中的斜杠,如此处文档中所述: http://us.php.net/manual/en/function.stripslashes.php
You can't run stripslashes() on an array, which is what $info is from storing mysql_fetch_array($results). You could iterate through $info and strip slashes on each entry, or explicitly strip slashes if you don't need to do all of them.
Edit: You can also create a small function to strip slashes in an array, as outlined in the documentation here: http://us.php.net/manual/en/function.stripslashes.php
不要那样做。 stripslashes() 不足以使字符串在 SQL 语句中安全使用。您的代码容易受到 SQL 注入攻击。请改用准备好的语句和绑定参数。
Do not do that. stripslashes() is not enough to make a string safe to use in an SQL statement. Your code is vulnerable to SQL injection. Use prepared statements and bound parameters instead.