duandong9195 2013-01-15 13:45
浏览 68
已采纳

在另一个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 2013-01-15 13:56
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题