claro Posted September 24, 2011 Share Posted September 24, 2011 good day here! I'm new in php, and i want to save diff array at the same time. is it possible? if (isset($_POST['submit'])) { $user_Id = $_POST['user_Id']; $se_Id = $_POST['se_Id']; $status = $_POST['status']; foreach ($_POST['user_Id'] as $user_Id) <-- this is working, but I want to include the other array { mysql_query("INSERT tbl_data (user_Id,se_Id,status) VALUES ('$user_Id','$se_Id,'$status'')") or die(mysql_error()); } } help me guys. Link to comment https://forums.phpfreaks.com/topic/247774-how-to-save-different-array/ Share on other sites More sharing options...
jcbones Posted September 24, 2011 Share Posted September 24, 2011 What "other array"? Link to comment https://forums.phpfreaks.com/topic/247774-how-to-save-different-array/#findComment-1272334 Share on other sites More sharing options...
claro Posted September 24, 2011 Author Share Posted September 24, 2011 $user_Id = $_POST['user_Id']; $se_Id = $_POST['se_Id']; $status = $_POST['status']; these are all arrays. from my db. Link to comment https://forums.phpfreaks.com/topic/247774-how-to-save-different-array/#findComment-1272341 Share on other sites More sharing options...
AbraCadaver Posted September 24, 2011 Share Posted September 24, 2011 Assuming that you mean one of the other post vars is an array, then something like this if the arrays are guaranteed to be of the same length and they are matched: foreach ($_POST['user_Id'] as $key => $user_Id) { $se_Id = $_POST['se_Id'][$key]; $status = $_POST['status'][$key]; } You also need to run the vars through mysql_real_escape_string() and if they are of a specific type like integer then cast them to int using (int). Link to comment https://forums.phpfreaks.com/topic/247774-how-to-save-different-array/#findComment-1272342 Share on other sites More sharing options...
claro Posted September 24, 2011 Author Share Posted September 24, 2011 thank you for your quick reply. I think my question is not clear. Here is my code. while ($row = mysql_fetch_array($query)) { echo "<tr>"; echo '<td><input type = "hidden" name = "se_Id[]" value = '.$row['se_Id'].'></td>'; echo '<td><input type = "hidden" name = "user_Id[]" value = '.$row['user_Id'].'>'.$row['fullname'].'</td>'; echo "<td><input type='checkbox' id='checkbox".$i."' name ='status[]' value='Cleared' onclick='checkbox_disabled(\"checkbox".$i."\",\"textbox".$i."\",\"checkbox".$i."2\");'></td>"; //echo "<td><input type='checkbox' id='checkbox".$i."2'></td>"; echo "<td><input type='text' id='textbox".$i."' name = 'status[]'></td>"; echo "</tr><br>"; $i++; } echo "<td></td><td><input type='submit' name='submit' value='Submit'></td>"; echo "</table>"; echo "</form>"; if (isset($_POST['submit'])) { foreach ($_POST['user_Id'] as $key => $user_Id) { $se_Id = $_POST['se_Id'][$key]; $status = $_POST['status'][$key]; mysql_query("INSERT tbl_data (user_Id,se_Id,Status) VALUES ('$user_Id','$se_Id','$status')") or die(mysql_error()); } I want those array to save at the same time. I tried your codes but the values are repeating. Link to comment https://forums.phpfreaks.com/topic/247774-how-to-save-different-array/#findComment-1272355 Share on other sites More sharing options...
claro Posted September 24, 2011 Author Share Posted September 24, 2011 Im dumb! sorry !! the code is running already, i just did'nt notice in my db. thank you thank you! Link to comment https://forums.phpfreaks.com/topic/247774-how-to-save-different-array/#findComment-1272356 Share on other sites More sharing options...
AbraCadaver Posted September 24, 2011 Share Posted September 24, 2011 You will have problems unless you make the array keys the same because the checkbox won't be submitted unless it is checked. So you will have them matched up with the wrong ids. Maybe like this: <input type="hidden" name="se_Id['.$i.']" <input type="hidden" name="user_Id['.$i.']" <input type="checkbox" name="status['.$i.']" Also, you should be able to loop and build one INSERT query and then execute just the one query. And use MYSQL_REAL_ESCAPE_STRING() on the post data or you will be hacked. Link to comment https://forums.phpfreaks.com/topic/247774-how-to-save-different-array/#findComment-1272383 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.