Jump to content

Recommended Posts

Hi,

 

I have a list of products and a check box next to it. The checkbox reads as <input type="checkbox" name="sale<?php print '$rownum'?>"  value="1" />. The $rownum is having value 1,2, so on.

 

now, If i select one of the checkbox and hit submit it will update that record in the databse. however if I select 2 or 3 checkbox and hit submit it just updates one record not all the 3 which I have selected. Can anyone tell what's wrong.

 

With $_post i am using $sale=$_POST["sale".'$rownum'];

 

Is there something which is wrong?

 

thanks

 

Link to comment
https://forums.phpfreaks.com/topic/106852-post-the-checkbox-value/
Share on other sites

here's a file that I figured out a fix (it's in an opensource mailgroup solution I put out). Parse through it, and it should help you figure it out.

show contacts:

<?php
elseif($_GET['page'] == "show_all_contacts"){
?><form action="remove_contacts.php" method="POST" name="removeContacts">
<?php
$contact_count = 1;
$checkbox_count = 0;
$sql = "SELECT email_address, id, name from {$_SESSION['username']}_table_{$list_name} WHERE verified =1 ORDER BY email_address";
$result = mysql_query($sql);
print "<h1><strong>VIEW/REMOVE CONTACTS</strong></h1><br /><table align='center'>\n<tr>\n";
while ($row = mysql_fetch_assoc($result)){
	if ($contact_count == 3){
		print "<td align='left'><input type='checkbox' name='{$row['id']}' value='".$row['id']."'><a href='remove_contacts.php?id={$row['id']}'><img src='templates/blue/recycler.gif' alt='Remove' title='Remove' border='0'></a>".$row['name']."(".$row['email_address'].")</td>\n</tr>\n";
		$contact_count = 1;
	}
	else{
		print "<td align='left'><input type='checkbox' name='{$row['id']}' value='".$row['id']."'><a href='remove_contacts.php?id={$row['id']}'><img src='templates/blue/recycler.gif' alt='Remove' title='Remove' border='0'></a>".$row['name']."(".$row['email_address'].")</td>\n";
		++$contact_count;
	}
}
if ($contact_count == 1){ print "</tr>\n";}
print "</table>\n";
$contact_count = 1;
$sql = "SELECT email_address, id, name from {$_SESSION['username']}_table_{$list_name} WHERE verified =0 ORDER BY email_address";
$result = mysql_query($sql);
print "<h1><strong>VIEW/REMOVE UNVERIFIED CONTACTS</strong></h1><br /><table align='center'>\n<tr>\n";
while ($row = mysql_fetch_assoc($result)){
	if ($contact_count == 3){
		print "<td align='left'><input type='checkbox' name='{$row['id']}' value='".$row['id']."'><a href='remove_contacts.php?id={$row['id']}'><img src='templates/blue/recycler.gif' alt='Remove' title='Remove' border='0'></a>".$row['name']."(".$row['email_address'].")</td>\n</tr>\n";
		$contact_count = 1;
	}
	else{
		print "<td align='left'><input type='checkbox' name='{$row['id']}' value='".$row['id']."'><a href='remove_contacts.php?id={$row['id']}'><img src='templates/blue/recycler.gif' alt='Remove' title='Remove' border='0'></a>".$row['name']."(".$row['email_address'].")</td>\n";
		++$contact_count;
	}
}
if ($contact_count == 1){ print "</tr>\n";}

print "<tr>\n<td align='left'><input type=submit value='DELETE'></td>\n</tr>\n</table>\n</form>\n";
}
?>

remove contacts

<?php
if (isset($_GET['id'])){
$removal_sql = "DELETE FROM `{$_SESSION['username']}_table_{$list_name}` WHERE id={$_GET['id']} LIMIT 1;";
	mysql_query($removal_sql);
}
else{
$removal = implode($_POST);
foreach($_POST as $key => $value) {
 if ($value != "DELETE"){
$removal_sql = "DELETE FROM `{$_SESSION['username']}_table_{$list_name}` WHERE id={$value} LIMIT 1;";
	 mysql_query($removal_sql);
	}

}
?>

You need to update the database (run the query) for each checkbox value that's posted.  Most people do this by passing the checkbox data in an array instead of an individual variable for each one, and then either loop through the array (putting the query inside the loop) or rephrasing the query (it really depends on the data etc.. as to what's best).

Hey art,

 

Using an array to pass multiple values from a form is a very common question that not only comes up a lot, but you will also find it inside people's code snippets in practically every other thread...because like 99% of forms have multiple values to be passed, and it's just more logical to use an array instead of individual variables. 

 

I pointed you in the right direction with my other post.  This post is me suggesting you use the search function for a very common coding practice. 

 

Good luck and happy coding

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.