Jump to content

Inset multiple records - duplication issue


pam_w

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

   $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

 

 

Link to comment
Share on other sites

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

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.