skip to Main Content

i’m a newbie and is still in learning stage to php database. I trying to create a feedback form with 10 descriptor and 3 options of “Disagree”, “Satisfactory” and “Agree” for user to check using radio button. When user chosen a option and submit, example “Agree”, value of 1 should update into database under “Agree” column. If another user submitted the same option, value under “Agree” column should increase to 2. My problem is unable to update the value correctly into the correct column in the database when goes to 2nd and 3rd descriptor. Would appreciate if anyone can help to solve my problem.Thank you very much.
My table: Column 1:ID, column 2:descriptor, column 3:disagree, column 4:satisfactory, column 5:agree
Example of my feedback form with php script(from internet):

<?php
$con=mysqli_connect("localhost","root","","testing2");
if($con){ echo "Connected";}

If(isset($_POST['update'])){

  $query="UPDATE form SET disagree=disagree+1 WHERE id='$_POST[ans1]'";
  $query="UPDATE form SET satisfactory=satisfactory+1 WHERE id='$_POST[ans1]'";
  $query="UPDATE form SET agree=agree+1 WHERE id='$_POST[ans1]'";
  $result=mysqli_query($con,$query);

  if($result){
    echo "OKAY";
  }else{
    echo "NOT OKAY";
  }
  
}

?>

<!DOCTYPE html>
<html>
<head>
	<title></title>
</head>
<body>
<form method="post" action="form.php">
	<table>
		<tr>
			<th></th>
			<th>Disagree</th>
			<th>Satisfactory</th>
			<th>Agree</th>
		</tr>
		        <tr>
              <td>The duration of the program is appropriate</td>
              <td><input type="radio" value="1" name="ans1" /></td>
              <td><input type="radio" value="1" name="ans1" /></td>
              <td><input type="radio" value="1" name="ans1" /></td>
            </tr>

            <tr>
              <td>The course content is revelent</td>
              <td><input type="radio" value="2" name="ans2"  /></td>
              <td><input type="radio" value="2" name="ans2" /></td>
              <td><input type="radio" value="2" name="ans2" /></td>
            </tr>

            <tr>
              <td>The learning objectives have been met</td>
              <td><input type="radio" value="3" name="ans3"  /></td>
              <td><input type="radio" value="3" name="ans3" /></td>
              <td><input type="radio" value="3" name="ans3" /></td>
            </tr>
	</table>
        
     <input type="submit" name="update" value="submit">

</form>

</body>
</html>

2

Answers


  1. You will have to do something like this to execute each one (Just cleaner please):

    $query="UPDATE form SET disagree=disagree+1 WHERE id='$_POST[ans1]'";
    $result=mysqli_query($con,$query);
    if($result){
        echo "OKAY";
    }else{
        echo "NOT OKAY";
    }
    
    $query="UPDATE form SET satisfactory=satisfactory+1 WHERE id='$_POST[ans1]'";
    $result=mysqli_query($con,$query);
    if($result){
        echo "OKAY";
    }else{
        echo "NOT OKAY";
    }
    
    $query="UPDATE form SET agree=agree+1 WHERE id='$_POST[ans1]'";
    $result=mysqli_query($con,$query);
    if($result){
        echo "OKAY";
    }else{
        echo "NOT OKAY";
    }
    

    Hopefully you get OKAY 3 times.

    Login or Signup to reply.
  2. I’ll ignore the security aspects of using raw data in a MySQL query like this.

    The values in your table can be used better, as can the names of the radio button groups:

    <?php
    $con=mysqli_connect("localhost","root","","testing2");
    if($con){ echo "Connected";}
    
    if(isset($_POST['update'])){
       foreach($_POST as $id => $column) {
           if(is_numeric($id)) {
                $query= 'UPDATE form SET ' . $column . '=' . $column.'+1 WHERE id=' . $id;
           }
       }
    
      $result=mysqli_query($con,$query);
    
      if($result){
        echo "OKAY";
      }else{
        echo "NOT OKAY";
      }
    
    }
    
    ?>
    
    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
    </head>
    <body>
    <form method="post" action="form.php">
        <table>
            <tr>
                <th></th>
                <th>Disagree</th>
                <th>Satisfactory</th>
                <th>Agree</th>
            </tr>
                <tr>
                  <td>The duration of the program is appropriate</td>
                  <td><input type="radio" value="disagree" name="1" /></td>
                  <td><input type="radio" value="satisfactory" name="1" /></td>
                  <td><input type="radio" value="agree" name="1" /></td>
                </tr>
    
                <tr>
                  <td>The course content is revelent</td>
                  <td><input type="radio" value="disagree" name="2"  /></td>
                  <td><input type="radio" value="satisfactory" name="2" /></td>
                  <td><input type="radio" value="agree" name="2" /></td>
                </tr>
    
                <tr>
                  <td>The learning objectives have been met</td>
                  <td><input type="radio" value="disagree" name="3"  /></td>
                  <td><input type="radio" value="satisfactory" name="3" /></td>
                  <td><input type="radio" value="agree" name="3" /></td>
                </tr>
        </table>
    
         <input type="submit" name="update" value="submit">
    
    </form>
    
    </body>
    </html>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search