Jump to content

[SOLVED] MySQL/PHP Probs -- single insert somehow inserting 3 rows!


Recommended Posts

		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')

 

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?

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

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?

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/

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.

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!

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.