13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Putting New Information in the Database<br />

279<br />

Listing 11.4<br />

Continued<br />

$query = "insert into books values<br />

('".$isbn."', '".$author."', '".$title."', '".$price."')";<br />

$result = $db->query($query);<br />

if ($result) {<br />

echo $db->affected_rows." book inserted into database.";<br />

} else {<br />

echo "An error has occurred. The item was not added.";<br />

}<br />

$db->close();<br />

?><br />

<br />

<br />

The results of successfully inserting a book are shown in Figure 11.4.<br />

Figure 11.4<br />

The script completes successfully <strong>and</strong> reports that the book has<br />

been added to the database.<br />

If you look at the code for insert_book.php, you can see that much of it is similar to<br />

the script you wrote to retrieve data from the database.You check that all the form fields<br />

were filled in, <strong>and</strong> you format them correctly for insertion into the database (if required)<br />

with addslashes():<br />

if (!get_magic_quotes_gpc()) {<br />

$isbn = addslashes($isbn);

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!