Jump to content

INSERT is not running


mbrown
 Share

Recommended Posts

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";
                                }
                        ?>

Link to comment
Share on other sites

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 by Jacques1
Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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 1
Link to comment
Share on other sites

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).  :  :happy-04:

Edited by Strider64
Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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).  :  :happy-04:

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.

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.