Jump to content

Loop Insert


Julian

Recommended Posts

Hello guys!

 

I want to insert a new row for each check box on mysql table.  But for some reason this script stores only the first item in the array.  Help will be much appreciated.

$especialidades = mysql_real_escape_string(implode(',', $_POST['id_especialidad']));	

$arr = array($especialidades);

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {

	foreach ($arr as $check) {
		//echo $check;
		$insertSQL = sprintf("INSERT INTO perfil_especialidad (id_perfil, id_categoria, id_subcategoria, id_especialidad) VALUES (%s, %s, %s, %s)", 
				   GetSQLValueString($_GET['id_perfil'], "int"),
				   GetSQLValueString($_GET['id_categoria'], "int"),
				   GetSQLValueString($_GET['id_subcategoria'], "int"),
				   GetSQLValueString($check, "int"));
	}

  mysql_select_db($database_red, $red);
  $Result1 = mysql_query($insertSQL, $red) or die(mysql_error());

 

Link to comment
https://forums.phpfreaks.com/topic/161584-loop-insert/
Share on other sites

$especialidades = mysql_real_escape_string($_POST['id_especialidad']));	

$arr = array($especialidades);

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {

	foreach ($arr as $check) {
		//echo $check;
		$insertSQL = sprintf("INSERT INTO perfil_especialidad (id_perfil, id_categoria, id_subcategoria, id_especialidad) VALUES (%s, %s, %s, %s)", 
				   GetSQLValueString($_GET['id_perfil'], "int"),
				   GetSQLValueString($_GET['id_categoria'], "int"),
				   GetSQLValueString($_GET['id_subcategoria'], "int"),
				   GetSQLValueString($check, "int"));
	}

  mysql_select_db($database_red, $red);
  $Result1 = mysql_query($insertSQL, $red) or die(mysql_error());

try this, i removed the implode() because that makes the array to a string.

Ted

Link to comment
https://forums.phpfreaks.com/topic/161584-loop-insert/#findComment-852708
Share on other sites

	

$arr = $_POST['id_especialidad'];

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {

	foreach ($arr as $check) {
		//echo $check;
		$insertSQL = sprintf("INSERT INTO perfil_especialidad (id_perfil, id_categoria, id_subcategoria, id_especialidad) VALUES (%s, %s, %s, %s)", 
				   GetSQLValueString($_GET['id_perfil'], "int"),
				   GetSQLValueString($_GET['id_categoria'], "int"),
				   GetSQLValueString($_GET['id_subcategoria'], "int"),
				   GetSQLValueString($check, "int"));
	}

  mysql_select_db($database_red, $red);
  $Result1 = mysql_query($insertSQL, $red) or die(mysql_error());

sorry, my bad, i forgot to remove the extra (, but i corrected it. The only thing i am not sure is the  $_POST['id_especialidad'], is it an array itself? can you post just this part of the form?

Thanks,

Link to comment
https://forums.phpfreaks.com/topic/161584-loop-insert/#findComment-852728
Share on other sites

The checkboxes are obtained dynamically from a table called 'especialidad' as follows

 

<?php
$new_espe = $_GET['id_subcategoria'];				  
$query_especialidades = sprintf("SELECT * FROM especialidad WHERE id_subcategoria = '%s' ORDER BY especialidad ASC", $new_espe);
$especialidades = mysql_query($query_especialidades, $red) or die(mysql_error());

echo '<table width="100%" border="0" cellspacing="0" cellpadding="2">';
	$count_ser = 0;
	while (list($id_especialidad, , $especialidad) = mysql_fetch_row($especialidades)) {
    		if ($count_ser % 4 == 0) echo '<tr>';
    			echo "<td width='18' valign='top'><input type='checkbox' id='espe' name='id_especialidad[]' value='$id_especialidad' /></td>";
			echo "<td class='class10' valign='top'>$especialidad</td>";
    			$count_ser++;
    				if ($count_ser % 4 == 0) echo '</tr>';
				}
					if ($count_ser % 4 != 0) echo '</tr>';
						echo '</table>';

?>

Link to comment
https://forums.phpfreaks.com/topic/161584-loop-insert/#findComment-852731
Share on other sites

$arr = $_POST['id_especialidad'];
mysql_select_db($database_red, $red);
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {

foreach ($arr as $check) {
//echo $check;
$insertSQL = sprintf("INSERT INTO perfil_especialidad (id_perfil, id_categoria, id_subcategoria, id_especialidad) VALUES ({$_GET['id_perfil']}, {$_GET['id_categoria']}, {$_GET['id_subcategoria']}, $check)");
$Result1 = mysql_query($insertSQL, $red) or die(mysql_error());}

Ted

 

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/161584-loop-insert/#findComment-852738
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.