gpt4 book ai didi

php - 用post方法将问题答案保存到mysql

转载 作者:行者123 更新时间:2023-11-29 12:48:01 25 4
gpt4 key购买 nike

每个问题都有 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."; ?>

最佳答案

尝试将输入名称设置为数组

// 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 ....");
}

关于php - 用post方法将问题答案保存到mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25171298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com