Trouble in updating a record in mysql database -


below code:

<?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 database');      //check if redemption code exist     $exist = mysql_query("select * redemption red_code = '$code_input'");      //check if redemption code usable     $usable = mysql_query("select * redemption code_status = 'usable' , red_code = '$code_input'");      //check if users have card     $possess = mysql_query("select * customer customer join card card on customer.customer_id = card.customer_id join redemption redemption on card.merchant_id = redemption.merchant_id card.merchant_id = redemption.merchant_id , redemption.red_code = '$code_input'");      //check if reward name "reward point"     $point = mysql_query("select * redemption redemption join reward reward on redemption.merchant_id = reward.merchant_id reward.reward_name  '%point%' , redemption.red_code =  '$code_input'");     $data3 = mysql_fetch_array($point);      $customer = mysql_query("select * customer c_email = '$email_code'");     $data1 = mysql_fetch_array($customer);      $merchant = mysql_query("select * redemption red_code = '$code_input'");     $data2 = mysql_fetch_array($merchant);      $card = mysql_query("select redemption.total_point, card.card_id customer customer join card card on customer.customer_id = card.customer_id join redemption redemption on card.merchant_id = redemption.merchant_id 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 customer                               $create = mysql_query("insert card (card_id, chop_amt, customer_id, merchant_id) values ('', '0', '".$data1["customer_id"]."', '".$data2["merchant_id"]."')");                  if(mysql_num_rows($point) == 1){                     //update chop amount in card details                     $update1 = mysql_query("update card set chop_amt = '".$data3["total_point"]."' customer_id = '".$data1["customer_id"]."' , 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"]."' red_code = '$code_input'");                      $response["success"] = 1;                     $response["message"] = "code redeemed!";                      echo json_encode($response);                 } else {                     $response["success"] = 0;                     $response["message"] = "you not have enough point use code!";                      echo json_encode($response);                 }             }         } else {             //error non-usable code             $response["success"] = 0;             $response["message"] = "code not usable!";              echo json_encode($response);         }     } else {         //error non existing code         $response["success"] = 0;         $response["message"] = "code not exist!";          echo json_encode($response);     } } else {     //error blank field     $response["success"] = 0;     $response["message"] = "please fill in code field!";      echo json_encode($response); } ?> 

my situation want system create new record in "card" if don't have 1 , update "redemption" table accordingly..

however, managed create new card not able update "redemption" table...can me? please tell me thing need examine this...thanks!

i have tried

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

at separate php file , can data want...however dun understand why not updating ><

without debugging code - stepping through - can't figure out what's going on, way code structured makes hard follow logic. single sql query not doing expect cause fail silently, , there large number of nested conditionals make hard keep what's going on.

i feeling can write update more efficiently - you're grabbing data php variables other queries, , passing them update, , can joining data in update statement instead.

secondly, please consider "breaking early". instance:

if ($_post['code_input'] == ''){     //error blank field     $response["success"] = 0;     $response["message"] = "please fill in code field!";      die json_encode($response); } 

this puts error you're sending after validation step, rather right @ other end of codefile.

next, consider factoring out validation/data retrieval steps functions of own. so, instead of code above, consider:

if (!isinputvalid($_post['code_input'])){     //error blank field     $response["success"] = 0;     $response["message"] = "please fill in code field!";      die json_encode($response); } function isinputvalid($input){     if ($input == ''){        return false;     }     return true;  } 

next, consider not relying on multiple mysql result sets , weird "return false or array" behaviour. consider creating variable called $totalpoints, rather $data3["total_point"].

try this, , i'm pretty sure bug become obvious...


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -