Jump to content

Can anyone see why this query is inserted 4 times?


sabinmash

Recommended Posts

I only see one "mysql_query($memberquery);"  Yet the record is inserted 4 times when I check the db.  Anyone see why?

 

Thank you for your time!

 

<?php 
$link_register = 1;
session_start(user);
include("header.php"); 



$firstname     	= $_SESSION['firstname'];
$middleinitial  = $_SESSION['middleinitial'];
$lastname     	= $_SESSION['lastname'];
$phone     		= $_SESSION['phone'];
$email     		= $_SESSION['email'];
$street     	= $_SESSION['street'];
$city     		= $_SESSION['city'];
$state     		= $_SESSION['state'];
$categoryArray  = $_SESSION['categoryArray'];
$gender     	= $_SESSION['gender'];


$con = mysql_connect("localhost", "root", "");

if (!$con)
	{
	die("Could not connect: ". mysql_error());
	}

//The database is selected with the mysql_select_db() function.	
mysql_select_db("hobbyclub", $con);

//put the data insert into a varaible so it can be checked later
$memberquery = "INSERT INTO member (firstname, middleinitial, lastname, phone, email, street, city, state, gender) 
		VALUES ('$firstname', '$middleinitial', '$lastname', '$phone', '$email', '$street', '$city', '$state', '$gender')";

// if ($categoryArray){
				 // foreach ($categoryArray as $category){
					// $categoryString .= "'$".$category."',";
				// }
// }		
// $categoryquery = "INSERT INTO category (categoryname) VALUES('$categoryString')";	
// $categoryquery = "INSERT INTO category (categoryname) VALUES(".$categoryString.")";	


//insert the data
mysql_query($memberquery);


?>

 

The commented out block of code is just me trying to figure out how to insert an array into the db.

Link to comment
Share on other sites

I dont think so, this happens consistently. 

 

Actually the code here doesn't include some html.  The html has some php injected into it - with that html commented out, and only this php running it still inserts twice.  It's as is the sql insert is written in a way that doubles the insert.  Is that the case? I don't see anything wrong with it.

 

<?php 
$link_register = 1;
session_start(user);
include("header.php"); 



$firstname     	= $_SESSION['firstname'];
$middleinitial  = $_SESSION['middleinitial'];
$lastname     	= $_SESSION['lastname'];
$phone     		= $_SESSION['phone'];
$email     		= $_SESSION['email'];
$street     	= $_SESSION['street'];
$city     		= $_SESSION['city'];
$state     		= $_SESSION['state'];
$categoryArray  = $_SESSION['categoryArray'];
$gender     	= $_SESSION['gender'];


$con = mysql_connect("localhost", "root", "");

if (!$con)
	{
	die("Could not connect: ". mysql_error());
	}

//The database is selected with the mysql_select_db() function.	
mysql_select_db("hobbyclub", $con);

//put the data insert into a varaible so it can be checked later
$memberquery = "INSERT INTO member (firstname, middleinitial, lastname, phone, email, street, city, state, gender) 
		VALUES ('$firstname', '$middleinitial', '$lastname', '$phone', '$email', '$street', '$city', '$state', '$gender')";

// if ($categoryArray){
				 // foreach ($categoryArray as $category){
					// $categoryString .= "'$".$category."',";
				// }
// }		
// $categoryquery = "INSERT INTO category (categoryname) VALUES('$categoryString')";	
// $categoryquery = "INSERT INTO category (categoryname) VALUES(".$categoryString.")";	


//insert the data
mysql_query($memberquery);


?>

Link to comment
Share on other sites

