为什么我的变量不会传递到每个页面?腓

时间:2014-04-05 18:57:38

标签: php variables post session-variables

我无法通过几个页面传递我的变量“quiz_name”以进行测验。将“quiz_name”变量从question_menu.php作为$ _POST []发送到quiz_created.php以处理quiz_created.php之后,假设将变量发送回question_menu.php。我究竟做错了什么。说实话,我觉得我接近错了。

重述:

quiz_name =“如何评分篮子”,提交给question_menu.php作为$ _POST ['quiz_name']。然后提交给quiz_created.php作为$ _POST ['quiz_name'],然后提交回question_menu.php。

我将一个Variable quiz_name undefined错误发送回quesion_menu。

coach_quizzes.php

<head>
    <title>Your Quizzes</title>
</head>
<body>
    <h1> Current Quizzes </h1>
    <form name="submit_button" action="create-quiz.php">
        <input type="submit" value="Create Quiz">
    </form>
</body>
</html>

提交至 - &gt; create_quiz.php

<?php session_start()
?>
<head>
    <title>Your Quizzes</title>
</head>
<body>
    <h1> Enter Quiz Name </h1>
    <form action="questions_menu(test).php" method="post">
        <input type="text" name="quiz_name" maxlength="30" size="30">
        <input type="submit" value="Create Quiz">
    </form>
</body>
</html>

输入:“如何为篮子打分”然后提交到question_menu.php

<!DOCTYPE html> 
<html>
<head>
    <title>Add Question</title>
    <link rel="stylesheet" type="text/css" href="css/info_style.css" />
</head>
<body>
    <div id="main">
        <header>
            <div id="welcome">
                <h2>Prairie View A&amp;M University</h2>
            </div><!--close welcome-->              
        </header>   
    <div id="site_content">     
        <form enctype="multipart/form-data" method="post" action="quiz_created.php">
            <table border="0" name"form_table">
                <tr>
                    <td>Quiz Name</td>
                    <td><?php echo $_POST['quiz_name']?></td>
                </tr>
            <tr>
                    <td>Question</td>
                    <td><textarea name="description" rows="4" cols="50"></textarea></td>
                </tr>
                <tr>
                    <td>Option 1</td>
                    <td><input type="text" name="option1" maxlength="30" size="30"></td>
                </tr>
                <tr>
                    <td>Option 2</td>
                    <td> <input type="text" name="option2" maxlength="30" size="30"></td>
                </tr>
                <tr>
                    <td>Option 3</td>
                    <td><input type="text" name="option3" maxlength="30" size="30"></td>
                </tr>
                <tr>
                    <td>Answer</td>
                    <td>
                        <select name="dropdown">
                            <option value='option1'>Option 1</option>
                            <option value='option2'>Option 2</option>
                            <option value='option3'>Option 3</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>Image</td>
                    <td><input type="file" name="file" /><br />
                    <input type="hidden" name="MAX_FILE_SIZE" value="10000" /><br>
                </td>
            </tr>
            <tr>
                <td colspan="2"><p>
                    <input type="submit" value="Add Question">
                </p></td>
            </tr>
    </table> 
</form>    


    <?php
$username = "root";
$password = "";
$hostname = "localhost";
$database = "basketball_database";
$table = "coach_john";

$con = mysql_connect($hostname, $username, $password)
 or die("Unable to connect to MYsql");
 //echo "Connected to mysql<br>";

 mysql_select_db("$database")
 or die("Could not select Basketball_database");
 //echo "Connected to database";

 //update when update button pressed
 if(isset($_POST['update'])){
     $UpdateQuery = "UPDATE $table SET question_description='$_POST[description]', option_a='$_POST[option1]', option_b='$_POST[option2]', option_c='$_POST[option3]', answer='$_POST[dropdown]', question_id='$_POST[questionID]'  WHERE question_id='$_POST[hidden]'";

     mysql_query($UpdateQuery, $con);

 };//end of if statement

  //delete when delete button pressed
 if(isset($_POST['delete'])){
     $DeleteQuery = "DELETE FROM $table WHERE question_id='$_POST[hidden]'";

     mysql_query($DeleteQuery, $con);

 };//end of if statement



