Need HELP urgently in dyanmic radio button generation,insert

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
newbaba
Forum Newbie
Posts: 17
Joined: Thu Jan 26, 2017 10:36 pm
Location: Karachi

Need HELP urgently in dyanmic radio button generation,insert

Post by newbaba »

i need urgent help please help me out in the following code i want to generate radio button from the values of mysql db in radio button format and want to insert in other table
these are the following codes
for dynamic radio generation

Code: Select all

<?php
$dbhost = 'localhost'; 
$dbuser = 'root'; 
$dbpass = ''; 
$db='tutorial';
$conn = mysqli_connect($dbhost,$dbuser,$dbpass,$db); 
if(! $conn ) 
		{ 
			die('Could not connect: ' . error); 
		} 
$sql = 'SELECT * FROM questions';
$result=mysqli_query($conn,$sql)


?>
<form action ="for.php" method="post">
<table width=100% cellspacing="1">
<tr>
<th scope="col">&nbsp;</th>
</tr>
<th scope="col">
<?php while($rows=mysqli_fetch_array($result)) ?>
		

<table width=100% cellspacing="1">
<tr>
<th scope="col">&nbsp;</th>

<input type="radio" name="radio1_1"id="q_1"value="<?php echo $rows['$q1'];?>">
<label for="q1">:<?php echo $rows['$q1'];?></label>
 
</tr>
</table>
<?php mysqli_close($conn);?>
</th>
</tr>
<tr>
<tr>
<th scope="col"><input type="submit"name="submit"id="submit"value="submit"></th>
<tr>
<table>
</form>
THE OTHER CODE FOR INSERTION INTO DATABASE

Code: Select all

<?php
$dbuser = 'root'; 
$dbpass = ''; 
$db='tutorial';
$dbhost='localhost';
global option1;
global option2;
global option3;

$conn = mysqli_connect($dbhost,$dbuser,$dbpass,$db); 
if(! $conn ) 
		{ 
			die('Could not connect: ' . error); 
		} 
		
		$option1=mysqli_real_escape_string($conn, $_POST['option1']);
		$option2=mysqli_real_escape_string($conn, $_POST['option2']);
		$option3=mysqli_real_escape_string($conn, $_POST['option3']);
		
		$sql = "INSERT INTO feedback (option1,opion2,option3) VALUES ('$option1,$option2,$option3')";
		
		mysqli_close($conn);
		
		?>
the code is not producing any error for display of radio button but not showing any output
please help me
thanks alot in advance
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Need HELP urgently in dyanmic radio button generation,in

Post by Celauran »

Syntax error here. This is meant to be the start of a block, so needs to have either { or : at the end, and the block needs to be closed by } or endwhile, respectively.

Code: Select all

<?php while($rows=mysqli_fetch_array($result)) ?>
Replace this:

Code: Select all

<?php while($rows=mysqli_fetch_array($result)) ?>
               

<table width=100% cellspacing="1">
<tr>
<th scope="col">&nbsp;</th>

<input type="radio" name="radio1_1"id="q_1"value="<?php echo $rows['$q1'];?>">
<label for="q1">:<?php echo $rows['$q1'];?></label>
 
</tr>
</table>
<?php mysqli_close($conn);?>
With this:

Code: Select all

<?php while ($rows = mysqli_fetch_array($result)): ?>
<table width=100% cellspacing="1">
    <tr>
        <th scope="col">&nbsp;</th>
        <td>
            <input type="radio" name="radio1_1"id="q_1"value="<?php echo $rows['$q1'];?>">
            <label for="q1">:<?php echo $rows['$q1'];?></label>
        </td>
    </tr>
</table>
<?php endwhile; ?>
That won't get you all the way there, though. I doubt $q1 is a valid column name, so you'll need to fix that also. Probably just q1, but I have no way of knowing since you're using a SELECT *
newbaba
Forum Newbie
Posts: 17
Joined: Thu Jan 26, 2017 10:36 pm
Location: Karachi

Re: Need HELP urgently in dyanmic radio button generation,in

Post by newbaba »

many many thanks its now showing the desired result the radio buttons with the value from the database table thanks alot
if you or anyone could please help me in insertion as well the errors i am facing are as follows
Undefined index: option1 in C:\wamp\www\for.php on line 18
Undefined index: option2 in C:\wamp\www\for.php on line 19
Undefined index: option1 in C:\wamp\www\for.php on line 20
ERROR: Could not able to execute INSERT INTO feedback (option1,opion2,option3) VALUES (',,'). Column count doesn't match value count at row 1


the php code is as follows

Code: Select all

<?php
$dbuser = 'root'; 
$dbpass = ''; 
$db='tutorial';
$dbhost='localhost';
/*
global option1;
global option2;
global option3;
*/

$conn = mysqli_connect($dbhost,$dbuser,$dbpass,$db); 
if(! $conn ) 
		{ 
			die('Could not connect: ' . error); 
		} 
		
		$option1=mysqli_real_escape_string($conn, $_POST['option1']);
		$option2=mysqli_real_escape_string($conn, $_POST['option2']);
		$option3=mysqli_real_escape_string($conn, $_POST['option3']);
		
		$sql = "INSERT INTO feedback (option1,opion2,option3) VALUES ('$option1,$option2,$option3')";
		
		if(mysqli_query($conn, $sql)){

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

        echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);
    }
		
		mysqli_close($conn);
		
		?>


User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Need HELP urgently in dyanmic radio button generation,in

Post by Celauran »

You need quotes around each value you are inserting. Also, the values appear to be empty strings.
newbaba
Forum Newbie
Posts: 17
Joined: Thu Jan 26, 2017 10:36 pm
Location: Karachi

Re: Need HELP urgently in dyanmic radio button generation,in

Post by newbaba »

Thanks alot thanks again it solved my issue
It was amazing really much appreciated
Post Reply