mbrown Posted March 2, 2017 Share Posted March 2, 2017 I am not having fun with this code this evening. What am I missing? I know I am tired but still. Any help would be great! <?php include_once ('include/db-config.php'); #Debug: echo $_SERVER['HTTP_REFERER']; $motionid=$_POST['motionid']; $userid=$_POST['userid']; $text=$_POST['discussiontext']; #echo "Motion ID: " . $motionid . "<br />User ID: " . $userid . "<br />Text: " . $text . "<br />"; if ( strlen($text) == 0 ) { echo "You have not entered any text"; } else { $addDiscussion=$db_con->prepare( "INSERT INTO discussion (user_id,motion_id,discussion_text) VALUES(:userid, :motionid, :text"); $addDiscussion->bindParam(':userid',$userid); $addDiscussion->bindParam(':motionid',$motionid); $addDiscussion->bindParam(':text',$text); $addDiscussion->execute(); echo "Added Discussion Text"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/303330-insert-is-not-running/ Share on other sites More sharing options...
Jacques1 Posted March 2, 2017 Share Posted March 2, 2017 (edited) Can you be a bit more specific? We can neither see your screen nor read your mind. What exactly happens? An error? A blank page? Something else? If it's a blank page, enable PDO exceptions, turn your error reporting all the way up and make PHP display the messages on the screen (assuming this is your development machine, not the live server). Edited March 2, 2017 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/303330-insert-is-not-running/#findComment-1543602 Share on other sites More sharing options...
mbrown Posted March 2, 2017 Author Share Posted March 2, 2017 Can you be a bit more specific? We can neither see your screen nor read your mind. What exactly happens? An error? A blank page? Something else? If it's a blank page, enable PDO exceptions, turn your error reporting all the way up and make PHP display the messages on the screen (assuming this is your development machine, not the live server). The echo runs but the INSERT does not actually run. I have my my sql server running everything and the INSERT is not executing. Quote Link to comment https://forums.phpfreaks.com/topic/303330-insert-is-not-running/#findComment-1543603 Share on other sites More sharing options...
Jacques1 Posted March 2, 2017 Share Posted March 2, 2017 There's a missing parenthesis in the query, and PDO would have told you that if you had enabled exceptions as I told you to. Quote Link to comment https://forums.phpfreaks.com/topic/303330-insert-is-not-running/#findComment-1543604 Share on other sites More sharing options...
kessie Posted March 2, 2017 Share Posted March 2, 2017 the code here looks okay, the problem could be at some other portion of the code. A thorough debugging could work. Take a time out to rest a bit and look into the code, I'm sure you'll find the problem. Quote Link to comment https://forums.phpfreaks.com/topic/303330-insert-is-not-running/#findComment-1543611 Share on other sites More sharing options...
Jacques1 Posted March 2, 2017 Share Posted March 2, 2017 Once again: Read the previous posts before you reply. It's great that you want to help, but it's not helpful to post false information which contradicts the facts that have already been established. No, the code is not OK. As I've already said, the query is syntactically wrong. Looking somewhere else isn't going to fix that. 1 Quote Link to comment https://forums.phpfreaks.com/topic/303330-insert-is-not-running/#findComment-1543613 Share on other sites More sharing options...
Strider64 Posted March 2, 2017 Share Posted March 2, 2017 (edited) You really should have error reporting turned on and an IDE that will flag syntax errors (it'll make life much easier). I would also separate the query from the prepare. Here's an example of mine -> $query = 'INSERT INTO trivia_questions (question, answer1, answer2, answer3, answer4, correct, category, play_date) VALUES (:question, :answer1, :answer2, :answer3, :answer4, :correct, :category, NOW())'; $stmt = $pdo->prepare($query); That way you can easily debug the script. Everyone who writes code gets syntax errors which are easily fixable and the less time you spend on them the more you can concentrate on the baddies (logic errors). Edited March 2, 2017 by Strider64 Quote Link to comment https://forums.phpfreaks.com/topic/303330-insert-is-not-running/#findComment-1543614 Share on other sites More sharing options...
mac_gyver Posted March 2, 2017 Share Posted March 2, 2017 @kessie, i have the same commentary about non-specific replies and replies repeating information already posted, just made for the purpose of posting a reply, any reply. when someone has a specific problem on any kind of help forum (cars, appliances, programming), replies that don't specifically and directly help with the subject matter, and are nothing more than supposition, just knock the thread off topic. an example i've had to deal with in finding a car part - someone asks about finding a replacement for a non-standard light bulb, for a specific year and model of a car, not listed in the operating manual, not available in the local auto parts store, which costs $10-20 from the dealer, and is available for less than $1 at electronics parts suppliers. anyone not posting specific, accurate, and relevant information about that light bulb for that year and model of car didn't help at the time of posting and doesn't help anyone who finds the thread later. @mbrown, if you form your sql query statement in a php variable, it will help avoid the type of sql syntax error you have, because it will separate the syntax of the sql statement from the php syntax that's using the sql statement. Quote Link to comment https://forums.phpfreaks.com/topic/303330-insert-is-not-running/#findComment-1543615 Share on other sites More sharing options...
Jacques1 Posted March 2, 2017 Share Posted March 2, 2017 Another way to separate the PHP code from the SQL query is to use clear formatting: $addDiscussion = $db_con->prepare(' INSERT INTO discussion (user_id, motion_id, discussion_text) VALUES (:userid, :motionid, :text) '); But really the most important step is configure the error handling. No database API just silently refuses to execute a query, there's always a detailed error message available. PHP usually expects you to fetch it manually (which is unrealistic), but when you enable exceptions, you'll get it automatically. Quote Link to comment https://forums.phpfreaks.com/topic/303330-insert-is-not-running/#findComment-1543619 Share on other sites More sharing options...
mbrown Posted March 2, 2017 Author Share Posted March 2, 2017 Another way to separate the PHP code from the SQL query is to use clear formatting: $addDiscussion = $db_con->prepare(' INSERT INTO discussion (user_id, motion_id, discussion_text) VALUES (:userid, :motionid, :text) '); But really the most important step is configure the error handling. No database API just silently refuses to execute a query, there's always a detailed error message available. PHP usually expects you to fetch it manually (which is unrealistic), but when you enable exceptions, you'll get it automatically. Thank you jacques. That is the plan moving forward. I got alot of the functionality out there so we could start using the system. I have all ready created a bug in our bug tracking system to make sure that everything is handled like you explained with the PDO exceptions. I can not believe that I did not code those in since day 1. You really should have error reporting turned on and an IDE that will flag syntax errors (it'll make life much easier). I would also separate the query from the prepare. Here's an example of mine -> $query = 'INSERT INTO trivia_questions (question, answer1, answer2, answer3, answer4, correct, category, play_date) VALUES (:question, :answer1, :answer2, :answer3, :answer4, :correct, :category, NOW())'; $stmt = $pdo->prepare($query); That way you can easily debug the script. Everyone who writes code gets syntax errors which are easily fixable and the less time you spend on them the more you can concentrate on the baddies (logic errors). Again Thank you for your advise. If I do it this way then I could still use the bind parameter lines to set up those variables correct? When I was in school we used mysqli which I know is outdated now so this is my first attempt using PDO. Once again: Read the previous posts before you reply. It's great that you want to help, but it's not helpful to post false information which contradicts the facts that have already been established. No, the code is not OK. As I've already said, the query is syntactically wrong. Looking somewhere else isn't going to fix that. Like Jacques1 stated, the code was not fine and that the syntax was incorrect. That is what I get for doing it while I was tired. Quote Link to comment https://forums.phpfreaks.com/topic/303330-insert-is-not-running/#findComment-1543625 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.