2013-05-19 70 views
0

更新記錄是我的代碼:麻煩在mysql數據庫下面

<?php 
$response = array(); 

if ($_POST['code_input'] != ''){ 
    $code_input = $_POST['code_input']; 
    $email_code = $_POST['email_code']; 

    $link = mysql_connect('localhost','root','') or die ('Could not connect: '.mysql_error()); 
    mysql_select_db('ichop') or die ('Could not connect to database'); 

    //check if redemption code exist 
    $exist = mysql_query("select * from redemption where red_code = '$code_input'"); 

    //check if redemption code is usable 
    $usable = mysql_query("select * from redemption where code_status = 'usable' and red_code = '$code_input'"); 

    //check if users already have the card 
    $possess = mysql_query("select * from customer customer join card card on customer.customer_id = card.customer_id join redemption redemption on card.merchant_id = redemption.merchant_id where card.merchant_id = redemption.merchant_id and redemption.red_code = '$code_input'"); 

    //check if reward name is "reward point" 
    $point = mysql_query("SELECT * FROM redemption redemption JOIN reward reward ON redemption.merchant_id = reward.merchant_id WHERE reward.reward_name LIKE '%point%' AND redemption.red_code = '$code_input'"); 
    $data3 = mysql_fetch_array($point); 

    $customer = mysql_query("select * from customer where C_email = '$email_code'"); 
    $data1 = mysql_fetch_array($customer); 

    $merchant = mysql_query("select * from redemption where red_code = '$code_input'"); 
    $data2 = mysql_fetch_array($merchant); 

    $card = mysql_query("select redemption.Total_Point, card.card_id from customer customer join card card on customer.customer_id = card.customer_id join redemption redemption on card.merchant_id = redemption.merchant_id where redemption.red_code = '$code_input'"); 
    $data4 = mysql_fetch_array($card); 

    if(mysql_num_rows($exist) == 1){ 
     if(mysql_num_rows($usable) == 1){ 
      if(mysql_num_rows($possess) == 1){ 

      } else { 
       //create new card for customer    
       $create = mysql_query("INSERT INTO card (Card_ID, Chop_Amt, Customer_ID, Merchant_ID) VALUES ('', '0', '".$data1["Customer_ID"]."', '".$data2["Merchant_ID"]."')"); 

       if(mysql_num_rows($point) == 1){ 
        //update the chop amount in card details 
        $update1 = mysql_query("UPDATE card SET Chop_Amt = '".$data3["Total_Point"]."' where Customer_ID = '".$data1["Customer_ID"]."' and Merchant_ID = '".$data2["Merchant_ID"]."'"); 

        $update2 = mysql_query("UPDATE redemption SET Code_Status = 'Unusable', Red_Date = now(), Point_Balance = '".$data3["Total_Point"]."', Card_ID = '".$data4["Card_ID"]."' where red_code = '$code_input'"); 

        $response["success"] = 1; 
        $response["message"] = "Code redeemed!"; 

        echo json_encode($response); 
       } else { 
        $response["success"] = 0; 
        $response["message"] = "You do not have enough point to use the code!"; 

        echo json_encode($response); 
       } 
      } 
     } else { 
      //error for non-usable code 
      $response["success"] = 0; 
      $response["message"] = "Code is not usable!"; 

      echo json_encode($response); 
     } 
    } else { 
     //error for non existing code 
     $response["success"] = 0; 
     $response["message"] = "Code does not exist!"; 

     echo json_encode($response); 
    } 
} else { 
    //error for blank field 
    $response["success"] = 0; 
    $response["message"] = "Please fill in the code field!"; 

    echo json_encode($response); 
} 
?> 

我的情況是,我想我的系統在「一卡通」創建一個新的記錄,如果他們沒有1,然後更新「贖回」表相應..

但是,我只設法創建一個新的卡,但我無法更新「贖回」表...任何人都可以幫助我嗎?請告訴我任何你需要檢查的東西......謝謝!

我已經試過

$card = mysql_query("select redemption.Total_Point, card.card_id from customer customer 
join card card on customer.customer_id = card.customer_id 
join redemption redemption on card.merchant_id = redemption.merchant_id 
where redemption.red_code = '$code_input'"); 
$data4 = mysql_fetch_array($card); 

在一個單獨的PHP文件,我可以得到我想要的數據。不過我逼債瞭解它爲什麼不更新> <

+0

它進入塊? select語句是否返回預期值? – draxxxeus

+0

嘗試調試:'$ update2 = mysql_query(「UPDATE redemption SET Code_Status ='Unusable',Red_Date = now(),Point_Balance ='」。$ data3 [「Total_Point」]。'',C​​ard_ID ='「。$ data4 [「Card_ID」]。「'其中red_code ='$ code_input'」)或die(mysql_error());'也請記住'mysql_ *'函數已棄用,最好使用'mysqli'或'PDO' – Fabio

