Jump to content

[SOLVED] Radio Button Keeps Storing 1 In DB


aeboi80

Recommended Posts

I have read a few of the threads on storing radio button values in a DB and I am a bit lost.  I have 2 radio buttons 1 is called Open the other is called close  Here is the code for the form page.

<?php 
// Connects to your Database 
mysql_connect("localhost", "powerful_switch", "switch") or die(mysql_error()); 
mysql_select_db("powerful_switch") or die(mysql_error()); 
?>
<?PHP

$open_status = 'unchecked';
$closed_status = 'unchecked';

if (isset($_POST['submit'])) {

$selected_radio = $_POST['switch'];

if ($selected_radio == 'open') {
$open_status = 'checked';
}
else if ($selected_radio == 'closed') {
$closed_status = 'checked';
}
}

?>

<form id="form1" name="form1" method="post" action="flip.php">
  
    <p>
      <input type="radio" name="switch" id="open" value="open" />
      Open  </p>
    <p>
      <input type="radio" name="switch" id="closed" value="closed" />
    Closed</p>
    <p>
      <input type="submit" name="submit" id="submit" value="Flip It!" />
    </p>
</form>

 

Here is the code for the flip.php file:

 

<?php 
// Connects to your Database 
$con=mysql_connect("localhost", "powerful_switch", "switch") or die(mysql_error()); 
mysql_select_db("powerful_switch") or die(mysql_error()); 
?>
<?php
if ($open_status = 'checked') {
	mysql_query("UPDATE switch SET status = '1' WHERE id = '1'");
	echo "Open ". $open_status;
    mysql_close($con);
}
else
if ($closed_status = 'checked') {
	mysql_query("UPDATE switch SET status = '0' WHERE id = '1'");
	echo "Closed ". $closed_status;
	mysql_close($con);
}

?>

 

No matter what happens the value is always value of $open_status = checked  So 1 is always updated in the DB table.

 

My DB table is called switch and has 2 columns

 

 

CREATE TABLE IF NOT EXISTS `switch` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `status` int(10) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=2 ;

 

Not sure what is going wrong here.

 

Thanks

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.