2012-03-02 27 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に基づいて本のタイトルが得られるはずです。

<?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

ああ、私はそれを逃したとは思わない。ありがとうございました。 –

関連する問題