pam_w Posted March 23, 2008 Share Posted March 23, 2008 Hi, I am having trouble inserting multiple records into a table. This code below works, there is only one issue when the records are inserted they are duplicated. I'm sure this is something really simple. I know the issue is with the second section of code when I remove this only one of each recorded is entered. for($i=1; $i <= $_POST["totalrows"]; $i++) { if(!isset($_POST["active$i"])) $_POST["active$i"] = 0; $insertSQL = sprintf("INSERT INTO Project_Staff_Access (Project_id, Staff_id, Access_id) VALUES (%s,%s,%s)", GetSQLValueString($_POST["project$i"], "int"), GetSQLValueString($_POST["staff$i"], "int"), GetSQLValueString($_POST["access$i"], "int")); mysql_select_db($database_Project_Application, $Project_Application); $Result1 = mysql_query($insertSQL, $testconnect) or die(mysql_error()); [b]$insertGoTo = "project_app_complete.php"; if (isset($_SERVER['QUERY_STRING'])) { $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; $insertGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $insertGoTo));[/b] } } Thanks for any help, Pam Quote Link to comment https://forums.phpfreaks.com/topic/97497-inset-multiple-records-duplication-issue/ Share on other sites More sharing options...
sqlnoob Posted March 23, 2008 Share Posted March 23, 2008 I don't see your problem ??? you do this: $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; $insertGoTo .= $_SERVER['QUERY_STRING']; do you want to do this? $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?" . $_SERVER['QUERY_STRING']; I'm not sure what you're trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/97497-inset-multiple-records-duplication-issue/#findComment-498870 Share on other sites More sharing options...
sqlnoob Posted March 23, 2008 Share Posted March 23, 2008 do you want to concatenate? http://www.w3schools.com/php/php_string.asp is the first example there what you're trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/97497-inset-multiple-records-duplication-issue/#findComment-498872 Share on other sites More sharing options...
pam_w Posted March 23, 2008 Author Share Posted March 23, 2008 I'm not too sure, I have a form which will add 8 new users to the database. If I remove the second part of the code and complete the form there is no problem, 8 new records are added. With the second part of the code each new record is added twice. The second part of the code is to take the user to a different page. Pam Quote Link to comment https://forums.phpfreaks.com/topic/97497-inset-multiple-records-duplication-issue/#findComment-498874 Share on other sites More sharing options...
sqlnoob Posted March 23, 2008 Share Posted March 23, 2008 ok so you only want it to insert 8 records, 1 for each player instead of 16 (2 for each player) and also echo a link to another page. Am I right about that assumption? Quote Link to comment https://forums.phpfreaks.com/topic/97497-inset-multiple-records-duplication-issue/#findComment-498877 Share on other sites More sharing options...
sqlnoob Posted March 23, 2008 Share Posted March 23, 2008 ok so it's this bit that's causing the trouble for you right? $insertGoTo .= $_SERVER['QUERY_STRING']; what would happen if you defined it as a new variable and gave it another name and then place it after the } of the if part, but before this header(sprintf("Location: %s", $insertGoTo));[/b] would that work for you? Quote Link to comment https://forums.phpfreaks.com/topic/97497-inset-multiple-records-duplication-issue/#findComment-498880 Share on other sites More sharing options...
pam_w Posted March 23, 2008 Author Share Posted March 23, 2008 It's all of this part: $insertGoTo = "project_app_complete.php"; if (isset($_SERVER['QUERY_STRING'])) { $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; $insertGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $insertGoTo)); If I take this out then the insert works fine, I am guessing it's part of this statement which is causing the duplication. I took this out of another page in my site. Did you mean rename $insertGoTo? Pam Quote Link to comment https://forums.phpfreaks.com/topic/97497-inset-multiple-records-duplication-issue/#findComment-498883 Share on other sites More sharing options...
sqlnoob Posted March 23, 2008 Share Posted March 23, 2008 $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; $insertGoTo .= $_SERVER['QUERY_STRING']; from w3school: Definition and Usage The stripos() function returns the position of the first occurrence of a string inside another string. If the string is not found, this function returns FALSE. so you're using the first line to see if there is a string am I right? and you use the second line to send a header so that the user goes there, am I right? so why not seperate the second line from the if construction and rename the variable like $thisinsertGoTo = $insertGoTo . $_SERVER['QUERY_STRING']; and then header(sprintf("Location: %s", $thisinsertGoTo)); I'm too much of a noob to know wether this is valid or not Quote Link to comment https://forums.phpfreaks.com/topic/97497-inset-multiple-records-duplication-issue/#findComment-498884 Share on other sites More sharing options...
sqlnoob Posted March 23, 2008 Share Posted March 23, 2008 would this work for you? $insertGoTo = "project_app_complete.php"; if (isset($_SERVER['QUERY_STRING'])) { $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; $thisinsertGoTo = $insertGoTo . $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $thisinsertGoTo)); Quote Link to comment https://forums.phpfreaks.com/topic/97497-inset-multiple-records-duplication-issue/#findComment-498885 Share on other sites More sharing options...
pam_w Posted March 23, 2008 Author Share Posted March 23, 2008 Hi, Thanks that worked! I've been using Dreamweaver so haven't really done much coding... yet! Pam Quote Link to comment https://forums.phpfreaks.com/topic/97497-inset-multiple-records-duplication-issue/#findComment-498998 Share on other sites More sharing options...
sqlnoob Posted March 23, 2008 Share Posted March 23, 2008 ok good to hear good luck with your dissertation Quote Link to comment https://forums.phpfreaks.com/topic/97497-inset-multiple-records-duplication-issue/#findComment-499016 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.