在我正在建设的这个网站上,人们可以预留一些平板电脑。 它首先检查MySQL数据库以查看有多少平板电脑是免费的,因此不再保留平板电脑。
但是,在提交表单后,将检查最大安装量,并显示错误消息,但它们仍会添加到数据库中。
示例:只有10个iOS平板电脑是免费的,用户试图保留15个。该网站显示“太多iOS平板电脑,只有10个可用”,但仍然在数据库中插入15个。
更新:检查表单中输入了多少平板电脑并检查数据库的语句:
if($_POST['ios'] > $ios) {echo "<span class=\"error\">Too much iOS tablet.. Max is " . $ios . "</span><br />";}
if($_POST['android'] > $android) {echo "<span class=\"error\">Too much Android tablets. Max is " . $android . "</span><br />";}
if($_POST['windows'] > $windows) {echo "<span class=\"error\">Too much Windows tablets. Max is " . $windows . "</span><br />";}
所有其他错误消息都正常。
代码:
<?
//check reservations on date
$sql = "SELECT SUM(ios) as iostotal,SUM(android) as androidtotal,SUM(windows) as windowstotal FROM reservaties WHERE '$datum' = datum";
$check = mysqli_query($link,$sql) or die(mysql_error());
while ($free = mysqli_fetch_array($check)) {
$ios = 15 - $free['iostotal'];
$android = 15 - $free['androidtotal'];
$windows = 15 - $free['windowstotal'];
}
?>
<?php
if(isset($_POST['submit'])) {
if($_POST['ios'] == null) {$resios = 0;} else {$resios = $_POST['ios'];}
if($_POST['android'] == null) {$resand = 0;} else {$resand = $_POST['android'];}
if($_POST['windows'] == null) {$reswin = 0;} else {$reswin = $_POST['windows'];}
//Check for errors
if($_POST['naam'] == null) {echo "<span class=\"error\">Gelieve een naam in te vullen</span><br />";}
if($_POST['opleiding'] == 0) {echo "<span class=\"error\">Selecteer een opleiding</span><br />";}
//PROBLEM STARTS HERE
if($_POST['ios'] > $ios) {echo "<span class=\"error\">Aantal iOS tablets overschreden. Maximum " . $ios . " tablets beschikbaar.</span><br />";}
if($_POST['android'] > $android) {echo "<span class=\"error\">Aantal Android tablets overschreden. Maximum " . $android . " tablets beschikbaar.</span><br />";}
if($_POST['windows'] > $windows) {echo "<span class=\"error\">Aantal Windows tablets overschreden. Maximum " . $windows . " tablets beschikbaar.</span><br />";}
//PROBLEM STOPS HERE
if($_POST['terms'] != 'on') {echo "<span class=\"error\">Reglement moet aanvaard worden.</span><br />";}
if($resios == 0 && $resand == 0 && $reswin == 0) {echo "<span class=\"error\">Er moet minstens 1 tablet gereserveerd worden</span>";}
else {
$query = "INSERT INTO reservaties(oplid,naam,datum,ios,android,windows) VALUES ('" . $_POST['opleiding'] . "','" . $_POST['naam'] . "','" . $datum . "','" . $resios . "','" . $resand . "', '" . $reswin . "')";
if(mysqli_query($link,$query)) {
echo "<p class=\"succes\">U hebt succesvol " . $resios . " iOS-tablets, " . $resand . " Android-tablets en " . $reswin . " Windows-tablets gereserveerd op " . $datum . "</p>";
}
else {
echo "<p class=\"error\">Er is een fout opgetreden. Probeer opnieuw, of neem contact op met de <a href=\"mailto:mediatheek.kantienberg@arteveldehs.be\">Mediatheek</a>.</p>";}
}
}
?>
答案 0 :(得分:0)
“但是,在提交表单后,将检查最大金额,显示错误消息,但它们仍会添加到数据库中。”
根据您的问题,我已提及您的整个代码your problem is you are not stopping insertion of data while you are checking requested entries are less than of equal to
。你必须将插入查询输出到其他标准。