继续得到:“stripslashes()期望参数1是字符串,在第30行的/target.php中给出的数组”错误

时间:2012-09-24 19:57:29

标签: php mysql pdo

我一直得到的错误是:stripslashes()期望参数1是字符串,第30行的/target.php中给出的数组

这是我对本网站的代码,我是新手,我已经尝试超过30分钟了。

我很抱歉提出这么愚蠢的问题。

<?php

// rnprofile.php
include_once 'rnheader.php';

if (!isset($_SESSION['user']))
    die("<br /><br />You need to login to view this page");
$user = $_SESSION['user'];
echo "<h3>Edit your Profile</h3>";
if (isset($_POST['text'])) {
    $text = sanitizeString($_POST['text']);
    $text = preg_replace('/\s\s+/', ' ', $text);
    $query = "SELECT * FROM rnprofiles WHERE user='$user'";
    if (queryMysql($query) != false) {
        queryMysql("UPDATE rnprofiles SET text='$text'
            where user='$user'");
    } else {
        $query = "INSERT INTO rnprofiles VALUES('$user', '$text')";
        queryMysql($query);
    }
} else {
    $query = "SELECT * FROM rnprofiles WHERE user='$user'";

    $result = queryMysql($query);
    $sth = $dbh->prepare($query);
    $sth->execute();
    if (queryMysql($query) != false/* mysql_num_rows($result) */) {
        $row = $sth->fetchAll(PDO::FETCH_ASSOC);
        //$row = mysql_fetch_row($result);
        $text = stripslashes($row[1]);
    }
    else
        $text = "";
}
$text = stripslashes(preg_replace('/\s\s+/', ' ', $text));
if (isset($_FILES['image']['name'])) {
    $saveto = "$user.jpg";
    move_uploaded_file($_FILES['image']['tmp_name'], $saveto);
    $typeok = TRUE;
    switch ($_FILES['image']['type']) {
        case "image/gif": $src = imagecreatefromgif($saveto);
            break;
        case "image/jpeg": // Both regular and progressive jpegs
        case "image/pjpeg": $src = imagecreatefromjpeg($saveto);
            break;
        case "image/png": $src = imagecreatefrompng($saveto);
            break;
        default: $typeok = FALSE;
            break;
    }
    if ($typeok) {
        list($w, $h) = getimagesize($saveto);
        $max = 100;
        $tw = $w;
        $th = $h;
        if ($w > $h && $max < $w) {
        $th = $max / $w * $h;
        $tw = $max;
    } elseif ($h > $w && $max < $h) {
        $tw = $max / $h * $w;
        $th = $max;
    } elseif ($max < $w) {
        $tw = $th = $max;
    }
    $tmp = imagecreatetruecolor($tw, $th);
    imagecopyresampled($tmp, $src, 0, 0, 0, 0, $tw, $th, $w, $h);
    imageconvolution($tmp, array(// Sharpen image
        array(−1, −1, −1),
        array(−1, 16, −1),
        array(−1, −1, −1)
            ), 8, 0);
    imagejpeg($tmp, $saveto);
    imagedestroy($tmp);
    imagedestroy($src);
    }
}
showProfile($user);
echo <<<_END
<form method='post' action='rnprofile.php'
enctype='multipart/form-data'>
Enter or edit your details and/or upload an image:<br />
<textarea name='text' cols='40' rows='3'>$text</textarea><br />
Image: <input type='file' name='image' size='14' maxlength='32' />
<input type='submit' value='Save Profile' />
</pre></form>
_END;
?>

3 个答案:

答案 0 :(得分:2)

来自PDO::FETCH手册

  

PDO :: FETCH_ASSOC :返回由返回的列名索引的数组   在结果集中

     

PDO :: FETCH_NUM :返回按返回的列号索引的数组   在结果集中,从第0列开始

您已使用FETCH_ASSOC,那么您必须使用列名作为$row

的键
    $row = $sth->fetchAll(PDO::FETCH_ASSOC);
    $text = stripslashes($row['column_name']);

或将其更改为FETCH::NUM

 $row = $sth->fetchAll(PDO::FETCH_NUM);
 $text = stripslashes($row[1);

答案 1 :(得分:1)

fetchAll方法返回一个包含所有行的数组,所以这一行:

$row = $sth->fetchAll(PDO::FETCH_ASSOC);

未按名称$row暗示的方式设置row。因此,$row[1]不是单个字符串,而是包含整行的关联数组。

你应该改变这个:

        $row = $sth->fetchAll(PDO::FETCH_ASSOC);
        //$row = mysql_fetch_row($result);
        $text = stripslashes($row[1]);

到此:

        $rows = $sth->fetchAll(PDO::FETCH_ASSOC);
        $text = stripslashes($rows[0]['column_name_of_interest']);

(请注意,我还将1更改为0:数组在PHP中从0编制索引,而不是从1编制索引。)

答案 2 :(得分:0)

这是因为你正在使用fetchAll,它返回一组关联数组:

if (queryMysql($query) != false/* mysql_num_rows($result) */) {
    $row = $sth->fetchAll(PDO::FETCH_ASSOC);
    $text = stripslashes($row[1]);

因此,当您调用stripslashes并传递它$row[1]时,您将传递一个关联数组,该数组由数据库的第二个结果组成。我想这可能就是你的意思:

if (queryMysql($query) != false/* mysql_num_rows($result) */) {
    if ($row = $sth->fetch(PDO::FETCH_ASSOC) {
        $text = stripslashes($row[1]);

假设您按名称搜索用户只会返回一个结果