Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/212703-updating-mysql-table-using-checkbox/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.