Jump to content


Photo

Newbie requests help - INSERT problem


  • Please log in to reply
2 replies to this topic

#1 everettcomstock

everettcomstock
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 29 October 2005 - 11:34 PM

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

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 30 October 2005 - 07:04 PM

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--]
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 everettcomstock

everettcomstock
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 31 October 2005 - 02:38 AM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users