Php 如何将信息插入数据库

Php 如何将信息插入数据库,php,mysql,database,insert,Php,Mysql,Database,Insert,抱歉发了这么长的邮件。我使用index.html来处理quiz1.php,但是我无法使问题和复选框正常工作,也就是说,无法将index.html中的其他信息插入到数据库中。它应该插入index.html中的信息和quiz1.php中的信息。需要帮助才能让它工作 这就是我到目前为止所做的 表单(index.html)第一页(获取人员信息) 全名: 测验名称: 类名: 日期: 测验(quiz1.php)第二页(将index.html中的信息放在顶部) //测验信息 结果(insert.php

抱歉发了这么长的邮件。我使用index.html来处理quiz1.php,但是我无法使问题和复选框正常工作,也就是说,无法将index.html中的其他信息插入到数据库中。它应该插入index.html中的信息和quiz1.php中的信息。需要帮助才能让它工作

这就是我到目前为止所做的

表单(index.html)第一页(获取人员信息)


全名:
测验名称:
类名:
日期:
测验(quiz1.php)第二页(将index.html中的信息放在顶部)


//测验信息
结果(insert.php)第三页(将第一页和第二页的信息输入数据库)


在quiz1.php中试试这个

实际上,您的
在循环外,而
在循环内。这不是构造正确的HTML,很可能是这不起作用的原因

另外,您的第三个脚本在
$\u POST
数组中需要
问题
元素,该元素从未设置或发送过。使用隐藏字段可以纠正这种情况

$answerFields = array(
                    'aee' => 'aee', 
                    'bee' => 'bee', 
                    'cee' => 'cee', 
                    'dee' => 'dee'
                );

echo "<form action='insert.php' method='post'>";

while ($row = mysql_fetch_array($question2))
{

    $id         = $row['id'];
    $question   = $row['question'];


    echo "<input type='hidden' name='question' value='{$question}' />"

    // Print Question
    printf('<div id="ContainerQuestion">');
    printf('<span class="Question">%s. %s</span>', $id, $question);

    // Print Answers
    foreach ($answerFields as $field=>$ans)
    {
        if (array_key_exists($field, $row) && $row[$field])
        {
            $checked = ($row["quizAnswer"] == $ans) ? 'checked' : '';
            printf(
                '<p><input type="checkbox" name="%s" %s value="%s">%s</p>', 
                $id, 
                $checked, 
                $ans, 
                $row[$field]
            );
        }
    }

}

echo '<input type="submit" value="Submit Quiz">';
echo '</form>';
$answerFields=数组(
“aee”=>“aee”,
“蜜蜂”=>“蜜蜂”,
'cee'=>'cee',
“dee”=>“dee”
);
回声“;
while($row=mysql\u fetch\u数组($question2))
{
$id=$row['id'];
$question=$row['question'];
回声“”
//打印问题
printf(“”);
printf('%s.%s',$id,$question);
//打印答案
foreach($answerFields作为$field=>$ans)
{
如果(数组\键\存在($field,$row)&&$row[$field])
{
$checked=($row[“quizAnswer”]==$ans)?“checked”:“”;
printf(
“%s

”, $id, 美元支票, $ans, $row[$field] ); } } } 回声'; 回声';

最后,请尝试将代码更改为使用
PDO
mysqli
。mysql*函数已被弃用,不应使用。本网站提供了大量的教程和资源,可以帮助您解决这一问题。

您说“需要帮助才能实现这一目标”,但没有说明它的作用。您必须告诉我们您预期会发生什么,实际会发生什么,以及您是如何知道的。@Andy Lester抱歉,当我在index.html中键入信息时,您的权利会很好地显示在quiz1.php上。当我参加测验并点击submit时,信息从insert.php提交到数据库,您将看到这样一条消息“谢谢!您的测验已插入”,但数据库表结果为空。此外,代码对SQL注入开放。有使用PHP进行SQL插入的正确方法的示例。@Nikolaos Dimopoulos数据库中唯一的东西是这个{$question}。此外,您给我的循环表单代码会在每个问题后显示“提交测验”按钮。@AgustinLeyva查看更改后的代码。我将表单元素和提交按钮移到了循环之外。这将使您的提交仅显示一次,但如果您的循环用于多个项目,它将打印相同的元素。这意味着它将为一个问题打印元素
bee
cee
等,并为第二个或第三个问题继续使用相同名称的循环元素。您可以将上述内容用于每页一个问题,或者您需要更改复选框的名称,以确保提交数据的唯一性。@NikolaosDimopoulos我如何对其进行编码,以便它可以执行您所说的“每页一个问题”@Agustineyva这将需要相当多的工作,并检查你一直在问哪个问题,但这是可以做到的。因此,您加载测验并将每个问题作为一页处理。您可以在数据库中添加一个
页面
字段来满足这一需求。然后,当页面加载时,只需打印该页面的问题(并更改查询以获得该页面)。检查传递的变量,如
quiz1.php?p=1
,然后在脚本
$\u GET['p']
中删除循环。或者,您需要为每个问题和答案创建唯一的HTML元素。
<?php
// info from index.html
$_SESSION['full_name']  = $_POST['full_name'];
$_SESSION['quiz_name']  = $_POST['quiz_name'];
$_SESSION['class_name'] = $_POST['class_name'];
$_SESSION['quiz_taken'] = $_POST['quiz_taken'];

