2013-04-24 81 views
0

我有一件很簡單的事情要到這裏去。基本查詢,但當我決定更進一步時,我向自己投擲了一條曲線。如何檢查訂單是否已放入

基本上它是這樣的:

  • 檢查,如果用戶在表單
  • 如果沒有輸入值,任意球開出,並顯示一個基本的錯誤
  • 如果他們再覈對該值該數據庫,以確保它是有效的/存在
  • 如果它是有效的/存在,設置會話變量,走訂單
  • 如果沒有,任意球開出,並顯示一個基本的錯誤

我想現在要做的是在那裏添加另一個檢查,如果用戶ID存在,那麼我需要檢查訂單狀態,如果他們已經訂購,那麼我想踢出並顯示一個簡單的消息讓他們知道他們已經下了訂單並正在處理。如果他們還沒有訂購,我想繼續按照上面的訂單。

數據庫有一個名爲「ordered」的字段,如果它們已經排序,那麼它有1;如果它們還沒有排序,則有0。

這裏是我的代碼工作,我已經試過幾件事情,但它一直吹起來:

<?php 
    session_start(); 
    $db_host = 'localhost'; 
    $db_username = 'xxxxxx'; 
    $db_password = 'xxxxxxxx'; 
    $db_name = 'xxxxxxxx'; 

    mysql_connect($db_host, $db_username, $db_password) or die(mysql_error()); 
    mysql_select_db($db_name); 

    if ($_SERVER['REQUEST_METHOD'] == "POST") { 
     /** Check whether the user has filled in the text field "employee_id" */ 
     if ($_POST['employee_id'] == "") { 
      $IdIsEmpty = true; 
     }else{ 
      $employee_id = $_POST['employee_id']; 
      if(mysql_num_rows(mysql_query("SELECT employee_id FROM TABLE_2   WHERE   employee_id = '$employee_id'"))){ 
       // if userid exists 
       $_SESSION['emp_id'] = $employee_id; 
       header('Location: orderform.php'); 
      exit; 
      } 
      $IdNotFound = true; 
     } 
    }?> 

    <head> 
    </head> 
    <body> 
    <b>Please enter your Employee ID: </b><br><br> 
    <form class="" action="index.php" method="post" enctype= 
    "multipart/form-data" name="test_form" id="test" accept-charset= 
    "utf-8"><input type="text" name="employee_id"> 
     <?php 
     /** Display error messages if "employee_id" field is empty or if ID does not   exist */ 
     if ($IdIsEmpty) { 
      echo ("<br>"); 
      echo ("<b>Enter your employee ID, please!</b>"); 
      echo ("<br>"); 
     }?> 
     <?php 
     /** Display error messages if "employee_id" field is empty or if ID does not exist */ 
     if ($IdNotFound) { 
      echo ("<br>"); 
      echo ("<b>Your employee ID not found!</b>"); 
      echo ("<br>"); 
     }?> 

     <input type="submit" value="Submit"> 
    </form> 
</body> 
</html> 
+1

停止()'!改用MySQLi/PDO。而對於你的問題,你可以添加更多的查詢來檢查。你有什麼困難? – Raptor 2013-04-24 02:13:34

回答

0

除了換出mysql_功能mysqli_像西瓦建議,你應該這樣做:

  • 轉義任何輸入 - 你可以從來沒有信託數據由用戶提供,所以做
    $employee_id = mysql_real_escape_string($_POST['employee_id']);
    如果它是一個數字,你也可以做
    $employee_id = intval($_POST['employee_id']);
    只要記住這一點,只要您使用的輸入。

  • 現在對於你的問題:

只需在同一查詢選擇ordered領域:採用`的mysql_query

$order_qry = mysql_query(" 
    SELECT employee_id, ordered 
    FROM TABLE_2 WHERE employee_id = '$employee_id' 
"); 
if(mysql_num_rows($order_qry)) { 
    $order = mysql_fetch_object($order_qry); 
    if(! $order->ordered) { // If not ordered 
     // ... do something 
    } else { // If already ordered 
    // ... tell the client 
    } 
} else { 
// No record found ... error message here 
} 
+0

謝謝盧卡斯!不得不做一些調整,並修復我所有的語法錯誤(請大括號大括號!)但是你的代碼工作得很完美。 – Tony 2013-04-24 15:47:43

相關問題