phpapprentice Posted October 16, 2008 Share Posted October 16, 2008 Please Help... How can I store data from radio button into mysql database? I want to store TRUE or FALSE into field1(IHE Personnel), field2(K-12 Personnel) , field3(Scholar). If IHE Personnel is selected, i want TRUE to be stored in field1, and it will store FALSE for both field2 and field3. This is my form ********************************************** Who are you? <input type="radio" name="personnel" value="true" />IHE Personnel <input type="radio" name="personnel" value="true" /> K-12 Personnel <input type="radio" name="personnel" value="true" /> Scholar ********************************************** phpapprentice Link to comment https://forums.phpfreaks.com/topic/128741-solved-radio-buttons-to-store-data-in-mysql-database/ Share on other sites More sharing options...
F1Fan Posted October 16, 2008 Share Posted October 16, 2008 You will need logic before you insert/update the DB table. This logic should assign all variables to FALSE, except the one that was selected. So, you'll need to change the values of each radio button to the field for each selection. Like this: <?php $fields['field1'] = false; $fields['field2'] = false; $fields['field3'] = false; $fields[$_POST['personnel']] = true; // do whatever with your DB... ?> Then radio buttons: <input type="radio" name="personnel" value="field1" />IHE Personnel <input type="radio" name="personnel" value="field2" /> K-12 Personnel <input type="radio" name="personnel" value="field3" /> Scholar Link to comment https://forums.phpfreaks.com/topic/128741-solved-radio-buttons-to-store-data-in-mysql-database/#findComment-667234 Share on other sites More sharing options...
phpapprentice Posted October 16, 2008 Author Share Posted October 16, 2008 this is my insert.php. there is an error. Error: Column count doesn't match value count at row 1 **************************************************** <?php $con = mysql_connect("localhost","mydatabase","mypassword"); if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("noyce", $con); $fields['field1'] = false; $fields['field2'] = false; $fields['field3'] = false; $fields[$_POST['personnel']] = true; $sql="INSERT INTO people (FirstName, LastName, field1, field2, field3) VALUES ('$_POST[FirstName]','$_POST[LastName]',$fields)";if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added";mysql_close($con) ?> Link to comment https://forums.phpfreaks.com/topic/128741-solved-radio-buttons-to-store-data-in-mysql-database/#findComment-667307 Share on other sites More sharing options...
F1Fan Posted October 16, 2008 Share Posted October 16, 2008 That's because SQL doesn't know PHP. You're trying to insert a raw array. Do this: <?php $sql="INSERT INTO people (FirstName, LastName, field1, field2, field3) VALUES ('{$_POST['FirstName']}', '{$_POST['LastName']}', {$fields['field1']}, {$fields['field2']}, {$fields['field3']})"; ?> Also, I corrected a little of the syntax. Link to comment https://forums.phpfreaks.com/topic/128741-solved-radio-buttons-to-store-data-in-mysql-database/#findComment-667314 Share on other sites More sharing options...
phpapprentice Posted October 16, 2008 Author Share Posted October 16, 2008 thank you very much. it works. It stores 0 and 1, How can I make those 0 and 1 to FALSE and TRUE mysql> select * from people; +----+----------+-----------+--------+--------+--------+ | id | LastName | FirstName | field1 | field2 | field3 | +----+----------+-----------+--------+--------+--------+ | 1 | Smith | Will | NULL | NULL | NULL | | 2 | | | NULL | NULL | NULL | | 3 | Poe | Fernando | NULL | NULL | NULL | | 4 | Moore | Stacey | 1 | 0 | 0 | +----+----------+-----------+--------+--------+--------+ this is the code that works(just the brackets, i removed them.) ************************************************************ $sql="INSERT INTO people (FirstName, LastName, field1, field2, field3) VALUES ('$_POST[FirstName]','$_POST[LastName]','$fields[field1]', '$fields[field2]', '$fields[field3]')"; Link to comment https://forums.phpfreaks.com/topic/128741-solved-radio-buttons-to-store-data-in-mysql-database/#findComment-667397 Share on other sites More sharing options...
F1Fan Posted October 16, 2008 Share Posted October 16, 2008 What data type are those fields? Link to comment https://forums.phpfreaks.com/topic/128741-solved-radio-buttons-to-store-data-in-mysql-database/#findComment-667399 Share on other sites More sharing options...
phpapprentice Posted October 16, 2008 Author Share Posted October 16, 2008 boolean Link to comment https://forums.phpfreaks.com/topic/128741-solved-radio-buttons-to-store-data-in-mysql-database/#findComment-667400 Share on other sites More sharing options...
F1Fan Posted October 16, 2008 Share Posted October 16, 2008 I guess that's just how it returns it, or that's how MySQL does Boolean. I don't use MySQL very often, so I'm not sure. In any case, you should be able to just convert it on extraction. Link to comment https://forums.phpfreaks.com/topic/128741-solved-radio-buttons-to-store-data-in-mysql-database/#findComment-667406 Share on other sites More sharing options...
phpapprentice Posted October 16, 2008 Author Share Posted October 16, 2008 OKAY, thanks a lot PROBLEM SOLVED Link to comment https://forums.phpfreaks.com/topic/128741-solved-radio-buttons-to-store-data-in-mysql-database/#findComment-667408 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.