@丫丫个呸 785002647@qq.com 谢谢!
我最近在看一本叫《由浅入深学PHP》的书,看到实例部分,是关于一个只能测试系统的,类似于爱情测试!
上面是两个数据库表,红色字体的或者加????的就是我看不懂的部分!!
代码是:
config.php:
<?php
$con=mysql_connect("localhost","root",""); //连接服务器
mysql_select_db("test"); //选择数据库
mysql_query("SET NAMES GB2312"); //设置编码
?>
creat_table.php:
<?php
include "config.php"; //调用配置文件
$sql1="CREATE TABLE question(
id int(6) not null primary key auto_increment,
f int(6) not null default 0,
t int(6) not null default 0,
content varchar(400) not null default '',
other varchar(60) not null default ''
)"; //创建建表语句1
$result1=mysql_query($sql1); //执行建表语句
if($result1) //如果成功执行操作
{
echo "成功创建问题表!"; //输出内容
}
else //如果执行失败
{
echo "创建问题表出现错误!"; //输出内容
}
echo "<p>";
$sql2="CREATE TABLE answer(
id int(6) not null primary key auto_increment,
f int(6) not null default 0,
t int(6) not null default 0,
content varchar(400) not null default '',
other varchar(60) not null default ''
)"; //创建建表语句2
$result2=mysql_query($sql2); //执行建表语句2
if($result2) //如果成功执行
{
echo "成功创建答案表!";
}
else
{
echo "创建答案表出现错误!";
}
mysql_close(); //关闭连接
?>
add_test.php:
<?php
echo "<center>";
if(!$_POST["test_name"])
{
echo "<h2>创建测试</h2>";
echo "<table border=1>";
echo "<form method=post action=".$_SERVER["PHP_SELF"].">";
echo "<tr>";
echo "<td>要创建测试题</td>";
echo "<td><input type=text name=test_name></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan=2><center><input type=submit value=下一步></center></td>";
echo "</tr>";
echo "</form>";
echo "</table>";
}
else
{
include "config.php";
$content=$_POST["test_name"];
$sql="INSERT INTO question(`content`) VALUES('$content')"; //这里是要插入到哪里呢??
if(mysql_query($sql))
{
$id=mysql_insert_id();
echo "成功创建测试内容:".$content;
echo "<p>";
echo "点<a href=add_question.php>这里</a>添加问题";
}
}
echo "<center>";
?>
add_question.php:
<?php
echo "<center>";
if(!$_POST["question_num"])
{
echo "<h2>创建问题</h2>";
echo "<table border=1>";
echo "<form method=post action=".$_SERVER["PHP_SELF"].">";
echo "<tr>";
echo "<td>要创建问题所属测试</td>";
echo "<td>";
echo "<select size=1 name=f>";
include "config.php";
$sql="SELECT `id`,`content` FROM question WHERE f=0";
$result=mysql_query($sql);
while($row=mysql_fetch_array($result))
{
echo "<option value=".$row["id"];
echo ">".$row[content];
echo "</option>";
}
echo "</select>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>要创建问题(包括最后结果)数量</td>";
echo "<td><input type=text name=question_num size=5></td>";
echo "</tr>";
echo "</tr>"; //这里是不是多了</tr>??
echo "<tr>";
echo "<td colspan=2><center><input type=submit value=下一步></center></td>";
echo "</tr>";
echo "</form>";
echo "</table>";
}
else if(!$_POST["question"])
{
echo "<h2>创建问题</h2>";
echo "<table border=1>";
echo "<form method=post action=".$_SERVER["PHP_SELF"].">";
$num=$_POST["question_num"];
echo "<input type=hidden name=question_num value=".$num.">";
echo "<input type=hidden name=f value=".$_POST["f"].">"; //这里完全不明白这两个隐藏表单的作用还有意思,可以详细的解答一下吗??
echo "<tr>";
echo "<td>问题内容</td>";
echo "<td>结果</td>";
echo "</tr>";
for($i=0;$i<$num;$i++)
{
echo "<tr>";
echo "<td>问题".($i+1).":<input type=text name=question[] size=40></td>";
echo "<td>";
echo "<select size=1 name=to[]>";
echo "<option value=0>不</option>";
echo "<option value=1>是</option>";
echo "</select>";
echo "</td>";
echo "</tr>";
}
echo "<tr>";
echo "<td><center><input type=submit value=提交></center></td>";
echo "</tr>";
echo "</form>";
echo "</table>";
}
else
{
$num=$_POST["question_num"];
$f=$_POST["f"];
$sql="INSERT INTO question(`f`,`t`,`content`) VALUES";
for($i=0;$i<$num;$i++)
{
$content=$_POST["question"][$i];
$t=$_POST["to"][$i];
$sql=$sql."('".$f."','".$t."','".$content."')";
if($i<$num-1) $sql=$sql.",";
}
include "config.php";
$re=mysql_query($sql);
if($re)
{
echo "成功创建测试所属问题:";
echo "<p>";
echo "点<a href=add_answer.php>这里</a>添加答案";
}
}
echo "<center>";
?>
add_answer.php:
<?php
echo "<center>";
if(!$_POST["f"])
{
echo "<h2>创建答案</h2>";
echo "<table border=1>";
echo "<form method=post action=".$_SERVER["PHP_SELF"].">";
echo "<tr>";
echo "<td>要创建答案所属测试</td>";
echo "<td>";
echo "<select size=1 name=f>";
include "config.php";
$sql="SELECT `id`,`content` FROM question WHERE f=0";
$result=mysql_query($sql);
while($row=mysql_fetch_array($result))
{
echo "<option value=".$row["id"];
echo ">".$row[content];
echo "</option>";
}
echo "</select>";
echo "</td>";
echo "</tr>";
echo "</tr>";
echo "<tr>";
echo "<td colspan=2><center><input type=submit value=下一步></center></td>";
echo "</tr>";
echo "</form>";
echo "</table>";
}
else if(!$_POST["content"])
{
$f=$_POST["f"];
include "config.php";
echo "<h2>创建答案</h2>";
$sql="SELECT `id`,`content` FROM question WHERE f='$f'";
$result=mysql_query($sql);
echo "问题ID及内容对照表:";
echo "<table border=1>";
echo "<tr><td>ID</td><td>内容</td></tr>";
$temp="<select size=1 name=t[]>";
while($row=mysql_fetch_array($result))
{
echo "<tr><td>".$row["id"]."</td><td>".$row["content"]."</td></tr>";
$temp=$temp."<option value=".$row["id"].">".$row["id"]."</option>";
}
echo "</table><br>";
$temp=$temp."</select>";
echo "<table border=1>";
echo "<form method=post action=".$_SERVER["PHP_SELF"].">";
$sql="SELECT `id`,`content` FROM question WHERE f='$f' and t=0";
$result=mysql_query($sql);
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td colspan=2><center>问题:".$row["content"]."</center></td>";
echo "</tr>";
echo "<input type=hidden name=f[] value=".$row["id"].">";
echo "<td>答案1<input type=text name=content[]>转到".$temp."</td>";
echo "<input type=hidden name=f[] value=".$row["id"].">";
echo "<td>答案2<input type=text name=content[]>转到".$temp."</td>";
echo "</tr>";
}
echo "<tr>";
echo "<td colspan=2><center><input type=submit value=提交></center></td>";
echo "</tr>";
echo "</form>";
echo "</table>";
}
else
{
$num=count($_POST["f"]);
$sql="INSERT INTO answer(`f`,`t`,`content`) VALUES";
for($i=0;$i<$num;$i++)
{
$f=$_POST["f"][$i];
$content=$_POST["content"][$i];
$t=$_POST["t"][$i];
$sql=$sql."('".$f."','".$t."','".$content."')";
if($i<$num-1) $sql=$sql.",";
}
include "config.php";
$re=mysql_query($sql);
if($re)
{
echo "成功创建测试所属答案!";
}
}
echo "<center>";
?>
show_test.php:
<?php
echo "<center>";
if(!$_GET["id"])
{
echo "没有提供ID";
}
else
{
echo "<table border=1>";
include "config.php";
$sql="SELECT content FROM question WHERE id='$_GET[id]'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
echo "<tr><td><h2>".$row["content"]."</h2></td></tr>";
if(!$_POST["t"])
{
$sql="SELECT * FROM question WHERE f='$_GET[id]' limit 1";
}
else
{
$sql="SELECT * FROM question WHERE f='$_GET[id]' and id='$_POST[t]'";
}
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
echo "<tr><td><h4>".$row["content"]."</h4></td></tr>";
if($row["t"]==0)
{
$sql="SELECT * FROM answer WHERE f='$row[id]'";
$result=mysql_query($sql);
echo "<form method=post action=".$_SERVER["PHP_SELF"]."?id=".$_GET["id"].">";
while($row=mysql_fetch_array($result))
{
echo "<tr><td>".$row["content"];
echo "<input type=radio name=t value=".$row["t"].">";
echo "</td></tr>";
}
echo "<tr>";
echo "<td><center><input type=submit value=下一步></center></td>";
echo "</tr>";
echo "</form>";
}
else
{
echo "<tr><td><a href=show_test.php?id=".$_GET["id"].">返回</a></td></tr>";
}
}
?>