The db name is test, the table name is books, the fields in the table are id, title, and author.
using this code below adds blank info to the db. the variables dont seem to be passing correctly.
//check if the submit variable exists
if ($_POST['title']&&$_POST['author']) {
$db->query("INSERT INTO books (title,author) VALUES('$title','$author')");
echo "<p><img src='images/36.gif'> It worked, we got". $_POST['title']." and ".$_POST['author']. ",Yay!</p>";
}else{
echo "<p><img src='images/40.gif'> it didnt work!</p>";
}
// get multiple rows
$users = $db->get_results("SELECT title,author FROM books");
foreach($users as $user){
// Access data using object syntax
echo "<b>";
echo $user->title;
echo "</b>";
echo " ";
echo $user->author;
echo "<br />";
}
?>







4 Comments
// Only process this form if it is a submit
if ( isset($_POST['title']) && isset($_POST['author']) )
{
// Ensure that variabls have a value
if ( ! $_POST['title'] )
{
echo "Please enter title.";
}
else if ( ! $_POST['author'] )
{
echo "Please enter author.";
}
// Evaluate at the same time as inserting to save code
// (Escape the incoming data to avoid sql injection attack and deal with 's)
else if ( ! $num_affected_rows = $db->query("INSERT INTO books (title,author) VALUES('".$db->escape($_POST['title'])."','".$db->escape($_POST['author'])."')") )
{
echo "Error inserting into the database";
}
else
{
echo "num_affected_rows: $num_affected_rows
";
echo "insert_id: {$db->insert_id}
";
}
}
if ( $results = $db->get_results("SELECT * FROM books") )
{
foreach($results as $result)
{
// Access data using object syntax
echo "<b>";
echo $result->title;
echo "</b>";
echo " ";
echo $result->author;
echo "<br />";
}
}
else
{
echo "There are no books just yet!";
}
// Evaluate at the same time as inserting to save code
// (Escape the incoming data to avoid sql injection attack and deal with 's)
else if ( ! $num_affected_rows = $db->query("INSERT INTO books (title,author) VALUES('".$db->escape($_POST['title'])."','".$db->escape($_POST['author'])."')") )
{
echo "Error inserting into the database";
}
else
{
echo "num_affected_rows: $num_affected_rows";
echo "insert_id: {$db->insert_id}";
}
thankyou.
$db->escape($_POST['author'])
// Get the number of changed rows
if ( ! $num_affected_rows = $db->query( etc...
// Evaluate the results of the query
if ( ! $num_affected_rows = $db->query( etc...
// If $num_affected_rows = 0 then it will be false
// (if any rows changed it will give the number of changes and be true)
Hey you know AdGuy always gets the last word! ;)