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条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站