duandong9195
duandong9195
2013-01-15 13:45

在另一个PHP文件中访问POST变量[重复]

Possible Duplicate:
Simplest method of passing one php variable in a url

I am making a quiz application. At the faculty end, teachers get to type the topic name (with which I will form a vanity URL), and type 3 questions with possible options including answer in one of them. Everytime a teacher creates a quiz, a new table will be created with the questions, options and answers everytime.

My db.php file:

<?php
mysql_connect("localhost","root","");
mysql_select_db("quiz");
?>

My faculty.php file:

<form action="fac_handler.php" method="POST">
Quiz Topic:<br><input type="text" name="topic"><br><br>Enter Questions:<br><br>
<?php
$a="a";
$b="b";
$c="c";
$d="d";
for ($i=1; $i<=3;$i++) 
{ 
?>
Question <?php echo $i?>:<br>
<textarea type="text" name="<?php echo "q".$i;?>"></textarea><br>
<br>
<input type="text" name="<?php echo $a.$i; ?>">&nbsp;&nbsp;
<input type="text" name="<?php echo $b.$i; ?>">&nbsp;&nbsp;
<input type="text" name="<?php echo $c.$i; ?>">&nbsp;&nbsp;
<input type="text" name="<?php echo $d.$i; ?>">&nbsp;&nbsp;<br><hr>
<?php
}
?>
<br>
<input type="submit" value="Submit">
</form>

My fac_handler.php file:

<?php
require "db.php";

if(isset($_POST['topic']) && !empty($_POST['topic']))
{
$topic=$_POST['topic'];

if(isset($_POST['q1'])&&isset($_POST['a1'])&&isset($_POST['b1'])&&isset($_POST['c1'])&&isset($_POST['d1'])) 
{
if(!empty($_POST['q1']) && !empty($_POST['a1']) && !empty($_POST['b1']) && !empty($_POST['c1']) && !empty($_POST['d1'])) 
{
$q1=$_POST['q1'];
$a1a=$_POST['a1'];
$a1b=$_POST['b1'];
$a1c=$_POST['c1'];
$a1d=$_POST['d1'];  
}
else
{
    echo "Please Set the Questions and the Options!";
    die();
}
} 
if(isset($_POST['q2'])&&isset($_POST['a2'])&&isset($_POST['b2'])&&isset($_POST['c2'])&&isset($_POST['d2']))
{
if(!empty($_POST['q2']) && !empty($_POST['a2']) && !empty($_POST['b2']) && !empty($_POST['c2']) && !empty($_POST['d2']))
{
$q2=$_POST['q2'];
$a2a=$_POST['a2'];
$a2b=$_POST['b2'];
$a2c=$_POST['c2'];
$a2d=$_POST['d2'];  
}
else
{
    echo "Please Set the Questions and the Options!";
    die();
}
}
if(isset($_POST['q3'])&&isset($_POST['a3'])&&isset($_POST['b3'])&&isset($_POST['c3'])&&isset($_POST['d3']))
{
if(!empty($_POST['q3']) && !empty($_POST['a3']) && !empty($_POST['b3']) && !empty($_POST['c3']) && !empty($_POST['d3']))
{
$q3=$_POST['q3'];
$a3a=$_POST['a3'];
$a3b=$_POST['b3'];
$a3c=$_POST['c3'];
$a3d=$_POST['d3'];  
}
else
{
    echo "Please Set the Questions and the Options!";
    die();
}
}  
$ans1=$a1a;
$ans2=$a2a;
$ans3=$a3a;

$qtbname="q_".$topic;
$top=$_SESSION['topic']=serialize($_POST['topic']);
$new="q_".$top;

$create_table="CREATE TABLE $qtbname(id int NOT NULL AUTO_INCREMENT,question VARCHAR(500),answer VARCHAR(30),optiona VARCHAR(30),optionb VARCHAR(30),optionc VARCHAR(30),optiond VARCHAR(30),PRIMARY KEY (id))";
mysql_query($create_table);

$query1="INSERT INTO $qtbname VALUES('','$q1','$ans1','$a1a','$a1b','$a1c','$a1d')";
$query2="INSERT INTO $qtbname VALUES('','$q2','$ans2','$a2a','$a2b','$a2c','$a2d')";
$query3="INSERT INTO $qtbname VALUES('','$q3','$ans3','$a3a','$a3b','$a3c','$a3d')";

$result1=mysql_query($query1);
$result2=mysql_query($query2);
$result3=mysql_query($query3);

header("Location: student.php");
}
else
{
    echo "Please Set the Quiz Topic";
}
?>

