php - 继续获取: "stripslashes() expects parameter 1 to be string, array given in/target.php on line 30" error

标签 php mysql pdo

我不断收到的错误是:stripslashes() 期望参数 1 为字符串,在/target.php 第 30 行给出的数组

这是我为此网站编写的代码,我对此很陌生,并且我已经尝试了 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;
?>

最佳答案

来自 PDO::FETCH手册

PDO::FETCH_ASSOC: returns an array indexed by column name as returned in your result set

PDO::FETCH_NUM: returns an array indexed by column number as returned in your result set, starting at column 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);

关于php - 继续获取: "stripslashes() expects parameter 1 to be string, array given in/target.php on line 30" error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12571998/

相关文章:

php - 为什么这个 PDO 语句会默默地失败?

php - 更新脚本不起作用?

php - 保留主页重定向,但将 http 更改为 https 全站(包括子域)

php - 启用 PDO 后 - 错误 2002 getaddrinfo 失败 : Name or service not known

php - PDO mysql查询问题

php - 在数据库中插入下拉菜单项

php - 如何在 PHP 中获得有用的错误消息?

php - 如何在opencart中创建全局函数

mysql - 如何将Excel文件导入MySQL数据库

php - Laravel 中的 "View not defined"错误