Julian Posted June 9, 2009 Share Posted June 9, 2009 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 More sharing options...
Julian Posted June 9, 2009 Author Share Posted June 9, 2009 Anyone? Link to comment https://forums.phpfreaks.com/topic/161584-loop-insert/#findComment-852694 Share on other sites More sharing options...
ted_chou12 Posted June 9, 2009 Share Posted June 9, 2009 $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 More sharing options...
Julian Posted June 9, 2009 Author Share Posted June 9, 2009 Thanks. Without the implode I get "NULL" on the insert. Any other ideas? The query is working, but I want to insert a new row for each int on the arrray, now it just insert the first selected box. Regards Link to comment https://forums.phpfreaks.com/topic/161584-loop-insert/#findComment-852720 Share on other sites More sharing options...
ted_chou12 Posted June 9, 2009 Share Posted June 9, 2009 $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 More sharing options...
Julian Posted June 9, 2009 Author Share Posted June 9, 2009 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 More sharing options...
ted_chou12 Posted June 9, 2009 Share Posted June 9, 2009 $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 More sharing options...
Julian Posted June 9, 2009 Author Share Posted June 9, 2009 Thanks again. Change the code and obtained the same result. Only 1 row inserted, with this code version instead of inserting the first checked box got the last checked box. . Basically is that the foreach is not making the query loop.... Link to comment https://forums.phpfreaks.com/topic/161584-loop-insert/#findComment-852747 Share on other sites More sharing options...
ted_chou12 Posted June 9, 2009 Share Posted June 9, 2009 hi, can you do this for me, because i am not sure what exactly is inside the $arr, $arr = $_POST['id_especialidad']; foreach ($arr as $a) {echo "'$a'";} and paste the result here Ted Link to comment https://forums.phpfreaks.com/topic/161584-loop-insert/#findComment-852759 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.