Jump to content

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

 

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.