echo $_SESSION['full_name'];
echo "<br />";
echo $_SESSION['quiz_name'];
echo "<br />";
echo $_SESSION['class_name'];
echo "<br />";
echo $_SESSION['quiz_taken'];
echo "<br />";
echo "<br />";

?>

// quiz info

<?php

$db_host = "localhost"; 
// Place the username for the MySQL database here 
$db_username = "root";  
// Place the password for the MySQL database here 
$db_pass = "";  
// Place the name for the MySQL database here 
$db_name = "test"; 

// Run the actual connection here  
mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysql_select_db("$db_name") or die ("no database");

//retreive questions from database and put into question box
$query2 = "SELECT `id`, `question`, `aee`, `bee`, "
        . "`cee`, `dee`, `quizAnswer` FROM `quiz1question`";

$question2    = mysql_query($query2);
$answerFields = array(
                    'aee'=>'aee', 
                    'bee'=>'bee', 
                    'cee'=>'cee', 
                    'dee'=>'dee'
                );

while ($row = mysql_fetch_array($question2))
{

    $id         = $row['id'];
    $question   = $row['question'];

    echo '<form action="insert.php" method="post">';

    // Print Question
    printf('<div id="ContainerQuestion">');
    printf('<span class="Question">%s. %s</span>', $id, $question);

    // Print Answers
    foreach ($answerFields as $field=>$ans)
    {
        if (array_key_exists($field, $row) && $row[$field])
        {
            $checked = ($row["quizAnswer"] == $ans) ? 'checked' : '';
            printf(
                '<p><input type="checkbox" name="%s" %s value="%s">%s</p>', 
                $id, 
                $checked, 
                $ans, 
                $row[$field]
            );
        }
    }
}

 echo '<input type="submit" value="Submit Quiz"></form>';

?>
<?php 

$localhost = "localhost";
$username  = "root";
$password  = "";
$database  = "test";
$table     = "quiz_results";

mysql_connect("$localhost","$username","$password") or die(mysql_error());
mysql_select_db("$database") or die(mysql_error());

// question & answers
$mysql1 = "INSERT INTO $table (question, aee, bee, cee, dee) "
        . "VALUES ('$_POST[question]','$_POST[aee]',"
        . "'$_POST[bee]','$_POST[cee]','$_POST[dee]')";

if(!mysql_query($mysql1)) 
{
    die(mysql_error());
}

// insert Name, quiz name, class name, and quiz taken 
$mysql = "INSERT INTO $table (full_name, quiz_name, class_name, quiz_taken) "
       . "VALUES ('$_POST[full_name]','$_POST[quiz_name]',"
       . "'$_POST[class_name]','$_POST[quiz_taken]')";

if(!mysql_query($mysql))
{
    die(mysql_error());
}

// echo
echo"Thank you!"; // mysql1
echo "<br />";
echo"Your Quiz has been Inserted"; // mysql

mysql_close();

?>
$answerFields = array(
                    'aee' => 'aee', 
                    'bee' => 'bee', 
                    'cee' => 'cee', 
                    'dee' => 'dee'
                );

echo "<form action='insert.php' method='post'>";

while ($row = mysql_fetch_array($question2))
{

    $id         = $row['id'];
    $question   = $row['question'];


    echo "<input type='hidden' name='question' value='{$question}' />"

    // Print Question
    printf('<div id="ContainerQuestion">');
    printf('<span class="Question">%s. %s</span>', $id, $question);

    // Print Answers
    foreach ($answerFields as $field=>$ans)
    {
        if (array_key_exists($field, $row) && $row[$field])
        {
            $checked = ($row["quizAnswer"] == $ans) ? 'checked' : '';
            printf(
                '<p><input type="checkbox" name="%s" %s value="%s">%s</p>', 
                $id, 
                $checked, 
                $ans, 
                $row[$field]
            );
        }
    }

}

echo '<input type="submit" value="Submit Quiz">';
echo '</form>';