Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote