我不断得到的错误是: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('/ss+/', ' ', $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('/ss+/', ' ', $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:返回按列名编制索引的数组 在结果集中
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);
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
索引的。
这是因为您使用的是 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]);
这是假设您按名称搜索用户只会返回一个结果