+0

@draxxxeus ,如果我手動控制更改值..它正在工作...我試圖運行另一個PHP文件中的選擇語句...它顯示錯誤和total_point –

回答

0

沒有調試代碼 - 通過它 - 我無法弄清楚發生了什麼,但代碼的結構使得很難遵循邏輯。一個單獨的SQL查詢沒有達到您期望的效果,可能會導致它無聲無息地失敗,並且存在大量嵌套條件,這使得很難跟上正在發生的事情。

我感覺你可以更有效地編寫更新 - 你從其他查詢中獲取數據到PHP變量中,並將它們傳遞到更新中,並且你可以通過在update語句中加入數據來做到這一點代替。

其次,請考慮「打破早」。例如:

if ($_POST['code_input'] == ''){ 
    //error for blank field 
    $response["success"] = 0; 
    $response["message"] = "Please fill in the code field!"; 

    die json_encode($response); 
} 

這會使您在驗證步驟後立即發回的錯誤,而不是在代碼文件的另一端。

接下來,考慮將所有這些驗證/數據檢索步驟分解爲它們自己的函數。 因此,而不是上面的代碼,可以考慮:

if (!isInputValid($_POST['code_input'])){ 
    //error for blank field 
    $response["success"] = 0; 
    $response["message"] = "Please fill in the code field!"; 

    die json_encode($response); 
} 
function isInputValid($input){ 
    if ($input == ''){ 
     return false; 
    } 
    return true; 
} 

其次,考慮不依賴於多個MySQL結果集,其怪異的「返回FALSE或數組」的行爲。考慮創建一個名爲$totalPoints的變量,而不是$data3["Total_Point"]

試試這個,我敢肯定的bug會變得顯而易見。

0

因爲他們不贊成你應該開始使用的,而不是PDO功能mysql_*。此外,您應該更仔細地處理這些查詢 - 我看到您幾次從同一個表中選擇幾乎相同的信息,但只是通過不同的列進行請求。例如,這些查詢$exist$usable可以合併爲一個查詢,然後您可以使用簡單的if/else語句檢查查詢結果。這將節省一些系統資源,並且會加快應用程序的運行速度。

此外,我不明白爲什麼你的sql查詢中使用表別名時,別名名稱本身是與表名相同?這些別名用於縮短表名(例如my_table_name變爲mtn,因爲寫起來更容易和更快),或者如果您要加入具有相同名稱但含義和用法不同的列的幾個表。

關於您編寫​​的代碼,正如@Neville K指出的那樣,要指出它的錯誤是非常困難的。你編寫它的方式不會使調試變得簡單。我花時間使用PDO重新組織代碼。代碼將最有可能不是立即工作 - 我沒有測試它,我沒有你的數據庫的結構。您可能需要做一些工作才能使其工作。我想建議你反對使用變量名稱,如data,data1,data2等。嘗試給變量一個有意義的名稱,並澄清它擁有的數據。

下面是代碼:

<?php 

$response = array(); 
$code_input = $_POST['code_input']; 
$email_code = $_POST['email_code']; 

