使用 while 循环从数据库中返回斜杠

发布于 2024-11-03 12:31:10 字数 869 浏览 0 评论 0原文

我可以循环遍历数据库结果并删除斜杠吗?

(每次编辑文本并将其放回数据库时,我开始得到更多斜杠)

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木森分化 2024-11-10 12:31:10

您不能在数组上运行 stripslashes(),这就是 $info 存储 mysql_fetch_array($results) 的内容。您可以迭代 $info 并去掉每个条目上的斜杠,或者如果不需要执行所有这些操作,则可以显式地去掉斜杠。

$i=0;
while($infoarray = mysql_fetch_assoc($results))
    {
        foreach($infoarray as $field=>$value){
            $data[$i][$field] = stripslashes($value);
        }
        $i++;
    }

编辑:您还可以创建一个小函数来去除数组中的斜杠,如此处文档中所述: http://us.php.net/manual/en/function.stripslashes.php

function stripslashes_deep($value)
{
    $value = is_array($value) ?
                array_map('stripslashes_deep', $value) :
                stripslashes($value);

    return $value;
}

// Example
$array = array("f\\'oo", "b\\'ar", array("fo\\'o", "b\\'ar"));
$array = stripslashes_deep($array);

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.

$i=0;
while($infoarray = mysql_fetch_assoc($results))
    {
        foreach($infoarray as $field=>$value){
            $data[$i][$field] = stripslashes($value);
        }
        $i++;
    }

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

function stripslashes_deep($value)
{
    $value = is_array($value) ?
                array_map('stripslashes_deep', $value) :
                stripslashes($value);

    return $value;
}

// Example
$array = array("f\\'oo", "b\\'ar", array("fo\\'o", "b\\'ar"));
$array = stripslashes_deep($array);
执笏见 2024-11-10 12:31:10

不要那样做。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文