在单个窗体中更新多行
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在单个窗体中更新多行相关的知识,希望对你有一定的参考价值。
This snippet allows you to update multiple rows of a database using a single form in one easy click
//get data fromdb //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"> </td> <td width="115" align="center"><strong>Name</strong></td> <td width="149"><strong>Email</strong></td> </tr>'; //loop through all results //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'; }
以上是关于在单个窗体中更新多行的主要内容,如果未能解决你的问题,请参考以下文章