if ($code_input != "" && $email_code != ""){ 

    // PDO link to database; 
    $host = 'localhost'; 
    $port = 3306; 
    $dbname = 'ichop'; 
    $dbuser = 'PUT_YOUR_DB_USER_HERE'; 
    $dbpass = 'PUT_YOUR_DB_USER_PASS_HERE'; 
    $connect_string = "mysql:host=".$host.";port=".$port.";dbname=".$dbname; 
    $db = new PDO($connect_string, $dbuser, $dbpass); 
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

    // Get the code from the database using a prepared statement (ps in the variables stands for Prepared Statement) 
    $rps = $db->prepare(' 
    SELECT * 
    FROM redemption 
    WHERE red_code = :code_input'); 
    // Bind a the value from $code_input to the :code_input variable in the sql code. 
    $rps->bindValue(':code_input', $code_input, PDO::PARAM_STR); // If the $code_input is an integer, use PDO::PARAM_INT 
    // Execute the query 
    $rps->execute(); 
    // Fetch the results 
    // - PDO::FETCH_ASSOC would return an associative array, containing the column names of the table 
    // i.e. 
    // array(
    //  'red_code' => 1234, 
    //  'usable' => true 
    //  .......... 
    // ) 
    // For more information visit http://www.php.net/manual/en/pdo.constants.php 
    $redemption_code = $rps->fetch(PDO::FETCH_ASSOC); 

    // Check if the code exists in the database. 
    if($redemption_code != ""){ 
     // Check if the code is usable 
     if($redemption_code['usable'] == 1 && $redemption_code['red_code'] == $code_input) { 
      //check if users already have the card 
      $pps = $db->prepare(' 
      SELECT * 
      FROM customer 
      JOIN card on customer.customer_id = card.customer_id 
      JOIN redemption redemption on card.merchant_id = redemption.merchant_id 
      WHERE card.merchant_id = redemption.merchant_id 
      AND redemption.red_code = :code_input'); 
      $pps->bindValue(':code_input', $code_input, PDO::PARAM_STR); 
      $pps->execute(); 
      $possessed = $pps->fetch(PDO::FETCH_ASSOC); 

      // This card haven't been used yet 
      if($possessed == ""){ 
       // check if reward name is "reward point" 
       // I believe this code can be merged with $redemption_code but I don't know your database structure so I'm leaving it as is. 
       $point_ps = $db->prepare(" 
       SELECT * 
       FROM redemption redemption 
       JOIN reward reward ON redemption.merchant_id = reward.merchant_id 
       WHERE reward.reward_name LIKE '%point%' 
       AND redemption.red_code = :code_input"); 
       $point_ps->bindValue(':code_input', $code_input, PDO::PARAM_STR); 
       $point_ps->execute(); 
       $point = $point_ps->fetch(PDO::FETCH_ASSOC); 

       // Please check if the column name "C_email" is with a capital C. Do the check for the column names in the other queries as well. 
       $customer_ps = $db->prepare('SELECT * FROM customer WHERE C_email'); 
       $customer_ps->bindValue(':email_code', PDO::PARAM_STR); 
       $customer_ps->execute(); 
       $customer = $customer_ps->fetch(PDO::FETCH_ASSOC); 

       // I've got no idea what this is. 
       $cdps = $db->prepare(" 
       SELECT 
        redemption.Total_Point, 
        card.card_id 
       FROM customer 
       JOIN card ON customer.customer_id = card.customer_id 
       JOIN redemption ON card.merchant_id = redemption.merchant_id 
       WHERE redemption.red_code = :code_input"); 
       $cdps->bindValue(':code_input', $code_input, PDO::PARAM_STR); 
       $card = $cdps->fetch(PDO::FETCH_ASSOC); 

       // Create new card for the customer 
       $insert_ps = $db->prepare("INSERT INTO card(Chop_Amt, Customer_ID, Merchant_ID) VALUES ('0', :customer_id, :merchant_id)"); 
       $insert_ps->bindValue(':customer_id', $customer["Customer_ID"], PDO::PARAM_INT); 
       $insert_ps->bindValue(':merchant_id', $redemption_code["Merchant_ID"], PDO::PARAM_INT); 
       $insert_ps->execute(); // This will return true on successful insert and false on unsuccessful. 

       if($insert_ps) { 
        // If, when executing the code, the redemption & card tables don't get updated 
        // you need to debug the $point variable - see if a record is being returned and 
        // if that's what you need. 
        if($point != ""){ 
         $card_update_ps = $db->prepare("UPDATE card SET Chop_Amt = :total_point WHERE Customer_ID = :customer_id AND Merchant_ID = merchant_id"); 
         $card_update_ps->bindValue(':customer_id', $customer["Customer_ID"], PDO::PARAM_INT); 
         $card_update_ps->bindValue(':merchant_id', $redemption_code["Merchant_ID"], PDO::PARAM_INT); 
         $card_update_ps->bindValue(':total_point', $point["Total_Point"], PDO::PARAM_INT); // I guess this is an integer? 
         $card_update_ps->execute(); 

         $redemption_update_ps = $db->prepare("UPDATE redemption SET Code_Status = 'Unusable', Red_Date = now(), Point_Balance = :total_point, Card_ID = :card_id WHERE red_code = :code_input"); 
         $redemption_update_ps->bindValue(':code_input', $code_input, PDO::PARAM_STR); 
         $redemption_update_ps->bindValue(':total_point', $point["Total_Point"], PDO::PARAM_INT); 
         $redemption_update_ps->bindValue(':card_id', $card['Card_ID'], PDO::PARAM_INT); 
         $redemption_update_ps->execute(); 

         $response["success"] = 1; 
         $response["message"] = "Code redeemed!"; 

         echo json_encode($response); 
        } else { 
         $response["success"] = 0; 
         $response["message"] = "You do not have enough point to use the code!"; 

         echo json_encode($response); 
        } 
       } 
       else { 
        // Print an error if you can't insert the card. 
       } 
      } 
      // This card was used 
      else { 
       // Print an error? 
      } 
     } 
     else { 
      //error for non-usable code 
      $response["success"] = 0; 
      $response["message"] = "Code is not usable!"; 
      echo json_encode($response); 
     } 
    } 
    // The redemption code does not exists 
    else { 
     //error for non existing code 
     $response["success"] = 0; 
     $response["message"] = "Code does not exist!"; 

     echo json_encode($response); 
    } 
} else { 
    //error for blank field 
    $response["success"] = 0; 
    $response["message"] = "Please fill in the code field!"; 

    echo json_encode($response); 
} 

?>