Page 1 of 1

problems with inserting multiple rows with an array

Posted: Fri Dec 05, 2003 9:24 pm
by gjb79
This was working, but I'm not sure what happened as it is no longer working.

It should build an array from a check box form element, then insert it into a mysql database table. It should insert 1 column that is a consistant variable, and a second column should contain a unique variable that is in the array.

Currently it is only inserting the last item in the array and ignoring the first two.

my insert statement

Code: Select all

if ((isset($HTTP_POST_VARS["MM_insert"])) && ($HTTP_POST_VARS["MM_insert"] == "addfiles")) {
  foreach ($selectedfile as $tempvalue) { 
  $insertSQL = sprintf("INSERT INTO filetopage (page, file) VALUES ($selectedpageid, $tempvalue)");
  }

  mysql_select_db($database_cms, $cms);
  $Result1 = mysql_query($insertSQL, $cms) or die(mysql_error());
}


my form code

Code: Select all

<input name='selectedfile[]' type='checkbox' value='" . $test_value . "'>";


the selectedfile[] array should contain a variety of variables.
the selectedpageid would be a constant, non changing, variable once the page is loaded, we'll say it = "1" for now, though it could also be 2, 3, or 4.

Any ideas on how I can get it to insert multiple rows into the table?

Thanks!

Posted: Fri Dec 05, 2003 10:04 pm
by infolock
#1, you are defining every checkbox as the exact same name... so no matter which one they choose, the end up $_POST name is going to be the last file they choose...

SO, here is a workaround :

#1 Declare $test_value as an array such as this :

$test_value=array('Test1','Test2','Test3');

#2 Declare $selected file as an array, and then loop it like this on your Form Code :

Code: Select all

$test_value=array('My File 1','My File 2','My File 3');
$selectedfile=array();
echo '
<html>
<body>
	<form action="go.php" method="post">';
foreach($test_value as $test)
{
		echo '<input name='.$selectedfile[].' type="checkbox" value="'.$test.'">';
}
echo '<input type="Submit" value="Go"></form></body></html>';
and then for your My_Insert_Statement php file, do this :

Code: Select all

include("formpage.php");  // include the form so you can access the variables
mysql_select_db($database_cms, $cms);
if ((isset($HTTP_POST_VARS["MM_insert"])) && ($HTTP_POST_VARS["MM_insert"] == "addfiles")) { 
  foreach ($selectedfile as $tempvalue) { 
  $insertSQL = "INSERT INTO filetopage (page, file) VALUES ('".$selectedpageid."', '".$tempvalue."'"; 
  $Result1 = mysql_query($insertSQL) or die(mysql_error()); 
  } 
}

Posted: Sat Dec 06, 2003 8:20 am
by gjb79
Should have been more specific. the input value was an array and it did have a loop on it so that each instance of the input values had a unique value from the array.

Thanks for your help!

Posted: Sat Dec 06, 2003 8:30 am
by Weirdan
@infolock
[php_man]variables.external[/php_man]
PHP manual wrote: PHP also understands arrays in the context of form variables (see the related faq). You may, for example, group related variables together, or use this feature to retrieve values from a multiple select input. For example, let's post a form to itself and upon submission display the data: