Below is the code I made to return a book to my mysql books database. When I typ
ID: 3836751 • Letter: B
Question
Below is the code I made to return a book to my mysql books database. When I type in a book that was a checked out, the code works fine and the quantity increments by 1 in my database. But when I type in a book that doesn't exists in my database, my else statement does not execute.
$bookTitle = $_POST['title'];
$sql = mysql_query("SELECT * FROM Books_T WHERE Title = '$bookTitle'") or die("Query failed".mysql_error());
$sql1 = mysql_query("UPDATE Books_T SET Quantity = Quantity + 1 WHERE Title = '$bookTitle'") or die("Query failed".mysql_error());
$good = mysql_fetch_array($sql);
$good1 = mysql_fetch_array($sql1);
if($good['Title'] == $bookTitle){
echo "Book successfuly returned, thank you.";
}
else
{
echo "Book was never checked out.";
header("refresh:80; url=login_homepage.html");
}
?>
Explanation / Answer
Hi,
Code snippet seems to work fine logically.
However try to make below modifications and run then query again.
MODIFICATION :-
$bookTitle = $_POST['title'];
$sql = mysql_query("SELECT * FROM Books_T WHERE Title = '$bookTitle'") or die("Query failed".mysql_error());
$sql1 = mysql_query("UPDATE Books_T SET Quantity = Quantity + 1 WHERE Title = '$bookTitle'") or die("Query failed".mysql_error());
$good = mysql_fetch_array($sql, MYSQL_ASSOC);
$good1 = mysql_fetch_array($sql1, MYSQL_ASSOC);
if($good['Title'] == $bookTitle)
{
echo "Book successfuly returned, thank you.";
}
else
{
echo "Book was never checked out.";
header("refresh:80; url=login_homepage.html");
}
?>
====================================================================================
Please let me know in case it work or not.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.