thepip3r Posted March 27, 2009 Share Posted March 27, 2009 foreach ($_POST as $key=>$val) { if ($val !== "") { if ($key == "q1" || $key == "q2" || $key == "q3" || $key == "q4") { $val = addslashes(htmlentities($val)); $q = "INSERT INTO survey_question_answers (sqaid, sqid, vc, txt) VALUES(NULL, '".substr($key,1,1)."', '$val', '')"; } elseif ($key == "q5") { $val = addslashes(htmlentities($val)); $q = "INSERT INTO survey_question_answers (sqaid, sqid, vc, txt) VALUES(NULL, '".substr($key,1,1)."', '', '$val')"; echo "$q <br>"; } $r = mysql_query($q); if (!$r) { errorHandler("mysql", 1, 1); } } somehow my q5 area is inserting 3 rows. i'm echoing out my query and it's only writing to the screen once so I don't see why I'm getting three rows inserted into my db... any ideas? Here is an example set of my insert: INSERT INTO survey_question_answers (sqaid, sqid, vc, txt) VALUES(NULL, '5', '', 'test') Quote Link to comment https://forums.phpfreaks.com/topic/151421-solved-mysqlphp-probs-single-insert-somehow-inserting-3-rows/ Share on other sites More sharing options...
PFMaBiSmAd Posted March 27, 2009 Share Posted March 27, 2009 Your page is probably being requested multiple times by your browser. What browser are you using and what is your form code - do you have any javascript code that submits the form, along with the browser's normal form submission? Quote Link to comment https://forums.phpfreaks.com/topic/151421-solved-mysqlphp-probs-single-insert-somehow-inserting-3-rows/#findComment-795377 Share on other sites More sharing options...
Brian W Posted March 27, 2009 Share Posted March 27, 2009 Try $q = "INSERT INTO survey_question_answers (sqaid, sqid, vc, txt) VALUES(NULL, '".substr($key,1,1)."', '', '$val') LIMIT 1"; I've seen phpMyAdmin use the LIMIT 1 before (i think) for inserts. IDK why it would be adding 3 Quote Link to comment https://forums.phpfreaks.com/topic/151421-solved-mysqlphp-probs-single-insert-somehow-inserting-3-rows/#findComment-795378 Share on other sites More sharing options...
mrMarcus Posted March 27, 2009 Share Posted March 27, 2009 always use LIMIT 1 when doing single queries for starters. Quote Link to comment https://forums.phpfreaks.com/topic/151421-solved-mysqlphp-probs-single-insert-somehow-inserting-3-rows/#findComment-795381 Share on other sites More sharing options...
PFMaBiSmAd Posted March 27, 2009 Share Posted March 27, 2009 LIMIT on an INSERT is invalid. Quote Link to comment https://forums.phpfreaks.com/topic/151421-solved-mysqlphp-probs-single-insert-somehow-inserting-3-rows/#findComment-795385 Share on other sites More sharing options...
Brian W Posted March 27, 2009 Share Posted March 27, 2009 meh, worth a shot. any better plans? Quote Link to comment https://forums.phpfreaks.com/topic/151421-solved-mysqlphp-probs-single-insert-somehow-inserting-3-rows/#findComment-795389 Share on other sites More sharing options...
PFMaBiSmAd Posted March 27, 2009 Share Posted March 27, 2009 I'm still kind of waiting to learn what browser and what the form code looks like, because FF will request a page twice under certain conditions and it sounds like some javascript validation code is probably submitting the form along with the normal browser form submission. It's also possible that url rewriting will cause the browser to request a page twice. Are you doing any url rewriting? Quote Link to comment https://forums.phpfreaks.com/topic/151421-solved-mysqlphp-probs-single-insert-somehow-inserting-3-rows/#findComment-795401 Share on other sites More sharing options...
thepip3r Posted March 27, 2009 Author Share Posted March 27, 2009 Sorry was afk for a bit... I'm using Chrome and no, i'm currently not using any javascript at all... Quote Link to comment https://forums.phpfreaks.com/topic/151421-solved-mysqlphp-probs-single-insert-somehow-inserting-3-rows/#findComment-795410 Share on other sites More sharing options...
thepip3r Posted March 27, 2009 Author Share Posted March 27, 2009 addon: i just tested with internet exploder and it's still adding 3 entries. Quote Link to comment https://forums.phpfreaks.com/topic/151421-solved-mysqlphp-probs-single-insert-somehow-inserting-3-rows/#findComment-795411 Share on other sites More sharing options...
PFMaBiSmAd Posted March 27, 2009 Share Posted March 27, 2009 Then it is something elsewhere in your code or you have rows leftover in the database from some previous testing. Quote Link to comment https://forums.phpfreaks.com/topic/151421-solved-mysqlphp-probs-single-insert-somehow-inserting-3-rows/#findComment-795414 Share on other sites More sharing options...
thepip3r Posted March 27, 2009 Author Share Posted March 27, 2009 well that's really the problem PFM -- i'm of the same mind except that little snippet of code is the ONLY place where I have insert statements and when I run through the complete web survey, the 4 radio buttosn that precede it enter fine, it's only when I tried to break out this html text field to write to a TEXT column when the problem started occurring so i know it has to do with this specific area of code. but like i said, i'm echoing out to see if the loop is done a bunch of times but it doesn't appear to be. check out the survey for yourself: http://www.zigsdigs.com/trigeo/survey/ Quote Link to comment https://forums.phpfreaks.com/topic/151421-solved-mysqlphp-probs-single-insert-somehow-inserting-3-rows/#findComment-795417 Share on other sites More sharing options...
xylex Posted March 27, 2009 Share Posted March 27, 2009 foreach ($_POST as $key=>$val) { if ($val !== "") { if ($key == "q1" || $key == "q2" || $key == "q3" || $key == "q4") { } elseif ($key == "q5") { } $r = mysql_query($q); } } You're running the query every time whether or not $key matches the if/elseif, so the post key=> value pairs for "submit" and "token" are causing a query to be run with the last set value of $q. Quote Link to comment https://forums.phpfreaks.com/topic/151421-solved-mysqlphp-probs-single-insert-somehow-inserting-3-rows/#findComment-795426 Share on other sites More sharing options...
PFMaBiSmAd Posted March 27, 2009 Share Posted March 27, 2009 Good job, xylex is correct. Your hidden field and submit button $_POST variables are causing two extra queries. Quote Link to comment https://forums.phpfreaks.com/topic/151421-solved-mysqlphp-probs-single-insert-somehow-inserting-3-rows/#findComment-795429 Share on other sites More sharing options...
thepip3r Posted March 27, 2009 Author Share Posted March 27, 2009 ah yes.... it's because i moved my $r = mysql_query($q); outside of my conditional and since the last insert is alwys the text field, it's running through submit and token. Thanx for the beautiful eyes xylex. Edit: thanx for both of your assistances guys! Quote Link to comment https://forums.phpfreaks.com/topic/151421-solved-mysqlphp-probs-single-insert-somehow-inserting-3-rows/#findComment-795430 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.