midgley Posted January 6, 2009 Share Posted January 6, 2009 im trying to get html form data into mysql. i am new to php so wanted to write something that i understand, so i have not copied anything but have now become stuck. see below: $name=mysql_real_escape_string($_POST['name']); $from=mysql_real_escape_string($_POST['from']); $comment=mysql_real_escape_string($_POST['comment']); $date = date (Y.M.D); echo $name . $from . $comment . $date; //working so far mysql_connect("localhost","root","") or die(mysql_error()); mysql_select_db("blog") or die(mysql_error()); mysql_query("INSERT INTO bloggers(name,from) VALUES('$name','$from')") or die(mysql_error()); //no errors but nothing in database think the problem might be with mysql_query(" please help Link to comment https://forums.phpfreaks.com/topic/139706-solved-the-answer-is-simple-but-i-can-not-seem-to-find-it-and-its-driving-me-crazy/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 6, 2009 Share Posted January 6, 2009 Add the following two lines of code immediately after your first opening <?php tag - ini_set ("display_errors", "1"); error_reporting(E_ALL); Among other possibilities, mysql_real_escape_string() can only be used after you have a connection to the mysql server. Link to comment https://forums.phpfreaks.com/topic/139706-solved-the-answer-is-simple-but-i-can-not-seem-to-find-it-and-its-driving-me-crazy/#findComment-730970 Share on other sites More sharing options...
midgley Posted January 6, 2009 Author Share Posted January 6, 2009 cheers for the error reporting code, will come in handy ini_set ("display_errors", "1"); error_reporting(E_ALL); now connected to the database before using the escape_string. the error message is now "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from) VALUES('james','leeds')' at line 1" Link to comment https://forums.phpfreaks.com/topic/139706-solved-the-answer-is-simple-but-i-can-not-seem-to-find-it-and-its-driving-me-crazy/#findComment-730980 Share on other sites More sharing options...
midgley Posted January 6, 2009 Author Share Posted January 6, 2009 now works $sql=("INSERT INTO bloggers VALUES('$name','$from','$comment','$date')") or die(mysql_error()); mysql($sql); Link to comment https://forums.phpfreaks.com/topic/139706-solved-the-answer-is-simple-but-i-can-not-seem-to-find-it-and-its-driving-me-crazy/#findComment-731009 Share on other sites More sharing options...
fenway Posted January 7, 2009 Share Posted January 7, 2009 now works $sql=("INSERT INTO bloggers VALUES('$name','$from','$comment','$date')") or die(mysql_error()); mysql($sql); NO! It might work, but you've dropped the column list... BAD IDEA. The reason you had the problem was that "from" is a reserved keyword, so you needed to enclose it in backticks (`from`). However, I'd recommend changing the name of the column instead. And I hope you're still escaping those data!!!! Link to comment https://forums.phpfreaks.com/topic/139706-solved-the-answer-is-simple-but-i-can-not-seem-to-find-it-and-its-driving-me-crazy/#findComment-731430 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.