2012-09-24 34 views
0

我一直得到的錯誤是:stripslashes()期望參數1是字符串, 30線上的target.php不斷收到:「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; 
?> 
+0

以及錯誤說,它的數組不是一個字符串。 – 2012-09-24 20:01:21

回答

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

啊是的!非常感謝! –

+0

這是不正確的,請參閱ruakh的答案。 – Barmar

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']); 

(請注意,我也改變10:陣列從0在PHP索引,而不是從1

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]); 

這是假設你通過名字用戶搜索將只返回一個結果

相關問題