简体   繁体   English

使用单选按钮将增量值更新到 phpmyadmin 数据库中

[英]Using radio button to update increment value into phpmyadmin database

i'm a newbie and is still in learning stage to php database.我是新手,仍处于学习 php 数据库的阶段。 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.我试图创建一个带有 10 个描述符和“不同意”、“满意”和“同意”的 3 个选项的反馈表单,供用户使用单选按钮进行检查。 When user chosen a option and submit, example "Agree", value of 1 should update into database under "Agree" column.当用户选择一个选项并提交时,例如“同意”,值 1 应更新到“同意”列下的数据库中。 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.如果另一个用户提交了相同的选项,则“同意”列下的值应增加到 2。我的问题是在转到第二个和第三个描述符时无法将值正确更新到数据库中的正确列中。 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):我的表:第 1 列:ID,第 2 列:描述符,第 3 列:不同意,第 4 列:满意,第 5 列:同意我的 php 脚本反馈表示例(来自互联网):

 <?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>

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.希望你能得到 3 次 OKAY。

I'll ignore the security aspects of using raw data in a MySQL query like this.我将忽略在这样的 MySQL 查询中使用原始数据的安全方面。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM