Procedural mysqli insert statement
Posted: Fri Aug 14, 2009 10:28 am
Hi Everyone,
I have a question that I have not been able to look up anywhere. I think I’m missing the proper nomenclature and therefore can’t find this in either a google search or at the PHP.net site. I going through "PHP and MySQL Web Development" by Welling and Thompson (which by the way is an excellent resource) and in chapter 11 they have a script to insert data (as variables) into the database. They are using an OO approach and in this case do not have the procedural code for inserting the variables. Here’s what the code looks like:
The snippet “$query = "insert into books values ('".$isbn."', '".$author."', '".$title."', '".$price."')";” is what I’m wondering about. Does anyone know what the proper syntax is for inserting variables into a MySQL database using a procedural approach? Or can someone provide me with the proper search phrase to google? Thanks much!
Cheers,
Rick
I have a question that I have not been able to look up anywhere. I think I’m missing the proper nomenclature and therefore can’t find this in either a google search or at the PHP.net site. I going through "PHP and MySQL Web Development" by Welling and Thompson (which by the way is an excellent resource) and in chapter 11 they have a script to insert data (as variables) into the database. They are using an OO approach and in this case do not have the procedural code for inserting the variables. Here’s what the code looks like:
Code: Select all
<?php
// create short variable names
$isbn=$_POST['isbn'];
$author=$_POST['author'];
$title=$_POST['title'];
$price=$_POST['price'];
if (!$isbn || !$author || !$title || !$price) {
echo "You have not entered all the required details.<br />"
."Please go back and try again.";
exit;
}
if (!get_magic_quotes_gpc()) {
$isbn = addslashes($isbn);
$author = addslashes($author);
$title = addslashes($title);
$price = doubleval($price);
}
@ $db = new mysqli('localhost', 'bookorama', 'bookorama123', 'books');
if (mysqli_connect_errno()) {
echo "Error: Could not connect to database. Please try again later.";
exit;
}
$query = "insert into books values
('".$isbn."', '".$author."', '".$title."', '".$price."')";
$result = $db->query($query);
if ($result) {
echo $db->affected_rows." book inserted into database.";
} else {
echo "An error has occurred. The item was not added.";
}
$db->close();
?>Cheers,
Rick