apply.html
<html>
<head>
<title>A new Application for a classroom</title>
</head><body>
<h1>CRMS - New classroom Entry</h1> <form action="insert_use.php" method="post">
<table border="0">
<tr>
<td>Classroom ID</td>
<td><input type="text" name="Cno" maxlength="13" size="13"></td>
</tr>
<tr>
<td>Course ID</td>
<td> <input type="text" name="CID" maxlength="30" size="30"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Register"></td>
</tr>
</table>
</form>
</body>
</html>
insert_use.php
<html>
<head>
<title>A new Application for a classroom</title>
</head>
<body>
<h1>CRMS - New classroom Entry Results</h1>
<?php
// create short variable names
$Cno=$_POST['Cno'];
$CID=$_POST['CID']; if (!$Cno || !$CID) {
echo "You have not entered all the required details.<br />"
."Please go back and try again.";
exit;
} if (!get_magic_quotes_gpc()) {
$Cno = addslashes($Cno);
$CID = addslashes($CID);
}$con = mysql_connect("localhost","root","");
//通过服务器locahost建立连接,用户名为root,无密码if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("crms", $con);
//31
$search_classroom = "SELECT * FROM classroom2 WHERE Cno = $Cno";
if (!mysql_query($search_classroom,$con))
{
die("The classroom does not exist.");
}
$search_student = "SELECT * FROM course2 WHERE CID = $CID";
if (!mysql_query($search_student,$con))
{
die("The student does not exist.");
}$sql="INSERT INTO use2 (Cno,CID)
VALUES ('$Cno','$CID')";//增加操作if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}echo "1 record added";mysql_close($con)
?>
</body>
</html>数据库crms中classroom2表Cno = D301的数据,在apply.html中输入Cno = D301时显示没有课室存在,但是输入Cno = 301时久成功增加,为什么啊?
<html>
<head>
<title>A new Application for a classroom</title>
</head><body>
<h1>CRMS - New classroom Entry</h1> <form action="insert_use.php" method="post">
<table border="0">
<tr>
<td>Classroom ID</td>
<td><input type="text" name="Cno" maxlength="13" size="13"></td>
</tr>
<tr>
<td>Course ID</td>
<td> <input type="text" name="CID" maxlength="30" size="30"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Register"></td>
</tr>
</table>
</form>
</body>
</html>
insert_use.php
<html>
<head>
<title>A new Application for a classroom</title>
</head>
<body>
<h1>CRMS - New classroom Entry Results</h1>
<?php
// create short variable names
$Cno=$_POST['Cno'];
$CID=$_POST['CID']; if (!$Cno || !$CID) {
echo "You have not entered all the required details.<br />"
."Please go back and try again.";
exit;
} if (!get_magic_quotes_gpc()) {
$Cno = addslashes($Cno);
$CID = addslashes($CID);
}$con = mysql_connect("localhost","root","");
//通过服务器locahost建立连接,用户名为root,无密码if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("crms", $con);
//31
$search_classroom = "SELECT * FROM classroom2 WHERE Cno = $Cno";
if (!mysql_query($search_classroom,$con))
{
die("The classroom does not exist.");
}
$search_student = "SELECT * FROM course2 WHERE CID = $CID";
if (!mysql_query($search_student,$con))
{
die("The student does not exist.");
}$sql="INSERT INTO use2 (Cno,CID)
VALUES ('$Cno','$CID')";//增加操作if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}echo "1 record added";mysql_close($con)
?>
</body>
</html>数据库crms中classroom2表Cno = D301的数据,在apply.html中输入Cno = D301时显示没有课室存在,但是输入Cno = 301时久成功增加,为什么啊?
$search_classroom = "SELECT * FROM classroom2 WHERE Cno = '{$Cno}'";
这样改的话,无论是D301还是301都成功,我想让它如果是301就失败,应该怎么改啊?
你的判断方法有问题。$search_classroom = "SELECT * FROM classroom2 WHERE Cno = '{$Cno}'";
//if (!mysql_query($search_classroom,$con)) die("The classroom does not exist."); //只有查询失败时,mysql_query()才会返回false
//但上面的SQL查询总是能够成功执行的,只不过表中符合查询条件Cno = '301'的数据记录时,查询到的结果是空的,但查询操作本身是执行成功的//你要做的是判断结果集中的记录数是否为0
$result = mysql_query($search_classroom, $con);
if (mysql_num_rows($result) < 1) echo 'The classroom does not exist.';