使用复选框更新mysql表

时间:2010-09-06 21:31:39

标签: php mysql database forms

我尝试使用多个复选框更新mysql表,但我的代码似乎不起作用,所以欢迎任何帮助。我的代码是:

<strong>Update <strong class="highlight">multiple</strong> <strong class="highlight">rows</strong> <strong class="highlight">in</strong> <strong class="highlight">mysql</strong></strong><br> 

<?php 
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="root"; // Mysql password 
$db_name="db_test"; // Database name 
$tbl_name="test_table"; // Table name

// Connect to server and select databse. 
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB"); 

$sql="SELECT * FROM $tbl_name"; 
$result=mysql_query($sql); 

// Count table <strong class="highlight">rows</strong> 
$count=mysql_num_rows($result); 
?> 
<table width="500" border="0" cellspacing="1" cellpadding="0"> 
<form name="form1" method="post" action="<? echo $_SERVER['REQUEST_URI']; ?>"> 
<tr> 
<td> 
<table width="500" border="0" cellspacing="1" cellpadding="0"> 


<tr> 
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Description</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Phone Number</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Operation</strong></td>
</tr> 
<?php 
while($rows=mysql_fetch_array($result))
{ 
?> 
<tr> 
<td align="center"><input type="hidden" name="id[]" value="<? echo $rows['id']; ?>" /><? echo $rows['id']; ?></td> 
<td align="center"><input name="name<? echo $rows['id']; ?>" type="text" id="name" value="<? echo $rows['name']; ?>"></td> 
<td align="center"><input name="email<? echo $rows['id']; ?>" type="text" id="email" value="<? echo $rows['email']; ?>"></td> 
<td align="center"><input name="description<? echo $rows['id']; ?>" type="text" id="description" value="<? echo $rows['description']; ?>"></td> 
<td align="center"><input name="phone_number<? echo $rows['id']; ?>" type="text" id="phone_number" value="<? echo $rows['phone_number']; ?>"></td>
<td align="center"><input name="operation<? echo $rows['id']; ?>" type="text" id="operation" value="<? echo $rows['operation']; ?>"></td>
<td align="center"><input name="ONOFF<? echo $rows['id']; ?>" type="checkbox" id="ONOFF" value="1" 
<?php if ($rows['ONOFF'] ==1) { echo "checked";} else {} ?> 
</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)
{ 
 foreach($_POST['id'] as $id)
 { 
  $onoff = 0;
    if (isset($_POST["ONOFF".$id]))
    {
        $onoff = 1;
    }

  $sql1="UPDATE ".$tbl_name." SET name='".$_POST["name".$id]."', email='".$_POST["email".$id]."', description='".$_POST["description".$id]."', phone_number='".$_POST["phone_number".$id]."', operation='".$_POST["operation".$id]."', ONOFF='".$onoff."' WHERE id='".$id."'";  
  $result1=mysql_query($sql1);
 } 
} 

if($result1){ 
header("location:test_update2.php");
} 
mysql_close(); 
?>

谢谢你的帮助。

问候。

2 个答案:

答案 0 :(得分:0)

使用

if($_POST['Submit']) 
代替
if($Submit)

第二件事是你应该只使用id =“name”一次,并且每行都有id =“name”。

答案 1 :(得分:0)

据我所知,在用户表单提交后,您希望将用户重定向到test_update2.php,问题在于您不能简单地执行此操作,因为标题已经发送。你不能在HTML之后使用header()方法,因为在输出一些HTML之后你无法控制头文件。

我修复了你的代码并对其进行了测试,结果非常好。

编辑:

<?php 
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="root"; // Mysql password 
$db_name="db_test"; // Database name 
$tbl_name="test_table"; // Table name

// Connect to server and select databse. 
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// Check if button name "Submit" is active, do this 
if(isset($_POST['Submit'])) { 
    foreach($_POST['id'] as $id) { 
        $onoff = 0;
        if (isset($_POST["ONOFF".$id])) {
            $onoff = 1;
        }
        if($onoff == 1) {
            $sql1="UPDATE ".$tbl_name." SET name='".$_POST["name".$id]."', email='".$_POST["email".$id]."', description='".$_POST["description".$id]."', phone_number='".$_POST["phone_number".$id]."', operation='".$_POST["operation".$id]."', ONOFF='".$onoff."' WHERE id='".$id."'"; 
        } else {
            $sql1="UPDATE ".$tbl_name." SET ONOFF='".$onoff."' WHERE id='".$id."'"; 
        }
        $result1=mysql_query($sql1);
    } 
} 

//get data from DB
$sql="SELECT * FROM $tbl_name"; 
$result=mysql_query($sql); 

// Count table <strong class="highlight">rows</strong> 
$count=mysql_num_rows($result); 
?> 
<strong>Update <strong class="highlight">multiple</strong> <strong class="highlight">rows</strong> <strong class="highlight">in</strong> <strong class="highlight">mysql</strong></strong><br> 

<table width="500" border="0" cellspacing="1" cellpadding="0"> 
    <form name="form1" method="post" action="<? echo $_SERVER['REQUEST_URI']; ?>"> 
        <tr> 
            <td> 
                <table width="500" border="0" cellspacing="1" cellpadding="0"> 


                    <tr> 
                        <td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
                        <td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
                        <td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
                        <td align="center" bgcolor="#FFFFFF"><strong>Description</strong></td>
                        <td align="center" bgcolor="#FFFFFF"><strong>Phone Number</strong></td>
                        <td align="center" bgcolor="#FFFFFF"><strong>Operation</strong></td>
                    </tr> 
                    <?php 
                while($rows=mysql_fetch_array($result))
                { 
                    ?> 
                    <tr> 
                        <td align="center"><input type="hidden" name="id[]" value="<? echo $rows['id']; ?>" /><? echo $rows['id']; ?></td> 
                        <td align="center"><input name="name<? echo $rows['id']; ?>" type="text" id="name" value="<? echo $rows['name']; ?>"></td> 
                        <td align="center"><input name="email<? echo $rows['id']; ?>" type="text" id="email" value="<? echo $rows['email']; ?>"></td> 
                        <td align="center"><input name="description<? echo $rows['id']; ?>" type="text" id="description" value="<? echo $rows['description']; ?>"></td> 
                        <td align="center"><input name="phone_number<? echo $rows['id']; ?>" type="text" id="phone_number" value="<? echo $rows['phone_number']; ?>"></td>
                        <td align="center"><input name="operation<? echo $rows['id']; ?>" type="text" id="operation" value="<? echo $rows['operation']; ?>"></td>
                        <td align="center"><input name="ONOFF<? echo $rows['id']; ?>" type="checkbox" id="ONOFF" value="1" 
                            <?php if ($rows['ONOFF'] ==1) { echo "checked";} else {} ?> 
                        </td> 
                    </tr> 
                    <?php 
            } 
            ?> 
            <tr> 
                <td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td> 
            </tr> 
        </table> 
    </td> 
</tr> 
</form> 
</table> 
<?php
//close mysql connection
mysql_close(); 
?>