0

I have my table named i_table with columns:

id name req_qty req_date rcv_qty rec_date
1 metal 1 2014年03月04日
2 spring 5 2014年03月04日

in my html/php:

<form action="insert.php" method="post">
<?php $resource2=mysql_query("SELECT * FROM i_table",$con);
 while($result2=mysql_fetch_array($resource2))
 { 
?>
<table>
<tr>
<td>
<input type="text" name="id" value="<?php echo $result2['id'];?>"/>
</td>
</tr>
<tr>
<td>
<input type="text" name="name" value="<?php echo $result2['name'];?>"/>
</td>
</tr>
<tr>
<td>
<input type="text" name="rcv[]" value="" />
</td>
</tr>
<tr>
<td>
<input type="date" name="rcv_date[]" value="" />
</td>
</tr>
</table>
<?php };?>
</form>

how can I insert in multiple arrays from an input according to their unique ID from db? pls help me here... huhu

asked Mar 11, 2014 at 15:11
3
  • Add the id as the key -> name="id[<?php echo $result2['id'];?>]",name="name[<?php echo $result2['id'];?>]", name="rcv[<?php echo $result2['id'];?>]",name="rcv_date[<?php echo $result2['id'];?>]". Then on post, using foreach() you can get the key id. Commented Mar 11, 2014 at 15:18
  • how can i do that? how to for each? please help... still learning php Commented Mar 11, 2014 at 15:23
  • Can u do a sample for for each sir? So I can follow? pls? Commented Mar 11, 2014 at 15:25

1 Answer 1

1

In your form, add the id as the key -

<form action="insert.php" method="post">
<?php $resource2=mysql_query("SELECT * FROM i_table",$con);
 while($result2=mysql_fetch_array($resource2))
 { 
?>
<table>
<tr>
<td>
<input type="text" name="id[<?php echo $result2['id'];?>]" value="<?php echo $result2['id'];?>"/>
</td>
</tr>
<tr>
<td>
<input type="text" name="name[<?php echo $result2['id'];?>]" value="<?php echo $result2['name'];?>"/>
</td>
</tr>
<tr>
<td>
<input type="text" name="rcv[<?php echo $result2['id'];?>]" value="" />
</td>
</tr>
<tr>
<td>
<input type="date" name="rcv_date[<?php echo $result2['id'];?>]" value="" />
</td>
</tr>
</table>
<?php };?>
</form>

Then on your insert.php where you post your form, get the id in your foreach -

<?php
 if(isset($_POST['id'])){
 foreach($_POST['id'] as $id=>$value){
 $sql = "UPDATE `i_table` SET `name` = '".$_POST['name'][$id]."', `rcv_qty` = '".$_POST['rcv'][$id]."', `rec_date` = '".$_POST['name'][$id]."' WHERE `id` = ".$id."";
 mysql_query($sql,$con);
 }
 }
?>

note the foreach() is just an example. You will want to sanitize your data before inserting/updating to prevent sql injection. see How can I prevent SQL injection in PHP?

answered Mar 11, 2014 at 15:47
Sign up to request clarification or add additional context in comments.

2 Comments

where do i get the variable $value? where did this came from?
$value is just user defined variable inside a foreach() loop (it could also be $v, $x, $val, etc). It might be helpful for you to read the php manual foreach (array_expression as $key => $value) - php.net/manual/en/control-structures.foreach.php. I did not use it in my example, but it had to be there so I could access the $key, or it would produce a syntax error. Typically you would access/use the $value value.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.