0

I have a form that requires an input from a user such as name, surname, telephone #, address, etc., and it contains Yes/No questions. For Yes/No questions I use radio buttons, for example: Is red your favorite colour? Yes No. A user has to check one. That form is inside my .php file, I also have another .php file that is connected with the form and should store data inside the database in MySQl. My question is am I storing radio buttons correctly and inside my database should my radio buttons be boolean?

A piece of my form that contains radio buttons:

<div class="control-group questions">
<div class="field-control">
<p>This is a Yes/No question</p>
<div class="input-wrapper">
<div class="answer"><input class="btn-checkbox" type="radio" name="groupone" value="yes" /><label>Yes</label></div>
<input class="btn-checkbox" type="radio" name="groupone" value="no" /><label>No</label><br />
</div>
</div>
</div>
<div class="control-group questions">
<div class="field-control">
<p>This is a Yes/No question</p>
<div class="input-wrapper">
<div class="answer"><input class="btn-checkbox" type="radio" name="grouptwo" value="yes" /> <label>Yes</label></div>
<input class="btn-checkbox" type="radio" name="grouptwo" value="no" /><label>No</label><br /
</div>
</div>
</div>

MySQL:

$value = @$_POST ['groupone'];
$sql = "INSERT INTO checkout (groupone) VALUES ('$value')";

if (!mysqli_query($connection, $sql)){
    die('Error: ' . mysqli_connect_error($connection));
}

$value = @$_POST ['grouptwo'];
$sql = "INSERT INTO checkout (grouptwo) VALUES ('$value')";

if (!mysqli_query($connection, $sql)){
    die('Error: ' . mysqli_connect_error($connection));
}
Mike Brant
  • 70,514
  • 10
  • 99
  • 103
Arwen
  • 205
  • 3
  • 10
  • Radio buttons are not really Boolean as they can have more than an on or off value. You could store their actual values which would be cleaner. – Jay Blanchard Oct 15 '14 at 20:23
  • 1
    Very important: Read through [How can I prevent SQL injection in PHP](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). Your code in its current form is highly vulnerable to tampering via SQL injection. Since you are using MySQLi, now is the time to begin learning to use [`prepare()/execute()`](http://php.net/manual/en/mysqli.prepare.php) to secure your code with prepared statements instead of passing `$_POST` directly into your SQL strings. – Michael Berkowski Oct 15 '14 at 20:27
  • Thank you Miachel Berkowski. I will definitely do it. At this moment I want to make sure that I am storing radio buttons properly and that everything that I type into the form goes to the database. Once I have it figured out I will make sure to prevent the sql injection. – Arwen Oct 15 '14 at 20:31
  • What do you want stored in your database? Do you want 'true' and 'false' or do you want 'yes' and 'no'? It's your database--you can do whatever you want. – i alarmed alien Oct 15 '14 at 20:43
  • I would prefer it to be "yes" and "no" but I can't figure out how to do it. In MySQL database after I write the name for the field, for example "groupone" I don't know what Type to make it and what Length should I pass it because I don't understand how MySQL stores data for radio buttons – Arwen Oct 15 '14 at 20:51
  • MySQL is just a generic data store--it doesn't know or care about how the data is presented in your form. You write PHP code to translate the values in your form into something you can put in your database. Since you're using phpmyadmin, [this link](http://codingforums.com/mysql/217959-what-type-radio-button.html) may be useful. – i alarmed alien Oct 16 '14 at 15:37
  • You can and should combine this into a single insert query. `INSERT INTO checkout (groupone, grouptwo) VALUES (?,?)`. You should also stop using error suppression (`@`). It is a horrible habit. Handle things properly in your code. – Mike Brant Oct 16 '14 at 15:59
  • @Mike Brant if I don't use @ then I get errors and my form doesn't get connected with the database. I don't know any other way to fix that error – Arwen Oct 16 '14 at 16:27
  • @Arwen You check for whether the $_POST variable is set and change behaviors appropriately, not suppress the errors. Fix errors, don't suppress them. It will make you a better developer. – Mike Brant Oct 16 '14 at 19:29

1 Answers1

0

If you want to store boolean value in your database, use this code

$value = @($_POST ['groupone'] == 'yes') ? true : false;
thangngoc89
  • 1,400
  • 11
  • 14