Jump to content

INSERT inserting blank record plus form data!


twilitegxa

Recommended Posts

What did I do wrong? Every time my form submits, it inserts the record, along with a blank record. I have had this problem before, but I can't remember what caused it. Can anyone help? Here is the form and insert page:

 

form:

<?php

$id = $_GET['id'];

//get defects
$get_defects = "select * from defects";
$get_defects_res = mysql_query($get_defects, $conn) or die(mysql_error());

$display_block = "<form action=insert_defect.php?id=$id method='post'>
<table><th>Defect</th><th>Level</th>
<tr><td><select name='defect'>";

while($defect_info = mysql_fetch_array($get_defects_res)){
$id = $defect_info['id'];
$defect = $defect_info['defect'];

$display_block .= "<option value=$id>$defect</option>";

}

$display_block .= "</select></td>";

$display_block .= "<td><select name=level>
<option value=1>1 BP</option>
<option value=2>2 BP</option>
</select></td></tr>
<tr><td> </td></tr>
<tr><td colspan=2><b>Description:</b> <i>(optional)</i><br />
<input type=text name=desc>
(Ex. Phobia - <u>Storms</u>)</td></tr>
<tr><td> </td></tr>
<tr><td colspan=2><b>Notes:</b> <i>(optional)</i><br />
<textarea name=notes></textarea><br />
(Ex. Although Rini is not a Sailor Scout when she first arrives,<br />she experiences a large degree of ageism (2 BP). Serena and<br />
the others simply treat Rini as a young child, forgetting how<br /> mature Rini really is for her age.)</td></tr>
<tr><td> </td></tr>
<tr><td><input type=submit value='Add Defect'> <input type=button value=Cancel></td></tr>
</table>

</form>";

echo $display_block;

?>

 

insert:

<?php

//get identity
$get_identity = "select * from scouts where id = '$_GET[id]'";
$get_identity_res = mysql_query($get_identity, $conn) or die(mysql_error());

while($identity_info = mysql_fetch_array($get_identity_res)){
$identity = $identity_info['identity'];
}

//insert defects
$insert_defect = "insert into scout_defects values ('', '$identity', '$_POST[defect]', '$_POST[desc]', '$_POST[level]', '$_POST[notes]')";
$insert_defect_res = mysql_query($insert_defect, $conn) or die(mysql_error());

?>

Link to comment
Share on other sites

$insert_defect = "insert into scout_defects values ('', '$identity', '$_POST[defect]', '$_POST[desc]', '$_POST[level]', '$_POST[notes]')";

 

Have I lost it or don't you need to define which columns you are inserting into?

 

$insert_defect = "insert into scout_defects (this, that, the_other) values ('',...)

 

also your $_POST[defect]

should be changed to $_POST['defect'] and therefore should be {$_POST['defect']}  ....  I think..... like 60% confident

Link to comment
Share on other sites

Well, I know you don't have to have which columns the values go into. But, the problem is that the posted values are getting sent, it's just instering an extra blank record for some reason, so I don't think the single quotes are the problem, but I can try it...

 

Any other suggestions?

Link to comment
Share on other sites

Your browser is requesting the page twice, once with the form's data and once by just referencing the URL of the page. Different browsers do this for different reasons, but the biggest offender is FF because some of the debugging add-on's request the page a second time. Also, some web server setups and url rewriting can cause this.

 

Your form processing code is not validating the data and in fact it is not even checking if the form was submitted before executing the mysql_query() statement.

Link to comment
Share on other sites

Thanks for the information. I will start working on my validation and see if I need any more help. Thanks!

 

Is there a way around the FireFox Debugging issue? I don't want this problem recurring. Will validation prevent this?

Link to comment
Share on other sites

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.