UPDATE DATABASE USING THE MULTIPLE OF CHECKBOX VALUES

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
stalin
Forum Newbie
Posts: 3
Joined: Sun Jan 16, 2011 12:09 am

UPDATE DATABASE USING THE MULTIPLE OF CHECKBOX VALUES

Post by stalin »

I am knew to php.How to update the mysql database using the multiple checkbox values.

***** PLEASE USE PHP CODE TAGS *****

Code: Select all

<?php
		$result = mysql_query("SELECT * FROM att"); 
		if(mysql_num_rows($result)>0)
		{
			$i=0;
			while($row = mysql_fetch_array($result))
			{
				$del=0;
    			$regno=$row['regno'];
    			$fname=$row['fname'];
				$i++;
?>
		   <tr>
            <td width="18%" height="42" align="center"><?php echo $i;?></td>
            <td width="26%" align="center"><?php echo $regno;?></td>
            <td width="28%" align="center"><?php echo $fname;?></td>
            <td width="28%" height="42" align="center"><input type="checkbox" name="<?php echo $regno;?>" value="0"/></td>
		    </tr>
<?php
}
}	
?>

            <tr>
            <td height="42" colspan="4" align="center"><input type="submit" name="submit" value="SUBMIT"/></td>
            </tr>
          </table></td>
      </tr>
    </table></td>
  </tr>
</table>
</td>
  </tr>
</table>
</td>
  </tr>
</table>
</form>
</body>
</html>
<?php
if(isset($_POST['$regno']))
{
	$result = mysql_query("SELECT * FROM att");
	if(mysql_num_rows($result)>0)
  	{
	 	$regno=$_REQUEST['regno'];
	 	$fname=$_REQUEST['fname'];
		echo $regno;
		$i=1;
 		while($row = mysql_fetch_array($result))
 		{
	   		if($regno==$row['regno'])
			{
 				$result1=mysql_query("insert into att (fh) values (0)"); 
				$result1=mysql_query("commit");
			}
  
		}
  	}  
}
?>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: UPDATE DATABASE USING THE MULTIPLE OF CHECKBOX VALUES

Post by social_experiment »

You use an array, from looking at your code you have to change the checkboxes slightly.

Code: Select all

<?php <input type="checkbox" name="<?php echo $regno;?>" value="0"/> ?>
$regno is a different value each time? If so, your html has to look like this

Code: Select all

 <input type="checkbox" name="someArray[]" value="regNoValueHere" /> 
When you submit the form :

Code: Select all

<?php
 if (is_array($_POST['someArray'])) {
 foreach ($_POST['someArray'] as $value) {
  // sql query here;
 }
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply