如何将信息插入数据库

时间:2012-11-05 23:02:58

标签: php mysql database insert

对不起,很长的帖子。我得到了index.html来使用quiz1.php,但是我无法得到问题和复选框以正确工作意味着将其与index.html中的其他信息一起插入到数据库中。它应该插入index.html中的信息和quiz1.php中的信息。需要帮助才能实现这一目标。

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

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

<form action="quiz1.php" method="post">
    Full Name: <input type="text" name="full_name" />
    Quiz Name: <input type="text" name="quiz_name" />
    Class Name: <input type="text" name="class_name" />
    Date: <input type="text" name="quiz_taken" />
    <input type="submit" value="Submit" class="flashit">
</form>

测验(quiz1.php)第二页(将index.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>';

?>

results(insert.php)第三页(从第一页和第二页输入信息到数据库)

<?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();

?>

1 个答案:

答案 0 :(得分:0)

在你的quiz1.php中试试这个。

有效地,您的</form>位于循环之外,而<form...>位于循环内部。这不是构建正确的HTML,很可能是为什么这不起作用的原因。

此外,您的第三个脚本期望question数组中的$_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>';

最后请请尝试更改您的代码以使用PDOmysqli。 mysql *函数已被弃用,不应使用。这个网站上有很多教程和资源可以帮助你解决这个问题。