RichG Posted February 25, 2008 Share Posted February 25, 2008 Hi Everyone I am new to this forum and need to look around at the content to know what information is already out there. So I am sorry of this has already been posted. I am working my way through a 'php and mysql web development' book and I have a small problem. The server is not reading one of my lines in my script. I know there are a few typo's in the book so can you let me know if this is all correct. I am not getting errors from the server it is just not reading my input form my form. I have created a form input page which will input values into a database once submitted (see here http://www.richdgrimes.co.uk/phptest/newbook.html. I have a validation line which checks of all fields have been filled in and returns a error line if you missed something out. But when i fill in all the fields and click submit it returns this error to me. This is what makes me think it may be a typinhg error in the book. Here is the script anyway and i have highlighted the line causing the problems. <?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 fields.<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 mysql( XXXXX', 'XXXX', 'XXXXX', 'XXXX'); if (mysql_connect_errno ()) { echo 'Error: Could not connect to database. Please check your settings and try again.'; exit; } $query = "insert into books values ('".$isbn."', '".$author."', '".$title."', '".$price."')"; $result = $db->query($query); echo $db->affected_rows. 'book inserted into database.'; $db->close(); ?> Hope you can help me Cheers RichG Quote Link to comment Share on other sites More sharing options...
Daney11 Posted February 25, 2008 Share Posted February 25, 2008 try if ($isbn == NULL || $author == NULL || $title == NULL || $price == NULL) Quote Link to comment Share on other sites More sharing options...
Daney11 Posted February 25, 2008 Share Posted February 25, 2008 and on http://www.richdgrimes.co.uk/phptest/newbook.html change the name of the price field from Price to price, all lowercase Quote Link to comment Share on other sites More sharing options...
RichG Posted February 25, 2008 Author Share Posted February 25, 2008 it is all in lowercase. the price with the capps is just text. the null vaules did not work either. could it be a problem on the server? Quote Link to comment Share on other sites More sharing options...
Daney11 Posted February 25, 2008 Share Posted February 25, 2008 Price <input name="Price" type="text" id="Price"> should be Price <input name="price" type="text" id="Price"> Quote Link to comment Share on other sites More sharing options...
timmy0320 Posted February 25, 2008 Share Posted February 25, 2008 $price=$_POST['price']; Your text field on your HTML is "Price" so what he meant by lowercase was change the INPUT FIELD to lowercase. You have <input name="Price" type="text" id="Price"> Change it to <input name="price" type="text" id="price"> Also, you should really do away with get_magic_quotes_gpc() as it will be gone in PHP6 which is soon to release even though all servers won't have it right at the moment, it'd be a good idea. Quote Link to comment Share on other sites More sharing options...
Chris92 Posted February 25, 2008 Share Posted February 25, 2008 Always try to set your variables into an isset or empty function to check if they are 0,1/false,true otherwise you will return an error saying some crap about an undefined error. <?php if( empty($_POST['isbn']) || empty($_POST['author']) || empty($_POST['title']) || empty($_POST['Price']) ) //Don't foget about case sensativity { die(" you have not entered all the required fields Please go back and try again."); } if (!get_magic_quotes_gpc()) { $isbn = addslashes($_POST['isbn']); $author = addslashes($_POST['author']); $title = addslashes($_POST['title']); $price = doubleval($_POST['Price']); } @$db = new mysql('XXXXX', 'XXXX', 'XXXXX', 'XXXX'); if(mysql_connect_errno()) { die("Error: Could not connect to database. Please check your settings and try again."); } $query = "INSERT INTO `books` VALUES('{$isbn}', '{$author}', '{$title}', '{$price}')"; $result = $db->query($query); echo $db->affected_rows."book inserted into database."; $db->close(); ?> Quote Link to comment Share on other sites More sharing options...
aschk Posted February 25, 2008 Share Posted February 25, 2008 sidenote to current topic: In php6 (if and when it arrives) the Engine will throw a E_CORE_ERROR on startup and should point you to the documentation. Apparently they're going to change magic_quotes_* stuff for an extension called input_filter. So i imagine you'll get classes called $filter = new input_filter_string($string); Quote Link to comment Share on other sites More sharing options...
RichG Posted February 25, 2008 Author Share Posted February 25, 2008 Cheers guys and Chris 92. Removed all files from server and made sure caps were not there. Then copied Chris92's text in and it works when i miss out a field. Can I confirm that the 'die' comment works the same as having an 'exit' at the end of an 'echo'? Could you look at my '@ $db' line now please as I get niether the error line saying "could not connect to db" or the other one 'confirming my submission'. I have gone into mysql and the fields have not been submitted. Cheers RichG Quote Link to comment Share on other sites More sharing options...
aschk Posted February 25, 2008 Share Posted February 25, 2008 What is @$db = new MySQL(....) ? Have you written a MySQL class? Do it connect to the database? I would have expected the following instead: @$db = mysql_connect('hostname', 'username', 'password); mysql_select_db("database name"); if(mysql_connect_errno()) { die("Error: Could not connect to database. Please check your settings and try again."); } Quote Link to comment Share on other sites More sharing options...
Chris92 Posted February 25, 2008 Share Posted February 25, 2008 I would use the mysql_connect funstion to connect to the databse, then use the mysql_select_db function to select the database, rather than the way you are doing it: <?php if( empty($_POST['isbn']) || empty($_POST['author']) || empty($_POST['title']) || empty($_POST['Price']) ) //Don't foget about case sensativity { die(" you have not entered all the required fields Please go back and try again."); } if (!get_magic_quotes_gpc()) { $isbn = addslashes($_POST['isbn']); $author = addslashes($_POST['author']); $title = addslashes($_POST['title']); $price = doubleval($_POST['Price']); } $connect = mysql_connect('host:port', 'username', 'password') if( empty($connect) ) { die("Error: Could not connect to mysql server."); } if( !mysql_select_db('database') ) { die("Error: Could not select the specifeid database."); } mysql_query("INSERT INTO `books` VALUES('{$isbn}', '{$author}', '{$title}', '{$price}')"); printf(mysql_affected_rows(), "book inserted into database."); mysql_close($connect); ?> Quote Link to comment Share on other sites More sharing options...
RichG Posted February 25, 2008 Author Share Posted February 25, 2008 Chris92 copied your code in and got this error Parse error: syntax error, unexpected T_IF in /homepages/12/d235019564/htdocs/phptest/insert_book.php on line 30 What version of mysql should i have to be compatable with your code? Quote Link to comment Share on other sites More sharing options...
Chris92 Posted February 25, 2008 Share Posted February 25, 2008 Yeah, I just noticed I forgot to close my line: $connect = mysql_connect('host:port', 'username', 'password') should have a ; at the end, sorry $connect = mysql_connect('host:port', 'username', 'password'); Quote Link to comment Share on other sites More sharing options...
aschk Posted February 25, 2008 Share Posted February 25, 2008 Missing semi-colon ( on this line: $connect = mysql_connect('host:port', 'username', 'password') At ; at the end... Quote Link to comment Share on other sites More sharing options...
RichG Posted February 25, 2008 Author Share Posted February 25, 2008 right we are getting somewhere line by line lol. This book may have to go in the bin as it is a couple of years old and i am out of practice. You are all typing different things now. I now get one of the errors up which i am happy about as it means my code os working. I am now getting the cannot connect to server error. my password and username are right but i am not sure if my host:port is ok. i know my host name but do not have a port no. also should my host name be absolute ie http:// in front of it. Also my account says i have to access the db from within the server so need to get a loaclhost connection. Quote Link to comment Share on other sites More sharing options...
Chris92 Posted February 25, 2008 Share Posted February 25, 2008 You don't usually need to fill the port in, the default port is 3306. It depends on what all is on the server and shit. Quote Link to comment Share on other sites More sharing options...
RichG Posted February 25, 2008 Author Share Posted February 25, 2008 if it does not need the port then all my settings are correct. What else could block my connection and return and empty $connect to create the error. Quote Link to comment Share on other sites More sharing options...
RichG Posted February 25, 2008 Author Share Posted February 25, 2008 Sorry case sensitive again lol. got to get the db right now connected to the server though Quote Link to comment Share on other sites More sharing options...
RichG Posted February 25, 2008 Author Share Posted February 25, 2008 Ok I now have entries into the databse and the connection works fine. Just one last problem When the form is submitted all the data goes to the db but the screen just shows the no 1 in the corner. go to http://www.richdgrimes.co.uk/phptest/newbook.html and see for yourself. Here is the last bit of code mysql_query("INSERT INTO `books` VALUES('{$isbn}', '{$author}', '{$title}', '{$price}')"); printf(mysql_affected_rows(), "book inserted into database."); Cheers RichG Quote Link to comment Share on other sites More sharing options...
timmy0320 Posted February 25, 2008 Share Posted February 25, 2008 Maybe try $num = mysql_affected_rows echo "".$num." book inserted into database."; Quote Link to comment Share on other sites More sharing options...
timmy0320 Posted February 25, 2008 Share Posted February 25, 2008 It won't let me edit my post but I forgot the (); after the mysql_affected_rows $query = mysql_query("INSERT INTO `books` VALUES('{$isbn}', '{$author}', '{$title}', '{$price}')"); mysql_affected_rows($query); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.