开发者

Updating mysql table using checkbox

开发者 https://www.devze.com 2023-01-15 14:52 出处:网络
i try to update a mysql table with multiple check box, but my code doesn\'t seem to work, so any help is welcome. My code is:

i try to update a mysql table with multiple check box, but my code doesn't seem to work, so any help is welcome. My code is:

<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开发者_JS百科, 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(); 
?>

Thanks for you help.

Regards.


Use

if($_POST['Submit']) 
instead of
if($Submit)

Second thing is that you should use id="name" only once, and you have id="name" on every row.


As far as I understood, after the user form submission you want to redirect the user to test_update2.php and the problem was that you couldn't simply do that since the headers were already been send. You can't ever use header() method after HTML, because you can't ever get control of the headers after you output some HTML.

I fixed your code and tested it out and it was working perfectly.

EDITED:

<?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(); 
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消