views:

41

answers:

2

Hi all, 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, 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.

A: 

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.

cichy
I'm sorry but this does not change any thing.
gallois
i have changed my code like you suggest but still can't update the table!!!!
gallois
Again nothing was changed with your code, still have the same problem
gallois
A: 

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(); 
?>
João Gala Louros
Thanks for the reply, but i want just to update multiple entry in mysql DB. the file that i posted is test_update2.php (i don't want to redirect, i want just to refresh the page after updating entries).
gallois
i m using this tutorial but it doesn't work at all:
gallois
http://www.phpeasystep.com/mysql/10.html
gallois
Just updated the post. Test this code, now it should work intended. It updates only the row with the checkbox "checked" and refresh after data is submitted.
João Gala Louros
post the code plz
gallois
You can see it above. I just edited my post. I also indented for better readability.
João Gala Louros
Always the same result!
gallois
Sorry a had a small bug there. Check it now again. But can you tell what you see? Did you tried to populate the DB table with some records? Can you get those records? If not is because you have something wrong in your connection string. Look for a file in your server folder named /logs/php_error.log then run the code that I posted above and see if he outputs any error.
João Gala Louros