Radio button in mysql using php | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Radio button in mysql using php

i am ferching data from mysql db . iwant them to be shown in radio button format basically i am preparing a mcq quiz so it should be like that thanks in advance

21st Dec 2016, 2:28 PM
Owais Khan
Owais Khan - avatar
5 Answers
+ 2
here is my code it is showing the radio button but not the data fetched why what i am missing also it said unidentified variabl q1 which i made global so it got resolved thanks in advance <?php $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = ''; $db='tutorial'; $conn = mysqli_connect($dbhost, $dbuser, $dbpass,$db); global $q1; if(! $conn ) { die('Could not connect: ' . error); } $sql = 'SELECT q1,option1,option2,option3 FROM questions'; //mysqli_select_db('tutorial'); $result = mysqli_query( $conn, $sql); if(! $result ) { die('Could not get data: ' ); } while($row = mysqli_fetch_array($result)) { echo ("<input type='radio' name='q1' value='$q1'>".'</br>'. " A {$row[1]} <br> ". " B {$row[2]} <br> ". " C {$row[3]} <br> ". "--------------------------------<br>"); }
25th Dec 2016, 6:45 PM
Owais Khan
Owais Khan - avatar
+ 1
Are you using any framework or just plain PHP?
21st Dec 2016, 3:37 PM
Rishi Anand
Rishi Anand - avatar
+ 1
just plain php using notepad++
21st Dec 2016, 3:48 PM
Owais Khan
Owais Khan - avatar
+ 1
So, you know how radio button is implemented. The properties you want to be dynamically implemented are its value and name. So create the element, and inside its value open a PHP block and echo the value from the db. However as far as I know you are not familiar with how exactly it works. In that case familiarise yourself with PHP more, that'll help
21st Dec 2016, 4:01 PM
Rishi Anand
Rishi Anand - avatar
+ 1
you have to put 3 radio button if you want three option and each radio button should have different value but same name
30th Dec 2016, 4:32 AM
Amit Dhankani
Amit Dhankani - avatar