mcclellanfsu Posted February 21, 2007 Share Posted February 21, 2007 Hello, I will tell you all right off the bat...I am just starting to learn PHP. I am trying to create just a very basic page that will insert book data into a database. When I run it I get NO ERRORS, but I also get no results. Nothing is inserted into the database. Here is my code: <html> <head> <title>Book-O-Rama - New Book Entry</title> </head> <body> <h1>Book-O-Rama - New Book Entry</h1> <form action="insert_book.php" method="post"> <table border="0"> <tr> <td>ISBN</td> <td><input type="text" name="isbn" maxlength="13" size="13"></td> </tr> <tr> <td>Author</td> <td> <input type="text" name="author" maxlength="30" size="30"></td> </tr> <tr> <td>Title</td> <td> <input type="text" name="title" maxlength="60" size="30"></td> </tr> <tr> <td>Price $</td> <td><input type="text" name="price" maxlength="7" size="7"></td> </tr> <tr> <td colspan="2"><input type="submit" value="Register"></td> </tr> </table> </form> </body> </html> and here is the php insert page: <html> <head> <title>Book-O-Rama Book Entry Results</title> </head> <body> <h1>Book-O-Rama Book Entry Results</h1> <?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); } $dbhost = 'localhost'; $dbuser = 'XXXXXX_XXXXXX; $dbpass = 'XXXXXXXXX'; //Connect to Database @ $db = new mysqli($dbhost, $dbuser, $dbpass); //Select Database $dbname = 'XXXXXX_bookorama'; 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.'; $db->close(); ?> </body> </html> Any help would be greatly appreciated. Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/39525-problem-with-results-not-showing-up/ Share on other sites More sharing options...
nloding Posted February 21, 2007 Share Posted February 21, 2007 You're missing a closing ' on $dbuser ... That's the only thing I see. BTW, "PHP and MySQL Web Development" is a great book ... try to add to it's examples, it really helped me. Link to comment https://forums.phpfreaks.com/topic/39525-problem-with-results-not-showing-up/#findComment-190735 Share on other sites More sharing options...
mcclellanfsu Posted February 21, 2007 Author Share Posted February 21, 2007 Thanks for the quick response! The ' in $dbuser was actually there...I just accidentally deleted it when I was putting the XXXX in there. I agree on the book. That was my plan, to add more but I thought I should get the original working first! Now I am getting another error (but at least it's not a blank screen this time).... Fatal error: Call to a member function on a non-object in /booktest/results.php on line 44 Here is the code (i'll mark line 44).... <body> <h1>Book-O-Rama Search Results</h1> <?php // create short variable names $searchtype=$_POST['searchtype']; $searchterm=$_POST['searchterm']; $searchterm= trim($searchterm); if (!$searchtype || !$searchterm) { echo 'You have not entered search details. Please go back and try again.'; exit; } if (!get_magic_quotes_gpc()) { $searchtype = addslashes($searchtype); $searchterm = addslashes($searchterm); } $db=mysql_connect ("localhost", "XXXXXXXX_XXXXXXX", "XXXXX") or die ('I cannot connect to the database because: ' . mysql_error()); mysql_select_db ("XXXXXXX_bookorama"); $query = "select * from books where ".$searchtype." like '%".$searchterm."%'"; $result = $db->query($query); // THIS IS LINE 44 $num_results = $result->num_rows; echo '<p>Number of books found: '.$num_results.'</p>'; for ($i=0; $i <$num_results; $i++) { $row = $result->fetch_assoc(); echo '<p><strong>'.($i+1).'. Title: '; echo htmlspecialchars(stripslashes($row['title'])); echo '</strong><br />Author: '; echo stripslashes($row['author']); echo '<br />ISBN: '; echo stripslashes($row['isbn']); echo '<br />Price: '; echo stripslashes($row['price']); echo '</p>'; } $result->free(); $db->close(); ?> </body> Any help will be greatly appreciated! Link to comment https://forums.phpfreaks.com/topic/39525-problem-with-results-not-showing-up/#findComment-190820 Share on other sites More sharing options...
nloding Posted February 22, 2007 Share Posted February 22, 2007 Fatal error: Call to a member function on a non-object in /booktest/results.php on line 44 You're using two forms of a mysql object -- you create $db using regular mysql, but then try to call it using mysqli objects. Google the difference ... mysqli is slower, but more functionality but requires PHP5. <?php //$db=mysql_connect ("localhost", "XXXXXXXX_XXXXXXX", "XXXXX") or die ('I cannot connect to the database because: ' . mysql_error()); //mysql_select_db ("XXXXXXX_bookorama"); //here's the mysqli way: $db = new mysqli("localhost", "user", "pass", "XXX_bookorama"); //now $db->query works //either that or replace $db->query with mysql_query ... $query = "select * from books where ".$searchtype." like '%".$searchterm."%'"; $result = $db->query($query); // THIS IS LINE 44 ?> Link to comment https://forums.phpfreaks.com/topic/39525-problem-with-results-not-showing-up/#findComment-190919 Share on other sites More sharing options...
mcclellanfsu Posted February 22, 2007 Author Share Posted February 22, 2007 Thank everyone very much for their quick responses! You are helping me out greatly! I have one last problem now. The error I am receiving is: Fatal error: Call to a member function on a non-object in /booktest/results.php on line 65 My code is: <body> <h1>Book-O-Rama Search Results</h1> <?php // create short variable names $searchtype=$_POST['searchtype']; $searchterm=$_POST['searchterm']; $searchterm= trim($searchterm); if (!$searchtype || !$searchterm) { echo 'You have not entered search details. Please go back and try again.'; exit; } if (!get_magic_quotes_gpc()) { $searchtype = addslashes($searchtype); $searchterm = addslashes($searchterm); } $db=mysql_connect ("localhost", "XXXXX_booktst", "XXXX") or die ('I cannot connect to the database because: ' . mysql_error()); mysql_select_db ("XXXXXXX_bookorama"); $query = "select * from books where ".$searchtype." like '%".$searchterm."%'"; $result = mysql_query($query); //Change made here -- used to be $db->query $num_results = $result->num_rows; echo '<p>Number of books found: '.$num_results.'</p>'; for ($i=0; $i <$num_results; $i++) { $row = $result->fetch_assoc(); echo '<p><strong>'.($i+1).'. Title: '; echo htmlspecialchars(stripslashes($row['title'])); echo '</strong><br />Author: '; echo stripslashes($row['author']); echo '<br />ISBN: '; echo stripslashes($row['isbn']); echo '<br />Price: '; echo stripslashes($row['price']); echo '</p>'; } $result->free(); //This is Line 65 $db->close(); ?> </body> Once again any help will be greatly appreciated. Thanks! Link to comment https://forums.phpfreaks.com/topic/39525-problem-with-results-not-showing-up/#findComment-191335 Share on other sites More sharing options...
mcclellanfsu Posted February 22, 2007 Author Share Posted February 22, 2007 Ok, I changed my code up a little bit, but now I am getting a new error.... Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in /booktest/insert_book.php on line 39 This is line 39: mysql_free_result($result); The rest of my code: <body> <h1>Book-O-Rama Book Entry Results</h1> <?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); } //Connect to Database $db=mysql_connect ("localhost", "mcclella_booktst", "testing") or die ('I cannot connect to the database because: ' . mysql_error()); mysql_select_db ("mcclella_bookorama"); $query = "insert into books values ('".$isbn."', '".$author."', '".$title."', '".$price."')"; $result = mysql_query($query); if ($result) echo $db->affected_rows.' The book has been sucessfully inserted into the database.'; mysql_free_result($result); //This is line 39 mysql_close(); ?> Link to comment https://forums.phpfreaks.com/topic/39525-problem-with-results-not-showing-up/#findComment-191412 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.