Jump to content

Recommended Posts

Hi Guys,

Sorry for the Newb question, but this is stumping me, and I am not finding any posts similar to my problem. I have created a simple PHP/MySQL guestbook on my website, and it actually works when you use IE. But I tried it using Firefox the other day and I got a "Couldn't Execute Query" (from my code) error.

Here is the code:

<?php

$host="localhost";

$user="root";

$password="";

$database="guest";

$connection=mysql_connect($host,$user,$password)

or die ("Couldn't connect to server");

$db=mysql_select_db($database,$connection)

or die ("Couldn't select database");

if ($button == "Submit")

{

foreach ($HTTP_POST_VARS as $value)

{

$query="INSERT INTO entry (name,comment)

VALUES ('$name','$comment')";

$result=mysql_query($query)

or die ("Couldn't Execute Query.");

}

}

?>

 

The code is sent three POST variables - $button, $name and $comment. My database has a table named entry. Entry has three fields - time, name, comment. Time is a TIMESTAMP field with the default value set to NULL so that MySQL will insert a value. Time is also the primary key. Name is setup as a STRING, and Comment is setup as LONGTEXT.

I believe my error is just the format that I am using for my INSERT string, but I am not yet skilled enough with PHP or MySQL to determine what I am doing wrong. Any help would be greatly appreciated! Thank you,

Everett

Link to comment
https://forums.phpfreaks.com/topic/2763-newbie-requests-help-insert-problem/
Share on other sites

if ($button == "Submit")

{

foreach ($HTTP_POST_VARS as $value)

{

$query="INSERT INTO entry (name,comment)

VALUES ('$name','$comment')";

$result=mysql_query($query)

or die ("Couldn't Execute Query.");

}

}

 

Firstly, that "foreach()" loop will execute the same insert 3 times if there are 3 POSTed values.

 

Secondly, you need to collect the variables from the $_POST array (use $_POST instea of $HTTP_POST_VARS if you are using php version above 4.0) in case "register_globals" is OFF (as it should be) on the server.

 

[!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--][span style=\"color:#0000BB\"]<?php

[/span][span style=\"color:#007700\"]if (isset([/span][span style=\"color:#0000BB\"]$_POST[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'button\'[/span][span style=\"color:#007700\"]])) {

    [/span][span style=\"color:#0000BB\"]$button [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]$_POST[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'button\'[/span][span style=\"color:#007700\"]];

    if ([/span][span style=\"color:#0000BB\"]$button [/span][span style=\"color:#007700\"]== [/span][span style=\"color:#DD0000\"]\"Submit\"[/span][span style=\"color:#007700\"])

    {

        [/span][span style=\"color:#0000BB\"]$name [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]$_POST[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'name\'[/span][span style=\"color:#007700\"]];

        [/span][span style=\"color:#0000BB\"]$comment [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]$_POST[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'comment\'[/span][span style=\"color:#007700\"]];

 

        [/span][span style=\"color:#0000BB\"]$query[/span][span style=\"color:#007700\"]=[/span][span style=\"color:#DD0000\"]\"INSERT INTO entry (name,comment)

        VALUES (\'$name\',\'$comment\')\"[/span][span style=\"color:#007700\"];

        [/span][span style=\"color:#0000BB\"]$result[/span][span style=\"color:#007700\"]=[/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$query[/span][span style=\"color:#007700\"])

        or die ([/span][span style=\"color:#DD0000\"]\"Couldn\'t Execute Query.\"[/span][span style=\"color:#007700\"]);

    }

}

 

[/span][span style=\"color:#0000BB\"]?>[/span]

[/span][!--PHP-Foot--][/div][!--PHP-EFoot--]

Hi Barand,

Thanks for the reply. And thanks for helping me clean up my code. I visted the PHP.net site after I read your reply and looked up some of the functions that you used, and agreee my original code needed improvement.

After much tinkering around, and some modification to my code, I actually found that the reason Firefox and Opera were not submitting entries correctly was because of the window configuration I was using. I was submitting the info from a child window that also closed itself when the submit button was pressed. Firefox and Opera close the window so fast, the information was never submitted to the database. I have fixed the window and everything works great.

 

Thanks again for all of the help. I would have been looking in the wrong direction for days I'm sure. Cheers,

Everett

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

×
×
  • 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.