2012-03-02 69 views
0

我正在構建一個類似Facebook的應用程序。我希望數據庫中的更新表能夠在用戶更改書籍狀態(讀取,閱讀或想要閱讀)時更新。添加到數據庫時返回空白的變量

雖然下面的代碼成功添加一行到數據庫中,這本書的標題是空白的,即。

這裏的更新查詢:

$Wall->Insert_Update($userID,'would like to read <a href="book.php?id='.$bookID.'">'.$title.'</a>.'); 

這裏就是被添加到數據庫中:

wants to read <a href="book.php?id=13"></a> 

正如你所看到的,沒有任何一個頭銜。

下面我已經包含了更新頁面的完整代碼。有問題的頁面通過另一個頁面通過ajax調用。

正如你看到的,我有應該得到基於在架子上的書的標題和BOOKID提供的select語句。

<?php 
//includes 
$Wall = new Wall_Updates(); 

$bookID = $_POST['bookID']; 
$shelfID = $_POST['shelfID']; 
$userID = $_SESSION['user_id']; 

$query = mysql_query("SELECT * FROM shelves WHERE userID = '$userID' AND bookID = '$bookID'"); 

if (mysql_num_rows($query) == 0) { 
$insert = "INSERT INTO shelves (bookID,shelfID,userID) VALUES ('$bookID','$shelfID','$userID')"; 
mysql_query($insert) or die(mysql_error()); 

//insert update 
$select = "SELECT title, shelfID, books.bookID FROM books, shelves WHERE books.bookID = shelves.bookID 
    AND shelves.bookID = '$bookID' AND userID = '$userID'"; 
mysql_query($select) or die(mysql_error()); 

$row = mysql_fetch_array($select); 
$title = $row['title']; 

if($shelfID == 1){ 
    $Wall->Insert_Update($userID,'read <a href="book.php?id='.$bookID.'">'.$title.'</a>.'); 
}elseif($shelfID == 2){ 
    $Wall->Insert_Update($userID,'wants to read <a href="book.php?id='.$bookID.'">'.$title.'</a>.'); 
}else{ 
    $Wall->Insert_Update($userID,'is currently reading <a href="book.php?id='.$bookID.'">inserting</a>.'); 
} 


} elseif (mysql_num_rows($query) == 1) { //ie row already exists 
$update = "UPDATE shelves SET shelfID = '$shelfID' WHERE userID = '$userID' AND bookID = '$bookID'"; 
mysql_query($update) or die(mysql_error()); 

//insert update 
$select = "SELECT title, shelfID, books.bookID FROM books, shelves WHERE books.bookID = shelves.bookID 
    AND shelfID = '$shelfID'"; 
mysql_query($select) or die(mysql_error()); 

$row = mysql_fetch_array($select); 
$title = $row['title']; 
$shelfID = $row['shelfID']; 

if($shelfID == 1){ 
    $Wall->Insert_Update($userID,'read <a href="book.php?id='.$bookID.'">'.$title.'</a>.'); 
}elseif($shelfID == 2){ 
    $Wall->Insert_Update($userID,'wants to read <a href="book.php?id='.$bookID.'">'.$title.'</a>.'); 
}else{ 
    $Wall->Insert_Update($userID,'is currently reading <a href="book.php?id='.$bookID.'">'.$title.'</a>.'); 
} 
} 

?> 

回答

1

請注意,您提取查詢而不是mysql_query資源。

更改代碼:

$select = "SELECT title, shelfID, books.bookID FROM books, shelves WHERE books.bookID = shelves.bookID 
    AND shelves.bookID = '$bookID' AND userID = '$userID'"; 
mysql_query($select) or die(mysql_error()); 

$row = mysql_fetch_array($select); 
$title = $row['title']; 

要:

$select = "SELECT title, shelfID, books.bookID FROM books, shelves WHERE books.bookID = shelves.bookID 
    AND shelves.bookID = '$bookID' AND userID = '$userID'"; 
$getData = mysql_query($select) or die(mysql_error()); 

$row = mysql_fetch_array($getData); 
$title = $row['title']; 

現在應該工作。

+0

啊,不能相信我錯過了。謝謝。 – 2012-03-03 10:32:15