0

i need help in the following code i am trying to create dynamic radio buttons from mysql db and want to insert into another table its not echoing data and not even inserting i am just beginner please help me out thanks in advance

<?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"> </th> </tr> <th scope="col"> <?php while($rows=mysqli_fetch_array($result)) ?> <table width=100% cellspacing="1"> <tr> <th scope="col"> </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> <?php
$dbuser = 'root'; 
$dbpass = ''; 
$db='tutorial';
$dbhost='localhost';
$conn = mysqli_connect($dbhost,$dbuser,$dbpass,$db); 
if(! $conn ) 
        { 
            die('Could not connect: ' . error); 
        } 
        //$first_name =  mysqli_real_escape_string($conn, $_POST['first_name']);
        $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);

        ?> 
2
Contributors
1
Reply
17
Views
6 Months
Discussion Span
Last Post by broj1
0

Quite many issues here, some big, some smaller. I'll try to comment on some without any particular order of importance.

  1. On line 9 you try to concatenate some unknown constant. It should be either a variable (i.e. $error, but defined somewhere) or mysqli's error function mysqli_connect_error().
  2. I strongly advise against using HTML tables for positioning elements. This is not strictly an error but it is a bad design practice. Google for semantic html. In your case a table within table calls for problems, since tr tags are already messed up
  3. There is no need to disconnect from DB and then reconnect on line 15
  4. The real problem is that the code for inserting into DB should trigger only on submitting the form probably on the for.php page.
  5. The $_POST['option1'], $_POST['option2']... elements do not exist since they have not been defined anywhere in the form. You should probably use $_POST['radio1_1'] since this is the name of your radio button. Probably you should have more radio buttons for each question.

Edited by broj1

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.