Jump to content

please help dynamic radio button insertion into database


newone

Recommended Posts

hi , thanks in advance guys for your help  I am preparing a quiz which has question and multiple options 

i am currently working to fetch data from database as radio buttons for which i am successful but on inserting that into a database gives me a blank value and on top of it the radio button is not sticky for each  mcqs

code for fetching the data 

<?php $dbhost = 'localhost'; 
$dbuser = 'root'; 
$dbpass = ''; 
$db='tutorial';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass,$db); 
if(isset($_POST['value'])){ $value = $_POST['value']; }
if(! $conn ) 
		{ 
			die('Could not connect: ' . error); 
		} 
$sql = 'SELECT q1,option1,option2,option3 FROM questions '; 

$result = mysqli_query( $conn, $sql); 



if(! $result ) 
		{
	die('Could not get data: ' ); 
		} 
	while($row = mysqli_fetch_array($result)) 
	{ 

echo "{$row['q1']}" .'</br></br>'.
			" A <input type='radio' name='RadioGroup1' value='ans' id ='ans' 'checked=checked'>" . $row['option1'] . '<br> ' .
			" B <input type='radio' name='RadioGroup1' value='ans'>" . $row['option2'] . '<br> '.
			" C <input type='radio' name='RadioGroup1' value='option3']}'>" . $row['option3'] .  '<br> '.'<br>'; 

//"--------------------------------<br>"); 
	}
	echo "<form name=\"insert\" action=\"dbinsert.php\" method=\"post\">";
echo "<input type=\"submit\" value=\"submit\">";
echo "</form>";
	
	mysqli_close($conn)?>

the insertion part code is here

if(isset($_POST['value'])){ $value = $_POST['ans'];}
if(! $link ) 
		{ 
			die('Could not link: ' . error); 
		} 
		   $id='null';
		    $ans =  mysqli_real_escape_string($link, $_POST['ans']);
			//$first_name =  mysqli_real_escape_string($link, $_POST['first_name']);
			
    
    $sql = "INSERT into base (ans)VALUES ('$ans')";
		
    if(mysqli_query($link, $sql)){

        echo '<a href="quiz2.html">BEGIN TEST</a>';
		
    } else{

        echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
    }
       mysqli_close($link);
Link to comment
Share on other sites

You have several other issues but as to your problem. Your button group is named name='RadioGroup1' but you are trying to insert ans. There is no $_POST['ans']. There is also no $_POST['value'] either. 

 

You also gave two buttons the same value. You also have a ridiculous amount of unnecessary escaping. Your opening form tag is in the completely wrong place. The whole bit of code is quite a mess.

 

You need to take a minute and study some basic tutorials. The whole thing needs a complete re-write. And while you're at it you should do it in PDO. https://phpdelusions.net/pdo

 

Edit * It looks like you were already give good information on this in your other posts but you did not listen to what you were told. Your were even given code by @gingerjm that you ignored. You are just wasting our time if you're not going to do what we tell you. After seeing the other posts and responses you got you have now irritated me for wasting my time on this post.

Edited by benanamen
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.