【问题标题】:Save questions answer to mysql with post method使用 post 方法将问题答案保存到 mysql
【发布时间】:2014-08-06 22:27:56
【问题描述】:

每个问题都有 3 个答案,在数据库中分配的分数为 0、1 或 2。

如何保存每个带有问题 ID 的答案和带有名称和电子邮件的答案 ID? 使用此脚本显示的数据库中的问题行中大约有 50 个问题:

<?php
include "config.php";
$db = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// Testar uppkoppling:
if (mysqli_connect_errno()){
// Databaskoppling error
 exit("Couldn't connect to the database: ".mysqli_connect_error());
}
$result = mysqli_query($db,"SELECT * FROM que");

echo '<form action="taemot.php" method="post" id="MyForm">';
while($row = mysqli_fetch_array($result)) {
echo '' . $row['que_question'] . '
<input type="radio" name="' . $row['que_answer0'] . '" value="' . $row['que_answer0'] . '">
<input type="radio" name="' . $row['que_answer1'] . '" value="' . $row['que_answer1'] . '">
<input type="radio" name="' . $row['que_answer2'] . '" value="' . $row['que_answer2'] . '"><br>';

}
echo '  <input type="text" name="name" value="Namn"><br>
       <input type="text" name = "email" value="Epostadress"><br>
       <input type="submit" value="Submit"> </form>';

?> 

taemot.php

 <? 
$name=$_POST['name']; 
$email=$_POST['email'];

mysql_connect("XXXX", "XXXX", "XXXX") 
or die(mysql_error()); mysql_select_db("database") or die(mysql_error()); 

mysql_query("INSERT INTO `data` VALUES ('$name', '$email')"); 

Print "Your information has been successfully added to the database."; ?>

【问题讨论】:

    标签: php mysql forms http-post


    【解决方案1】:

    尝试输入名称为数组

    // Assign $i to define which question number is on
    <input type="radio" name="ans[$i]" value="'. $row['que_answer0'] .'">
    <input type="radio" name="ans[$i]" value="'. $row['que_answer1'] .'">
    <input type="radio" name="ans[$i]" value="'. $row['que_answer2'] .'">
    $i++;
    

    对于taemont.php

    $aryAns = $_POST['ans'];
    foreach( $aryAns AS $key => $value ) { //$key is the question number, $value is the question answers
      mysql_query("INSERT INTO data ....");
    }
    

    【讨论】:

      【解决方案2】:

      使用此解决方案,数据库中的 $key 行发布值 $i 而不是问题 ID? 这个解决方案如何处理多个问题,因为在以下情况下大约有 50 个问题:

      echo '' . $row['que_question'] . '
      

      目前 50 个问题中只有 1 个值是可点击的。

      看这张图就明白了:http://s29.postimg.org/d4llvjajr/questions.jpg

       $result = mysqli_query($db,"SELECT * FROM que");
      
       echo '<form action="taemot.php" method="post" id="MyForm">';
       while($row = mysqli_fetch_array($result)) {
        echo '' . $row['que_question'] . '
       <input type="radio" name="ans[$i]" value="'. $row['que_answer0'] .'">
       <input type="radio" name="ans[$i]" value="'. $row['que_answer1'] .'">
       <input type="radio" name="ans[$i]" value="'. $row['que_answer2'] .'">
       ';$i++;
      
        }
         echo '   <input type="text" name="name" value="Namn"><br>
           <input type="text" name = "email" value="Epostadress"><br>
           <input type="submit" value="Submit"> </form>';
      

      taemot.php:

      $aryAns = $_POST['ans'];
      foreach( $aryAns AS $key => $value ) { //$key is the question number, $value is the question answers
      mysql_query("INSERT INTO `data` VALUES ('$name', '$email', '$key', '$value')");
      }  
      
      Print "Your information has been successfully added to the database."; ?>
      

      【讨论】:

        猜你喜欢
        • 2018-07-22
        • 1970-01-01
        • 2012-05-08
        • 2016-12-23
        • 1970-01-01
        • 1970-01-01
        • 2020-12-22
        • 2021-07-24
        • 1970-01-01
        相关资源
        最近更新 更多