Update Multiple rows in a single form

PHP Result Center PHP Result Center เป็นหมวด ที่ไว้รวบรวม โปรแกรม Code php Javascript CSS CMS

Moderator: mindphp, ผู้ดูแลกระดาน

ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41356
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Update Multiple rows in a single form

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

Update Multiple rows in a single form

โค้ด: เลือกทั้งหมด

//get data fromdb
$sql = mysql_query("SELECT * FROM table");
$count=mysql_num_rows($sql);

//start a table
echo '<form name="form1" method="post" action="">
<table width="292" border="0" cellspacing="1" cellpadding="0">';

//start header of table
echo '<tr>
<td width="17" align="center">&nbsp;</td>
<td width="115" align="center"><strong>Name</strong></td>
<td width="149"><strong>Email</strong></td>
</tr>';

//loop through all results
while($r=mysql_fetch_object($sql)){
    
//print out table contents and add id into an array and email into an array
echo '<tr>
<td align="center"><input type="hidden" name="id[]" value='.$r->id.' readonly></td>
<td align="center">'.$r->name.'</td>
<td><input name="email[]" type="text" id="price" value="'.$r->email.'"></td>
</tr>';
}

//submit the form

echo'<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>';


//if form has been pressed proccess it

if($_POST["Submit"])
{
    //get data from form    
    $name = $_POST['name'];
    //loop through all array items 
    foreach($_POST['id'] as $value) 
    {
        //minus value by 1 since arrays start at 0
        $item = $value-1;
        //update table
        $sql1 = mysql_query("UPDATE table SET email='$email[$item]' WHERE id='$value'")or die(mysql_error());
    }
    
//redirect user 
$_SESSION['success'] = 'Updated';
header("location:index.php");
} 
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 73