$mysql = "SELECT * FROM $table";



 $mydata = mysql_query($mysql,$con);



 //create table
 echo "<table border=1
 <tr>
 <th>Question ID</th>
 <th>Quiz Name</th>
 <th>Question Description</th>
 <th>Option 1</th>
 <th>Option 2</th>
 <th>Option 3</th>
 <th>Answer</th>
 <th>Picture</th>
 </tr>";

 //insert data into rows
 while($records = mysql_fetch_array($mydata)){
     echo "<form action=questions_menu(test).php method=post>";
     echo "<tr>";
     echo "<td>"."<input type=text name=questionID size=5 value=".$records['question_id']." </td>";
     echo "<td>"."<input type=text name=option1 size=18 value=".$records['quiz_name']." </td>";
     echo "<td>"."<textarea name=description rows=1 cols=25>".$records['question_description']."</textarea>"."</td>";
     echo "<td>"."<input type=text name=option1 size=18 value=".$records['option_a']." </td>";
     echo "<td>"."<input type=text name=option2 size=15 value=".$records['option_b']." </td>";
     echo "<td>"."<input type=text name=option3 size= 15 value=".$records['option_c']." </td>";
     echo "<td>"."<input type=text name=answer size=15 value=".$records['answer']." </td>";
     echo "<td>". $records['image'] ." </td>";

     echo "<td>"."<input type=hidden name=hidden value=".$records['question_id']." </td>";

     //update button
    echo "<td>"."<input type=submit name=update value=Update onclick='return confirm(\"Are you sure you want to update question?\")'>"." </td>";
     //delete button
     echo "<td>"."<input type=submit name=delete value=Delete onclick='return confirm(\"Are you sure you want to delete question?\")'>"." </td>";


     echo "</tr>";  

     echo "</form>";//end form

 } echo "</table>";

 mysql_close();
?>  <!-- End of php code-->

    </div><!--close site_content-->     

    <footer>
     <a href="test folder/index.html">Home</a> | <a href="test folder/ourwork.html">Photos</a> | <a href="test folder/testimonials.html">Videos</a> | <a href="test folder/projects.html">Schedule</a> | <a href="test folder/contact.html">Contact</a><br/><br/>

    </footer>   

  </div><!--close main-->


</body>
</html>

添加信息后,将submit value =“Add Question”信息发送到quiz_created.php:

<?php
$username = "root";
$password = "";
$hostname = "localhost";
$database = "basketball_database";
$table = "coach_john";

$con = mysql_connect($hostname, $username, $password)
 or die("Unable to connect to MYsql");
// echo "Connected to mysql<br>";

$db = mysql_select_db("$database")
 or die("Could not select Basketball_database");
 //echo "Connected to database";

 $mysql = "INSERT INTO $table(question_description, quiz_name, option_a, option_b, option_c, answer) VALUES('$_POST[description]','$_POST[quiz_name]','$_POST[option1]','$_POST[option2]','$_POST[option3]','$_POST[dropdown]')";

 if(!mysql_query($mysql))
 echo mysql_errno($con) . ": " . mysql_error($con) . "\n";
 //die("Disconnected");
 $quiz=$_POST['quiz_name'];

 //Upload images
 if($_POST)
{

if ($_FILES["file"]["error"] > 0)
{
// if there is error in file uploading 
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";

}
else
{
// check if file already exit in "images" folder.
if (file_exists("images/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{  //move_uploaded_file function will upload your image. 
if(move_uploaded_file($_FILES["file"]["tmp_name"],"uploaded/" . $_FILES["file"]["name"]))
{


// If file has uploaded successfully, store its name in data base
$image_loc=addslashes (getcwd()."/uploaded"."/".$_FILES["file"]["name"]);
$query_image = "insert into $table (image, img_location) values ('".$_FILES['file']['name']."', '$image_loc')";
if(mysql_query($query_image))
{
echo "Stored in: " . "uploaded/" . $_FILES["file"]["name"];
}
else
{
echo 'File name not stored in database';
}
}
}


}
}
//end of image upload

 mysql_close();

?>

<html>
<title>User Added</title>

<body>
<h2>
Question has been added!
</h2>
</body>
<form action="questions_menu(test).php" method="post">
<input type="submit" value="Add Another Question">
<input type="hidden" name="quiz_name" value='<?php echo $quiz;?>'>
</form>
</html>

然后最后发送回question_menu.php,但我得到一个Variable quiz_name undefined错误。 我希望我能得到帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

我发现很多问题,但有一个问题是你没有正确地转发你的php插件。

 echo "<td>"."<input type=text name=questionID size=5 value=".$records['question_id']." </td>";

应该是:

 echo '<td><input type="text" name="questionID" size="5" value="'.$records['question_id'].'" /></td>';

你现在拥有它的方式,你正在使用echo的引号而没有值=&#34;&#34;。在整个代码中都可以看到这一点。不知道你在哪里获得你的$ records数组,但是你要弄清楚。

这些行也存在问题。

 $UpdateQuery = "UPDATE $table SET question_description='$_POST[description]', option_a='$_POST[option1]', option_b='$_POST[option2]', option_c='$_POST[option3]', answer='$_POST[dropdown]', question_id='$_POST[questionID]'  WHERE question_id='$_POST[hidden]'";

一个主要问题是您尝试将原始POST数据设置到数据库中。这是一个巨大的安全漏洞。其他问题,如你的其他PHP,与引号和双引号,分号等有关...阅读语法。你的整个剧本基本上都是错的。