Jump to content

how to save different array


claro

Recommended Posts

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

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

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.

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.

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.