2014-02-18 55 views
-3

在這個網站上,我正在建設,人們可以保留一些平板電腦。 它首先檢查MySQL數據庫以查看有多少個平板電腦可用,因此不會再有可能保留的平板電腦。如果語句被忽略 - php MySQL

但是,在提交表單後,檢查最大ammount,顯示錯誤消息,但它們仍被添加到數據庫中。

示例:只有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:[email protected]\">Mediatheek</a>.</p>";} 
    } 
} 
?> 
+0

你有百萬IF在你的代碼中哪一個被忽略?或者我們自己檢測到它? –

+0

,你說'錯誤消息顯示'爲什麼不發佈錯誤信息呢? –

+1

如果它是用英文寫成的,我會幫助你,但必須弄清楚你的代碼出了什麼問題,同時必須閱讀任何解釋它的語言,這是一個不錯的選擇。我也沒有看到IF來檢查天氣或者沒有想要的產品數量。 – Prix

回答

0

「不過,在表單提交後,最大量被選中,則顯示錯誤消息,但他們仍然被添加到數據庫中。」

根據你的問題,我已經提到你的整個代碼,your problem is you are not stopping insertion of data while you are checking requested entries are less than of equal to。您必須將插入查詢輸出到其他條件。

+0

感謝您的回答。將嘗試並實施此。 –

+0

現在做了一個快速的解決方案,通過設置數量保留到最大值,如果發佈的數量太多。但是肯定會使我的if語句更好,因爲現在非常混亂。 –