twilitegxa Posted March 25, 2010 Share Posted March 25, 2010 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()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/196436-insert-inserting-blank-record-plus-form-data/ Share on other sites More sharing options...
andrewgauger Posted March 25, 2010 Share Posted March 25, 2010 $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 Quote Link to comment https://forums.phpfreaks.com/topic/196436-insert-inserting-blank-record-plus-form-data/#findComment-1031396 Share on other sites More sharing options...
twilitegxa Posted March 25, 2010 Author Share Posted March 25, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/196436-insert-inserting-blank-record-plus-form-data/#findComment-1031400 Share on other sites More sharing options...
andrewgauger Posted March 25, 2010 Share Posted March 25, 2010 throw an: echo $insert_defect line after defining it and post it please. That should help debugging the sql. Quote Link to comment https://forums.phpfreaks.com/topic/196436-insert-inserting-blank-record-plus-form-data/#findComment-1031406 Share on other sites More sharing options...
PFMaBiSmAd Posted March 25, 2010 Share Posted March 25, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/196436-insert-inserting-blank-record-plus-form-data/#findComment-1031409 Share on other sites More sharing options...
andrewgauger Posted March 25, 2010 Share Posted March 25, 2010 Sorry for the bad advice/ wild-goose chase. Thank you PFMaBiSmAd for this insight on FireFox w/ add-ons I will look into my own code to see if this is happening without my knowledge to me. Quote Link to comment https://forums.phpfreaks.com/topic/196436-insert-inserting-blank-record-plus-form-data/#findComment-1031421 Share on other sites More sharing options...
twilitegxa Posted March 25, 2010 Author Share Posted March 25, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/196436-insert-inserting-blank-record-plus-form-data/#findComment-1031433 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.