Now as you can see, after the table gets created dynamically everytime with a table prefix, it gets redirected to student.php file which looks like this.

My student.php file:

<?php
require "db.php";

$query="SELECT * FROM $new";
$result=mysql_query($query);
$n="1";
$i="a";

while($row=mysql_fetch_assoc($result))
{
echo "Question ".$n." : ".$row['question'];
echo "<br>";
$ansr=$row['answer'];
?>
<form action="std_handler.php" method="POST">
<input type="radio" name="<?php echo $i; ?>" value="<?php echo $row['optiona']; ?>"><?php echo $row['optiona']; ?>
<input type="radio" name="<?php echo $i; ?>" value="<?php echo $row['optionb']; ?>"><?php echo $row['optionb']; ?>
<input type="radio" name="<?php echo $i; ?>" value="<?php echo $row['optionc']; ?>"><?php echo $row['optionc']; ?>
<input type="radio" name="<?php echo $i; ?>" value="<?php echo $row['optiond']; ?>"><?php echo $row['optiond']; ?><br>
<?php
++$i;
++$n;
}
?>
<input type="submit" value="Submit">
</form>

The fourth line in this student.php file, the query, $query="SELECT * FROM $new";, I need to access the table which I just created at the faculty end to display the questions and answers in text and radio button format.

Here is my std_handler.php file:

<?php
if(isset($_POST['a']))
{
$ans=$_POST['a'];
echo $ans;
echo "<br>";
}
if(isset($_POST['b']))
{
$ans=$_POST['b'];
echo $ans;
echo "<br>";
}
if(isset($_POST['c']))
{
$ans=$_POST['c'];
echo $ans;
echo "<br>";
}
?>

In the faculty handler file, after the validations, I tried to use session and get the variable, I tried to make it global, I tried serializing it, I couldn't succeed to use and select the dynamic table name in the student.php page.

What I want is, as soon as I get redirected to the student.php page, I want to select the table instantly created earlier, how do I do that? How do I access that variable with which I created the table and executed the query at the faculty end?

I keep getting this error: Notice: Undefined variable: new in C:\xampp\htdocs\quiz\student.php on line 4

Please help me solve this problem. Thank you.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • doushui5587 doushui5587 8年前

    Do you want something like this:

    Bad solution, but you can see how to pass a variable

    In fac_handler.php:

    Change

    header("Location: student.php");
    

    to

    header("Location: student.php?url=". urlencode($new));
    

    On the student page:

    $query="SELECT * FROM " . mysql_real_escape_string($_GET["url"]);
    

    This is a method, but if I where you I would do this on an other way.

    How I would do this

    Create a ID that you send and resolve that name to the ride database name.

    点赞 评论 复制链接分享
  • dptn69182 dptn69182 8年前

    Okay, I solved this problem, this is what I did.

    I removed these two from the fac_handler.php page:

    $top=$_SESSION['topic']=serialize($_POST['topic']);
    $new="q_".$top;
    

    And few lined below, I changed the header location to:

    header("Location: student.php?url=".$qtbname); 
    

    And in the student.php page, I changed my query to:

    $query="SELECT * FROM " . mysql_real_escape_string($_GET["url"]);
    

    For the last two modifications above, I sincerely thank Lawrence. Also Thunda thanks a lot for giving me that idea to use the GET variable thing too.

    deceze and tim.baker (there in the comments): Thanks a lot for your advice too, now that I solved my problem, I will go ahead with normalization as you suggested.

    Thanks a lot stackoverflow, was struggling with this for a long time.

    点赞 评论 复制链接分享
  • donglu4633 donglu4633 8年前

    If I had to guess I would say this. When it gives you this error: Notice: Undefined variable: new in C:\xampp\htdocs\quiz\student.php on line 4 it's because it's just that. If we look at the code here:

    require "db.php";
    
    $query="SELECT * FROM $new";
    $result=mysql_query($query);
    $n="1";
    $i="a";
    

    this is your student.php ^^^

    You are not setting the value of $new.

    So you need to actually define the value.

    $new = $_GET['x'];
    $query="SELECT * FROM $new";
    $result=mysql_query($query);
    $n="1";
    $i="a";
    

    Where x is whatever you are passing as your query value.

    or something along those lines.

    点赞 评论 复制链接分享