aeboi80 Posted November 12, 2009 Share Posted November 12, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/181207-solved-radio-button-keeps-storing-1-in-db/ Share on other sites More sharing options...
Philip Posted November 12, 2009 Share Posted November 12, 2009 if ($open_status = 'checked') { and if ($closed_status = 'checked') { both need a comparison operator == if ($open_status == 'checked') { and if ($closed_status == 'checked') { Quote Link to comment https://forums.phpfreaks.com/topic/181207-solved-radio-button-keeps-storing-1-in-db/#findComment-955982 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.