Jump to content

Extra Empty Row or Duplicate on Insert


a2bardeals

Recommended Posts

ok so i am playing around with a simple script. a form with a post action sending to a page with the sql insert code:

my form is simple:
[code]<form action="do_register.php" method="POST">
Name: <input type="text" name="fname" size="20">
Age: <select name="age">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
Favorite Color: <select name="color">
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
<option value="orange">Orange</option>
<option value="purple">Purple</option>
<option value="pink">Pink</option>
</select>
<input type="SUBMIT" value="GO">
</form>[/code]

this sends the data just fine. it goes to "do_register.php"

then i insert the data:

[code]
<?
$con = mysql_connect("localhost", "user", "pass");

mysql_select_db("database", $con);

$sql = "INSERT INTO 'table' (`fname`, `age` , `color`)
VALUES ('$_POST[fname]' , '$_POST[age]' , '$_POST[color]');";

if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }

echo 'Thanks, ';
echo $fname;
echo '<br>Your info has been submitted!';

?>
[/code]


it all seems fine after you post but when i call the results there is one row with the data and another blank row inserted after it. The ID row which is the index is set to auto_increment and it assignes two rows (eg. one submission = one row id:23 with the data and one row id:24 with no data.)

Any ideas?
Link to comment
https://forums.phpfreaks.com/topic/28901-extra-empty-row-or-duplicate-on-insert/
Share on other sites

Also, and not to be picky, but your insert command should reference the array members with quotes.  Otherwise, it treats the string as a constant and has to figure out that it isn't, which is an extra step and will slow down the server if it has a lot of requests.  To illustrate:

Use this:
VALUES ('{$_POST['fname']}' , '{$_POST['age']}' , '{$_POST['color']}');";

Instead of:
VALUES ('$_POST[fname]' , '$_POST[age]' , '$_POST[color]');";

[quote author=a2bardeals link=topic=116755.msg475889#msg475889 date=1164835509]
ok so i am playing around with a simple script. a form with a post action sending to a page with the sql insert code:

my form is simple:
[code]<form action="do_register.php" method="POST">
Name: <input type="text" name="fname" size="20">
Age: <select name="age">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
Favorite Color: <select name="color">
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
<option value="orange">Orange</option>
<option value="purple">Purple</option>
<option value="pink">Pink</option>
</select>
<input type="SUBMIT" value="GO">
</form>[/code]

this sends the data just fine. it goes to "do_register.php"

then i insert the data:

[code]
<?
$con = mysql_connect("localhost", "user", "pass");

mysql_select_db("database", $con);

$sql = "INSERT INTO 'table' (`fname`, `age` , `color`)
VALUES ('$_POST[fname]' , '$_POST[age]' , '$_POST[color]');";

if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }

echo 'Thanks, ';
echo $fname;
echo '<br>Your info has been submitted!';

?>
[/code]


it all seems fine after you post but when i call the results there is one row with the data and another blank row inserted after it. The ID row which is the index is set to auto_increment and it assignes two rows (eg. one submission = one row id:23 with the data and one row id:24 with no data.)

Any ideas?

[/quote]
I am using firefox 1.5.0.8 and my only other testing browser is Safari and it does not insert the extra row in Safari. So it may be a Firefox issue. But why would Firefox submit twice? It makes no sense to me and i need to fix it! Grrrrrr.

If you would like to test what im working with a different browser, go to: [url=http://www.barcheese.com/newyears]http://www.barcheese.com/newyears[/url]


Help!

Archived

This topic is now archived and is closed to further replies.

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