我正在建立一个类似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获取书籍的标题。
<?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>.');
}
}
?>
答案 0 :(得分: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'];
现在应该工作。