sabinmash Posted November 1, 2011 Share Posted November 1, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/250222-can-anyone-see-why-this-query-is-inserted-4-times/ Share on other sites More sharing options...
floridaflatlander Posted November 1, 2011 Share Posted November 1, 2011 Sometimes if you click refresh or the back button the code is reloaded and places another record in the table. Could that have happened? Quote Link to comment https://forums.phpfreaks.com/topic/250222-can-anyone-see-why-this-query-is-inserted-4-times/#findComment-1283929 Share on other sites More sharing options...
sabinmash Posted November 1, 2011 Author Share Posted November 1, 2011 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/250222-can-anyone-see-why-this-query-is-inserted-4-times/#findComment-1284070 Share on other sites More sharing options...
msaz87 Posted November 1, 2011 Share Posted November 1, 2011 How are you telling the script when to INSERT? From the looks of it, every time you visit this page it'll INSERT the session values, there's no specific trigger for it. Quote Link to comment https://forums.phpfreaks.com/topic/250222-can-anyone-see-why-this-query-is-inserted-4-times/#findComment-1284072 Share on other sites More sharing options...
sabinmash Posted November 1, 2011 Author Share Posted November 1, 2011 After hitting the confirm button of the page before, it transfers you to the page that runs this PHP script. Quote Link to comment https://forums.phpfreaks.com/topic/250222-can-anyone-see-why-this-query-is-inserted-4-times/#findComment-1284074 Share on other sites More sharing options...
PFMaBiSmAd Posted November 1, 2011 Share Posted November 1, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/250222-can-anyone-see-why-this-query-is-inserted-4-times/#findComment-1284076 Share on other sites More sharing options...
sabinmash Posted November 1, 2011 Author Share Posted November 1, 2011 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"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/250222-can-anyone-see-why-this-query-is-inserted-4-times/#findComment-1284104 Share on other sites More sharing options...
floridaflatlander Posted November 1, 2011 Share Posted November 1, 2011 If the data in the rows is unique, run a select query to see if it exist. If it does redirect or whatever you want to do. If it doesn't enter the data into the table. I usually run the select and then an if, soooooo if (mysqli_num_rows($r) == 0) {do this} else {do this} Quote Link to comment https://forums.phpfreaks.com/topic/250222-can-anyone-see-why-this-query-is-inserted-4-times/#findComment-1284113 Share on other sites More sharing options...
sabinmash Posted November 1, 2011 Author Share Posted November 1, 2011 Oh I see. I do have form validation that runs a select and checks. But once you do enter unique information, it enters multiple records. Quote Link to comment https://forums.phpfreaks.com/topic/250222-can-anyone-see-why-this-query-is-inserted-4-times/#findComment-1284115 Share on other sites More sharing options...
PFMaBiSmAd Posted November 2, 2011 Share Posted November 2, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/250222-can-anyone-see-why-this-query-is-inserted-4-times/#findComment-1284243 Share on other sites More sharing options...
sabinmash Posted November 2, 2011 Author Share Posted November 2, 2011 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"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/250222-can-anyone-see-why-this-query-is-inserted-4-times/#findComment-1284357 Share on other sites More sharing options...
sabinmash Posted November 3, 2011 Author Share Posted November 3, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/250222-can-anyone-see-why-this-query-is-inserted-4-times/#findComment-1284471 Share on other sites More sharing options...
sabinmash Posted November 3, 2011 Author Share Posted November 3, 2011 Silly me, the typos are now obvious. Seems to be fixed now, not sure why Chrome was inserting twice, separating processes into difference php files helps though. Quote Link to comment https://forums.phpfreaks.com/topic/250222-can-anyone-see-why-this-query-is-inserted-4-times/#findComment-1284520 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.