Browsers can request a page twice (FF does this when firebug add on is running and when the default page encoding doesn't match the actual web page, IE does this in relation to getting the favicon.ico file) and some web hosting behind a proxy can request a page twice and some URL rewriting can cause a page to be requested twice along with some javascript in your form.

 

How come your current code is NOT testing if a form was submitted before using the data, like the code that was given in one of your previous threads?

 

Edit: It's likely that the confirm button code is causing the page to be requested/submitted to twice. How about posting that relevant code as well?

Link to comment
Share on other sites

Oh, sorry!  Here is the full thing:

 

<?php 
$link_register = 1;
session_start(user);

include("header.php"); 

$firstname     	= $_SESSION['firstname'];
$middleinitial  = $_SESSION['middleinitial'];
$lastname     	= $_SESSION['lastname'];
$phone     		= $_SESSION['phone'];
$email     		= $_SESSION['email'];
$street     	= $_SESSION['street'];
$city     		= $_SESSION['city'];
$state     		= $_SESSION['state'];
$categoryArray  = $_SESSION['categoryArray'];
$gender     	= $_SESSION['gender'];


?>

   <div id="banner-wrap">
      <div id="banner" class="container_24 rounded">
         <h2><?PHP echo 'Hello '.$firstname.'!'; ?></h2>
         <div id="featured">
            <img src="img/featuredImage1.jpg" alt="Featured" />
            <img src="img/featuredImage2.jpg" alt="Featured" />
         </div>

         <p> If the following information is correct, please click the confirm button.<br />
	 </p>
         
<form class="confirmation" action="thanks.php" method="POST">
  <table border="0" cellspacing="0">
         <tr>
            <td>Name:</td>
		<td><?PHP echo $firstname.' '.$middleinitial.' '.$lastname.'<br /><br />' ?></td>
         </tr>
         <tr>
            <td>Telephone:  </td>
		<td><?PHP echo $phone.'<br /><br />' ?></td>
		</tr>
         <tr>
            <td>Email:  </td>
		<td><?PHP echo $email.'<br /><br />' ?></td>
         </tr>
         <tr>
            <td>Address:  </td>
		<td><?PHP echo $street.', '.$city.', '.$state.'<br /><br />' ?></td>
		</tr>		 
	 <tr>
		<td>Category</td>
		<td>
			 <!-- display the category array.   Categories comes from a mulitple select tag in the 
				registration form, it wors by getting put into an array rather than a normal var.  
				to see them you musy loop through that array with a foreach.
				Here, we take the session variable from the register page and put its value into 
				a normal variable we can cycle through, then if that variable has something in it, 
				we loop through it with the foreach-->
			<?PHP $categoryArray = $_SESSION['categoryArray'];

				if ($categoryArray){
				 foreach ($categoryArray as $cat){
					echo $cat.'<br />';}
				} 
				echo '<br />'?>
		</tr>
         <tr>
            <td>Gender:  </td>
		<td><?PHP echo $gender ?></td>
         </tr>
	</table>

 <button type="submit" value="Confirm" name="submit"/>	CONFIRM </button>
</form>      
</div>

      <p id="breadcrumbs" class="container_24 rounded"> You're here: <a href="#">Home</a> > <a href="#">Welcome</a> </p>
   </div> <!-- end banner-wrap -->


<?php include("footer.php"); ?>

Link to comment
Share on other sites

So, the code you posted that has the INSERT query is the thanks.php page?

 

Did you ever determine why the data was being inserted 4 times (the things I mentioned generally only cause data to be submitted twice)? What that just due to testing your page two times, then looking and seeing the data four times?

Link to comment
Share on other sites

Got FF and tried it.  It was chrome.  The sql inserts one into the database with FF, and 2 with Chrome.  And FOUR in chrome if I keep this code:

 

<?php 
	 	//if the query did not work, display error, otherwise display msg.
		if (!mysql_query($memberquery, $con)){
			die('Error: ' . mysql_error());

			echo '<h2>Sorry, there was a problem processing your registration, 
				hit the back button and try again!</h2>';

		//if the query worked, display success message and a link to see the roster.
		} else {
			echo '<h2>Thanks '.$_SESSION['firstname'].'! You\'ve been registered!</h2>';
			echo '<p> Now that you\'ve registered, meet the rest of the team.  Click this
				<a href="roster.php"><b>roster</b></a> link to see your new club mates.</p>';
			mysql_query($test);
			}			
		//close connection
		if (!$con)
			{
			mysql_close($con);
			}
			?>

 

Also, in my last post i gave "the full code", it was hte wrong page, sorry for hte confusion.  This is hte right page below.  Works in FF for the original sql insert.  Now I want a 2nd insert for an associative table, called member-category.  I am making an array into a string  with the vales separated by commas, and removing the final comma.  I am trying to stick it into another sql insert.  Nothing gets inserted  into the member-category table though.

 

<?php 
$link_register = 1;
session_start(user);
include("header.php"); 

$firstname     	= $_SESSION['firstname'];
$middleinitial  = $_SESSION['middleinitial'];
$lastname     	= $_SESSION['lastname'];
$phone     		= $_SESSION['phone'];
$email     		= $_SESSION['email'];
$street     	= $_SESSION['street'];
$city     		= $_SESSION['city'];
$state     		= $_SESSION['state'];
$categoryArray  = $_SESSION['categoryArray'];
$gender     	= $_SESSION['gender'];


$con = mysql_connect("localhost", "root", "");

if (!$con)
	{
	die("Could not connect: ". mysql_error());
	}

//The database is selected with the mysql_select_db() function.	
mysql_select_db("hobbyclub", $con);

//put the data insert into a varaible so it can be checked later
$memberquery = "INSERT INTO member 
					(firstname, 
					middleinitial, 
					lastname, 
					phone, 
					email, 
					street, 
					city, 
					state, 
					gender) 
				VALUES 
					('$firstname', 
					'$middleinitial', 
					'$lastname', 
					'$phone', 
					'$email', 
					'$street', 
					'$city', 
					'$state', 
					'$gender')";

//echo "names".$categoryArray[0].' '.$categoryArray[1].' '.$categoryArray[2].' '.$categoryArray[3];	

	 if ($categoryArray){
		foreach ($categoryArray as $category){
			$categoryString .= "'$".$category."',";
		}
	}

//redefine named arrays as their number, so they can go into member-category table
if ($categoryArray[0]){
	$categoryArray[0] = 0;
}
if ($categoryArray[1]){
	$categoryArray[1] = 1;
}
if ($categoryArray[2]){
	$categoryArray[2] = 2;
}
if ($categoryArray[3]){

	$categoryArray[3] = 3;
}

//select memberid from db, put it in var, 
$querymemberid = mysql_query("SELECT * FROM  member WHERE email='$email'");

//then put that + all the category array bacj into member-categeroy table
$member_id = mysql_num_rows($querymemberid);

						//concatonate array vars
						foreach ($categoryArray as $category){
							$category_id .= "'".$category."',";	
						}

echo "member id: ".$member_id ;	

//varaible with string of arrays now inside have the last comma stripped off.
$category_id = rtrim($category_id, ",")	;
echo $category_id;

$membercategoryquery = "INSERT INTO member-category
						(member_id, 
						category_id) 
					VALUES 
						('$member_id', 
						'$category_id')";

$test = "INSERT INTO member-category
						(member_id, 
						category_id) 
					VALUES 
						('1', 
						'2')";							


//insert the data
//mysql_query($memberquery);
mysql_query($membercategoryquery);
mysql_query($test);
?>

   <div id="banner-wrap">
      <div id="banner" class="container_24 rounded">
         <?php 
	 	//if the query did not work, display error, otherwise display msg.
		if (!mysql_query($memberquery, $con)){
			die('Error: ' . mysql_error());

			echo '<h2>Sorry, there was a problem processing your registration, 
				hit the back button and try again!</h2>';

		//if the query worked, display success message and a link to see the roster.
		} else {
			echo '<h2>Thanks '.$_SESSION['firstname'].'! You\'ve been registered!</h2>';
			echo '<p> Now that you\'ve registered, meet the rest of the team.  Click this
				<a href="roster.php"><b>roster</b></a> link to see your new club mates.</p>';
			mysql_query($test);
			}			
		//close connection
		if (!$con)
			{
			mysql_close($con);
			}
			?>
      </div>

     
   </div> <!-- end banner-wrap -->


<?php include("footer.php"); 
?>

Link to comment
Share on other sites

OK, I fixed it.  I took the php insert code and separated them onto their own insertmember.php, insertcategory.php processing page, that redirect to one another.

 

It's almost working fine, but I still cant for the life of me get this one table to update with a loop.

 

for ($i=0; 1<categoryArray; $i++){
	$membercategoryquery = "INSERT INTO `hobbyclub`.`member-category` (
							`ID` ,
							`member_id` ,
							`category_id`
							)
						VALUES (
							NULL , '$member_id[0]', '$categoryArray[i]'
							)";	
	mysql_query($membercategoryquery);						
}		

 

Any suggestions for this?  It looks right to me but nothing happens.

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.