简体   繁体   English

一次更新多行

[英]Update multiple rows at once

I have this code which permits me to display all the data in the database as a textarea, I need to update them by clicking a update button! 我有这段代码,可以将数据库中的所有数据显示为文本区域,我需要通过单击更新按钮来更新它们!

Based on this one, is supposed to make me edit them, but when i click submit it doesn't... 基于这一点,应该让我对其进行编辑,但是当我单击“提交”时,它不会...

<form name="form1" method="post" action="">
<tr>
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">

<tr>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Email</strong></td>
</tr>

<?php
    while($rows=mysql_fetch_array($result)){
?>

<tr>
    <td align="center">
        <? $id[]=$rows['id']; ?>
        <? echo $rows['id']; ?>
    </td>
    <td align="center">
        <input name="name[]" type="text" id="name" value="<? echo $rows['name']; ?>">
    </td>
    <td align="center">
        <input name="lastname[]" type="text" id="lastname" value="<? echo $rows['lastname']; ?>">
    </td>
    <td align="center">
        <input name="email[]" type="text" id="email" value="<? echo $rows['email']; ?>">
    </td>
</tr>

<?php
    }
?>

<tr>
    <td colspan="4" align="center">
        <input type="submit" name="Submit" value="Submit"/>
    </td>
</tr>
</table>
</td>
</tr>
</form>
</table>

<?php

// Check if button name "Submit" is active, do this
if($Submit)
{
    for($i=0;$i<$count;$i++)
    {
        $sql1="UPDATE $tbl_name SET name='$name[$i]', lastname='$lastname[$i]', email='$email[$i]' WHERE id='$id[$i]'";
        $result1=mysql_query($sql1);
    }
}

if($result1)
{
    header("location:update_multiple.php");
}

Yes because you forget your id's 是的,因为您忘记了自己的身份证

<? $id[]=$rows['id']; ?> <? $id[]=$rows['id']; ?> cannot be passed like that <? $id[]=$rows['id']; ?>不能那样传递

<input type="hidden" name="id[]" value ="<?php echo $rows['id']; ?>" /><? echo $rows['id']; ?>

and script if($Submit){ should be if($_POST['Submit'] != ''){ 和脚本if($Submit){应该是if($_POST['Submit'] != ''){

You aren't defining $Submit in your post, so the stuff in the { ... } is never executed. 您没有在帖子中定义$Submit ,所以{ ... }内容永远不会执行。

You should try something like this for your update: 您应该尝试使用以下方法进行更新:

if(isset($_POST[$name]))
{
// update stuff
}

In your code, as the if statement never executes, $result is never set, so the user isn't redirected away - it will just show the same page each time. 在您的代码中,由于永远不会执行if语句,因此不会设置$ result,因此不会重定向用户-每次都只会显示相同的页面。

To check if a post occur when clicking a button should be set as follow: 要检查单击按钮时是否发帖,应按以下步骤进行设置:

In the <form> tag add the following <form>标记中添加以下内容

<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 

And lastly, where you check if the button was clicked: 最后,检查按钮是否被单击:

if(isset($_POST['Submit'])) { 

//Update fields

}

Remember that the submit button name field is case sensitive in php 请记住,在php中,提交按钮名称字段区分大小写

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM