-1

This is the code I have written:

index.html

<!DOCTYPE html>
<html>
<head>
</head>
<body>

<form action="db.php" method="post">

YES:<input type="radio" name="YES" value="YES">

NO:<input type="radio"  name="NO"  value="NO" >

<input type="submit" name="submit value="submit">
</form>

</body>
<html>

db.php

<?php

    $con = mysqli_connect('localhost','my user id','my password');
    if(!con)
    { echo 'not connected to server';}else { echo 'something else is wrong' ;}

    if(!mysqli_select_db($con,'my user id')
    { echo 'Database error selection';}

    $YES = $_POST['YES'];
    $NO = $_POST['NO'];

    $sql = INSERT INTO users (YES,NO) VALUES ('$YES','$NO');

    if(!mysqli_query($con,$sql))
    { echo 'Answer not submitted please try again!!!!!!!!';}
    else{
    echo 'Your answer successfully submitted \n Thanks for participating';}

    header(" refresh:2; url=index.html");

?>

There is something wrong in it. Can anyone please tell what is wrong?

Note: If 2 users submit YES then in database YES would get int value as 2 If 1 users submit NO then in database NO would get int value as 1.

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
rohit singh
  • 13
  • 1
  • 3
  • What's exactly wrong with it? Aside from not closing this attribute `name="submit"` – Lixus Apr 22 '17 at 15:01
  • first of all, you have to give same name to both of radio button in input, also for reference http://stackoverflow.com/questions/15868935/adding-radio-button-values-to-mysql-table-using-php – sagar patel Apr 22 '17 at 15:13
  • And your radio buttons might as well be checkboxes since selecting one won't deselect the other. and you don't associate a numeric value to them anyway. – Sloan Thrasher Apr 22 '17 at 15:14

2 Answers2

0

You are using radio button the wrong way. Radio button should have the same 'name'. In your case you may use name="yesno".

 YES:<input type="radio" name="yesorno" value="yes">
 NO:<input type="radio" name="yesorno" value="no">

For your PHP it should be like this.

 <?php

if (isset($_POST['submit'])) {
 $yesorno=$_POST['yesno'];
  $sql = INSERT INTO users (columnName) VALUES ('$yesorno');
  mysqli_query($con,$sql); // Execute query
}
?>

And please express your goal as clear as possible. For now i pinpointed what is wrong on your coding semantics.

YATO
  • 107
  • 14
  • I want the answer to be submitted in database, in such a way so I can understand how many people had submitted the amswer as "YES" and how many submitted the answer as "NO". Forgot about my codes which I had written can u give me the new from scratch – rohit singh Apr 23 '17 at 07:22
  • Then save their answers as I written. Then in a new Query SELECT * FROM table_name WHERE columnName='YES'; You have to write different scripts. – YATO Apr 23 '17 at 08:40
  • Can u explain it more clearly by giving it in a code format. I am unable to understand what are you trying to say – rohit singh Apr 23 '17 at 09:01
  • Please give me the database SQL also (the code I had to write to make the table)..... – rohit singh Apr 23 '17 at 09:22
  • @rohitsingh I think you should open a new question for that. But, if you would browse for more answers here in stackoverflow, I am sure you'll find what you are looking for. – YATO Apr 24 '17 at 02:22
0

Above looks good. Another way is to add a label:

<label for="yesorno">Question:</label>
<input type="radio" name="yesorno" value="yes">YES:
<input type="radio" name="yesorno" value="no">NO:<br />
Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75