+ 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
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>");
}
+ 1
Are you using any framework or just plain PHP?
+ 1
just plain php using notepad++
+ 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
+ 1
you have to put 3 radio button if you want three option and each radio button should have different value but same name