سؤال

I can't get my variable "quiz_name" passed along several pages in order to make a quiz. After the "quiz_name" variable is sent from question_menu.php as a $_POST[] to quiz_created.php for processing the quiz_created.php is suppose to send the variable back to question_menu.php. What am I doing wrong. To be honest I think I'm approaching it wrong.

Reworded:

quiz_name = "How to score a Basket" which is submitted to question_menu.php as a $_POST['quiz_name']. Then is submitted a to quiz_created.php as a $_POST['quiz_name'] and then submitted back to question_menu.php.

I get an Variable quiz_name undefined error after it is sent back to 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>

submit to -> 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>

Enter: "How to score a Basket" then submit to 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>

after the information is added submit value= "Add Question" information is sent to 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>

then finally sent back to question_menu.php but I get an Variable quiz_name undefined error. I hope I can get help. Thank You.

هل كانت مفيدة؟

المحلول

I see many problems, but one is that you're not escaping your php inserts properly.

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

should be:

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

The way you have it now, you are using the quotes for the echo and there are none for the value="". This is seen all throughout your code. Not sure where you are getting your $records array either, but that's for you to figure out.

These lines are problematic too.

 $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]'";

One major problem is you're trying to set raw POST data into a database. That is a HUGE security flaw. Other problems, like the rest of your php, have to do with quotes and double quotes, semicolons, etc... Read up on syntax. Your whole script is basically wrong.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top