Insert statement problem

I have a really basic question, but for some reason I can't figure out what I'm doing wrong here. I use virtually identical code on another site and it works fine, so I'm wondering if it might be a database issue rather than a code issue.

I have an insert statement based on form data that submits to a mySql database. The problem is that each time it inserts a record, it also inserts an extra blank record. The insert statement is not within a loop, and the ID field is auto-increment. Here's the code I'm using:


$name = $_POST['name'];
$address = $_POST['address'];
$citystate = $_POST['citystate'];
$phone = $_POST['phone'];
$email = $_POST['email'];

$sql = "INSERT INTO table (name, address, citystate, phone, email) VALUES ("$name", "$address", "$citystate", "$phone", "$email")";

$result = mysql_query($sql) or die(mysql_error()); // printing mysql_error always helps

if ($result != 0) {
echo "Thank you, ";
echo $name;
echo "!";
echo "


Click here to return to home page.";}
else {
echo "Insert has failed";
echo "
SQL STATEMENT:
";
echo $sql;
}

Anyone know what I'm missing here?

Thanks.

Comments

  • : I have a really basic question, but for some reason I can't figure out what I'm doing wrong here. I use virtually identical code on another site and it works fine, so I'm wondering if it might be a database issue rather than a code issue.
    :
    : I have an insert statement based on form data that submits to a mySql database. The problem is that each time it inserts a record, it also inserts an extra blank record. The insert statement is not within a loop, and the ID field is auto-increment. Here's the code I'm using:
    :
    :
    : $name = $_POST['name'];
    : $address = $_POST['address'];
    : $citystate = $_POST['citystate'];
    : $phone = $_POST['phone'];
    : $email = $_POST['email'];
    :
    : $sql = "INSERT INTO table (name, address, citystate, phone, email) VALUES ("$name", "$address", "$citystate", "$phone", "$email")";
    :
    : $result = mysql_query($sql) or die(mysql_error()); // printing mysql_error always helps
    :
    : if ($result != 0) {
    : echo "Thank you, ";
    : echo $name;
    : echo "!";
    : echo "Click here to return to home page.";}
    : else {
    : echo "Insert has failed";
    : echo "SQL STATEMENT:";
    : echo $sql;
    : }
    :
    : Anyone know what I'm missing here?
    :
    : Thanks.
    :
    Did you do a mysql_connect(); first...? Does table 'table' exist ? Do all those fields exist ?

    what's the error ?
    ;-)
    -mac-
    mailto:mac_doggie@hotmail.com
    the Netherlands...


  • I am having the same problem. Did you ever figure out why you were having the problem?
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories