Showing posts with label radio. Show all posts
Showing posts with label radio. Show all posts

Monday, June 13, 2016

Update Multiple Records Based On Radio Buttons Using FOR EACH

Today I will share a code snippet which will enable you to update multiple records based on radio buttons selection.

Consider this scenario:

Players are applying to play for a football team. There are more players than there are places, so some will get to play for the First team, some for the B team, some will be in the Reserves, and some will not get selected at all.

If you want to be able to pull up all players and assign their application result in the database in one update process.

Now you can do it using FOR EACH as shown in the snippet below:


foreach ($_POST[result] as $id => $result) {

$results[$result][] = $id;

}

$query = "UPDATE tablename SET result = CASE ";

foreach ($results as $res => $ids) {

$query .= sprintf("WHEN id IN (%s) THEN %s ", join(,, $ids), $res);

}

$query .= END;


Hope php script can guide you in the right direction.

Enjoy!

Friday, May 13, 2016

How To Add Radio Button Value In Database In PHP

Today I will share a working script example which will help you to insert radio button value in database using PHP.

<?php
if(isset($_POST[submit]))
{
$number=$_POST[number];
if($number!="")
{
$query=mysql_query("UPDATE users SET number = $number WHERE name = ".$user->name.";");
if($query)
{
header(Location: /me);
}
else
{
$error_message = <font color="red"><strong>Please choose a number.</strong></font><br /><br />;
}
}
}
?>


<form method="post" action="">
<input type="radio" name="number" value="1" />1
<input type="radio" name="number" value="2" />2
<input type="radio" name="number" value="3" />3
<input type="submit" name="submit" />
</form>
?>

This simple script will enable you to add radio button values in database easily.Hope you found this post useful.

Enjoy!