HokieTracks Posted November 25, 2009 Share Posted November 25, 2009 I feel like an idiot because I have spent the last hour trying to figure out what is wrong with my code here. I feel like it must be a typo somewhere but I can't find one. <form action="questioninsert.php" method="post" name="question"> Asked By:<br> <input type="text" name="asker"><br><br> Answered By:<br> <input type="text" name="by"><br><br> Question:<br> <textarea cols="115" rows="20" name="question"></textarea><br><br> Answer:<br> <textarea cols="115" rows="20" name="answer"></textarea> <input type="submit" value="Post"> </form> <?php mysql_connect("localhost","*******_*****","********"); mysql_select_db("******_****"); $asker = mysql_real_escape_string(stripslashes($_POST['asker'])); $by = mysql_real_escape_string(stripslashes($_POST['by'])); $question = mysql_real_escape_string(stripslashes(nl2br(ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\">\\0</a>", $_POST['question'])))); $answer = mysql_real_escape_string(stripslashes(nl2br(ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\">\\0</a>", $_POST['answer'])))); $date = mysql_real_escape_string(date("Y-m-d H:i:s")); mysql_query("INSERT INTO questions (question, answer, asker, by, date) VALUES ('$question', '$answer', '$asker', '$by', '$date')"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/182947-whats-wrong-here/ Share on other sites More sharing options...
rajivgonsalves Posted November 25, 2009 Share Posted November 25, 2009 first of all always put a die to your mysql queries mysql_query("INSERT INTO questions (question, answer, asker, by, date) VALUES ('$question', '$answer', '$asker', '$by', '$date')") or die("Cannot execute".mysql_error()); secondly I think date is a mysql reserve keyword so you should enclose your field names in `` Quote Link to comment https://forums.phpfreaks.com/topic/182947-whats-wrong-here/#findComment-965629 Share on other sites More sharing options...
HokieTracks Posted November 25, 2009 Author Share Posted November 25, 2009 first of all always put a die to your mysql queries mysql_query("INSERT INTO questions (question, answer, asker, by, date) VALUES ('$question', '$answer', '$asker', '$by', '$date')") or die("Cannot execute".mysql_error()); secondly I think date is a mysql reserve keyword so you should enclose your field names in `` Oh yeah I totally forgot about die. But enclosing my field names with ' ' didn't help. I've used date as a field name for mysql before without any problems so I don't think it could be a problem with that. I'm getting this error "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 'by, date) VALUES ('sadsafads', 'fasdfsdafdsa', 'dsafdsf', 'dasfd', '2009-11-25 1' at line 1". Which doesn't make a lot of sense to me because if the syntax is right before it then why is it wrong half way through the query. Quote Link to comment https://forums.phpfreaks.com/topic/182947-whats-wrong-here/#findComment-965630 Share on other sites More sharing options...
abazoskib Posted November 25, 2009 Share Posted November 25, 2009 He didn't say enclose them in '', he said enclose them with ``. There is a difference. Quote Link to comment https://forums.phpfreaks.com/topic/182947-whats-wrong-here/#findComment-965639 Share on other sites More sharing options...
HokieTracks Posted November 25, 2009 Author Share Posted November 25, 2009 He didn't say enclose them in '', he said enclose them with ``. There is a difference. Oh my bad, but I got it to work without it. I tried changing the field name of "by" to "author" and it started working. Is there some reason that mysql doesn't like "by" as a field name? Quote Link to comment https://forums.phpfreaks.com/topic/182947-whats-wrong-here/#findComment-965644 Share on other sites More sharing options...
waynew Posted November 25, 2009 Share Posted November 25, 2009 first of all always put a die to your mysql queries mysql_query("INSERT INTO questions (question, answer, asker, by, date) VALUES ('$question', '$answer', '$asker', '$by', '$date')") or die("Cannot execute".mysql_error()); Using die() blindly is bad practise. Use <?php mysql_query("INSERT INTO questions (question, answer, asker, by, date) VALUES ('$question', '$answer', '$asker', '$by', '$date')") or trigger_error("Cannot execute".mysql_error()); ?> You should really be checking to see if a query was successful. How is killing the entire script user friendly? secondly I think date is a mysql reserve keyword so you should enclose your field names in `` Quote Link to comment https://forums.phpfreaks.com/topic/182947-whats-wrong-here/#findComment-965651 Share on other sites More sharing options...
Psycho Posted November 25, 2009 Share Posted November 25, 2009 Didn't a previous poster make the statement that "DATE" might be a reserved word? You then find that when changing from "BY" to "AUTHOR" it starts working. So, wouldn't it be logical to assume that "BY" may be a reserved word? A quick google search would have confirmed that: http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html Quote Link to comment https://forums.phpfreaks.com/topic/182947-whats-wrong-here/#findComment-965653 Share on other sites More sharing options...
HokieTracks Posted November 25, 2009 Author Share Posted November 25, 2009 Didn't a previous poster make the statement that "DATE" might be a reserved word? You then find that when changing from "BY" to "AUTHOR" it starts working. So, wouldn't it be logical to assume that "BY" may be a reserved word? A quick google search would have confirmed that: http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html Thanks for the info, no need to get mad though. I'm still learning php so I am just trying to learn as much as I can by asking questions. And, would you really rather have me use google to find an answer than use the website which you seem to be heavily involved in? I would think you would want to be a better php source than google search. Quote Link to comment https://forums.phpfreaks.com/topic/182947-whats-wrong-here/#findComment-965664 Share on other sites More sharing options...
waynew Posted November 25, 2009 Share Posted November 25, 2009 first of all always put a die to your mysql queries mysql_query("INSERT INTO questions (question, answer, asker, by, date) VALUES ('$question', '$answer', '$asker', '$by', '$date')") or die("Cannot execute".mysql_error()); Using die() blindly is bad practise. Use <?php mysql_query("INSERT INTO questions (question, answer, asker, by, date) VALUES ('$question', '$answer', '$asker', '$by', '$date')") or trigger_error("Cannot execute".mysql_error()); ?> You should really be checking to see if a query was successful. How is killing the entire script user friendly? Quote Link to comment https://forums.phpfreaks.com/topic/182947-whats-wrong-here/#findComment-965670 Share on other sites More sharing options...
Psycho Posted November 25, 2009 Share Posted November 25, 2009 Not mad, just trying to get people to think logically and solve thier own problems. That is the essence of programming in my opinion. I have no incentive to people asking questions on this site. I do this for my own benefit. Trying to solve real problems is an activity that helps exercise my mind and it is enlightening to see how others would solve a problem differently than myself. I surely don't want to spend time answering questions that could be answered by a search engine. Quote Link to comment https://forums.phpfreaks.com/topic/182947-whats-wrong-here/#findComment-965671 Share on other sites More sharing options...
HokieTracks Posted November 25, 2009 Author Share Posted November 25, 2009 Not mad, just trying to get people to think logically and solve thier own problems. That is the essence of programming in my opinion. I have no incentive to people asking questions on this site. I do this for my own benefit. Trying to solve real problems is an activity that helps exercise my mind and it is enlightening to see how others would solve a problem differently than myself. I surely don't want to spend time answering questions that could be answered by a search engine. Fair enough. My fault for assuming you were trying to be mean. first of all always put a die to your mysql queries mysql_query("INSERT INTO questions (question, answer, asker, by, date) VALUES ('$question', '$answer', '$asker', '$by', '$date')") or die("Cannot execute".mysql_error()); Using die() blindly is bad practise. Use <?php mysql_query("INSERT INTO questions (question, answer, asker, by, date) VALUES ('$question', '$answer', '$asker', '$by', '$date')") or trigger_error("Cannot execute".mysql_error()); ?> You should really be checking to see if a query was successful. How is killing the entire script user friendly? Good point, I never actually thought about that before. Definitely something I will use in the future. Quote Link to comment https://forums.phpfreaks.com/topic/182947-whats-wrong-here/#findComment-965676 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.