以下代码应该用于预定大学的讲座。当用户输入他们想要参加的讲座的详细信息时,代码会执行所有查询(2)并发布到数据库。
声明&#34; if ($totalrows<=5)
&#34;没有工作(没有错误),这应该将预订的数量锁定为5以上的讲座,并且消息是回声。仍然是新手,并将感谢任何帮助。
<?php
session_start();
$usernm = "root";
$passwd = "";
$host = "localhost";
$database = "swift";
mysql_connect($host, $usernm, $passwd);
mysql_select_db($database);
$existingusername = $_SESSION['myssession'];
$Unit_id = $_POST['Unit_id'];
$User_name = $_POST['User_name'];
$Date = $_POST['Date'];
`
`
$Lecture_id = $_POST['Lecture_id'];
$Room_name = $_POST['Room_name'];
if ($Unit_id == "0001") {
$values = "SELECT * FROM lecturedetails WHERE Unit_id='0001'";
$result = mysql_query($values);
$totalrows = mysql_num_rows($result);
if ($totalrows <= 5) {
$savevalues = "INSERT INTO delegate_booking VALUES('$Unit_id','$User_name','$Date','$Lecture_id','$Room_name')";
$saveresult = mysql_query($savevalues);
} else {
echo "Bookings are full or late booking";
}
if ($Unit_id == "0002") {
$values = "SELECT * FROM lecturedetails WHERE Unit_id='0002'";
$result = mysql_query($values);
$totalrows = mysql_num_rows($result);
if ($totalrows <= 5) {
$savevalues = "INSERT INTO delegate_booking VALUES('$Unit_id','$User_name','$Date','$Lecture_id','$Room_name')";
$saveresult = mysql_query($savevalues);
} else {
echo "Bookings are full or late booking";
}
}
}
?>
<form action="LOGOUT.PHP">
<input type="submit" value="LOG OUT"/>
</form>
<tr>
<td><a href="Delegate.html">Back to Delegate member area</a></td>
</tr>
答案 0 :(得分:1)
您获得的示例包含重复的代码,不必要的变量检查(但也错过了输入验证(!)),嵌套被破坏,客户端库已经过时了。
以下是如何解决这些问题的大部分而不是缺少输入值检查的示例,因为没有关于您在那里使用的字符串格式的规范。
<?php
session_start();
$existingusername = $_SESSION['myssession'];
$Unit_id = $_POST['Unit_id'];
$User_name = $_POST['User_name'];
$Date = $_POST['Date'];
$Lecture_id = $_POST['Lecture_id'];
$Room_name = $_POST['Room_name'];
$pdo = new PDO(
'mysql:host=localhost;dbname=swift;charset=utf8', "root", "",
[PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);
$count = function ($Unit_id) use ($pdo) {
$stmt = $pdo->prepare('SELECT COUNT(*) FROM lecturedetails WHERE Unit_id = ?');
$stmt->execute([$Unit_id]);
return $stmt->fetchColumn();
};
$insert = function ($Unit_id, $User_name, $Date, $Lecture_id, $Room_name) use ($pdo) {
$insert = $pdo->prepare("INSERT INTO delegate_booking VALUES(?, ?, ?, ?, ?)");
$result = $insert->execute([$Unit_id, $User_name, $Date, $Lecture_id, $Room_name]);
};
if ($count($Unit_id) <= 5) {
$insert($Unit_id, $User_name, $Date, $Lecture_id, $Room_name);
} else {
echo "Bookings are full or late booking";
}
?>
<form action="LOGOUT.PHP">
<input type="submit" value="LOG OUT"/>
</form>
<tr>
<td><a href="Delegate.html">Back to Delegate member area</a></td>
</tr>
这对你有所帮助。如前所述,您检查错误表的行数的逻辑问题仍然没有修复。
答案 1 :(得分:0)
我认为你的括号错了。 if($Unit_id=="0002")
位于if($Unit_id=="0001")
内,不应该是正确的。
$ existingusername = $ _ SESSION [&#39; myssession&#39;];
$ UNIT_ID = $ _ POST [&#39; UNIT_ID&#39;]; $ USER_NAME = $ _ POST [&#39; USER_NAME&#39;]; $ Date = $ _ POST [&#39; Date&#39;];
$ Lecture_id = $ _ POST [&#39; Lecture_id&#39;]; $ ROOM_NAME = $ _ POST [&#39; ROOM_NAME&#39;];
if($Unit_id=="0001") { <-- start here
$ values =&#34; SELECT * FROM lecturedetails WHERE Unit_id =&#39; 0001&#39;&#34 ;; $结果= mysql_query($值); $ totalrows = mysql_num_rows($结果);
if ($totalrows<=5) { $savevalues= "INSERT INTO delegate_booking VALUES('$Unit_id','$User_name','$Date','$Lecture_id','$Room_name')"; $saveresult=mysql_query($savevalues); } else { echo "Bookings are full or late booking"; } if($Unit_id=="0002") { $values="SELECT * FROM lecturedetails WHERE Unit_id='0002'"; $result=mysql_query($values); $totalrows=mysql_num_rows($result); if ($totalrows<=5) { $savevalues= "INSERT INTO delegate_booking VALUES('$Unit_id','$User_name','$Date','$Lecture_id','$Room_name')"; $saveresult=mysql_query($savevalues); } else { echo "Bookings are full or late booking"; } } } <-- end here, why?
&GT;