0

I have a code based website in which an employee has to update their reward points by the coupon code provides them and when that code reflect their account means when points are updated in their account they are able to shop in the website. But there is a restriction for the code that code is deleted once used. Sometimes I found a query from customers that they update their account with the code provided but code did not reflect the account and deleted from the database and so thereafter they are not able to use the code again now I want that code only deleted when the code update points in their account. I have an another table named customer_reward where code saved after add points in the customers account but the code that not reflect account recharge is not saved in that table so I want that code only delete when that code is saved in the customer_reward table.

the complete code is given below:

<?php
if(isset($_POST['sub'])){
    $db_host="localhost";
    $db_username="root";
    $db_password="";
    $db_name="14";
    $con=mysql_connect("$db_host", "$db_username", "$db_password") or die("could not connect to mysql!!!");
    if($con=="")
    {
        echo "Database not connected!!!!";
    }
    else
    {
        $isdb=mysql_select_db("$db_name") or die("database not available!!!!");
        if($isdb=="")
        {
            echo "database not selected!!!!";
        }
        else
        {   
            $emp_ID=$_POST['emp_ID'];
            $code=$_POST['code'];

            $query = mysql_query("select * from oc_abhireward where `Code`='$code'") or die (mysql_error());
            $data=mysql_fetch_assoc($query);
            $code_db=$data['Code'];
            $points_db=$data['Point'];
            if($code==$code_db)
            {
            $query1 = mysql_query("select * from oc_customer where `emp_ID`='$emp_ID'") or die (mysql_error());
            $data1=mysql_fetch_assoc($query1);
            $customer_id=$data1['customer_id'];
            $query2=mysql_query("INSERT INTO `oc_customer_reward` (customer_id, order_id, description, Code, points, date_added) VALUES ($customer_id, 0, 'rewarded',  '$code', $points_db, NOW());");
            $query4=mysql_query("INSERT INTO `oc_customer_recharge`(emp_ID, Code, points, date_added) VALUES ('$emp_ID', '$code', $points_db, NOW());");
                    if ($code==$code_db)
                    {
                    query5 = mysql_query("select * from oc_customer_recharge where Code='$code'")or die (mysql_error());
                    $data2=mysql_fetch_assoc($query4);
                    $emp_ID=$data2['emp_ID'];
                    $query6 = mysql_query("DELETE FROM oc_abhireward WHERE Code='$code'");
                    }
                    else
                    {
                    exit();
                    }

            header("location:http://localhost/14/index.php?route=account/account");
            exit();
            }
            else
            {

            }           
        }
    }
}
?>
Sandy
  • 1
  • 8
  • 1
    Your question seem easy, but reading your description is very complicated. Maybe if you separate in list using "-" or format better the code using ` on `tables` – Juan Carlos Oropeza Jul 25 '15 at 05:00

3 Answers3

0

I think the problem is that you are really not veryfing whether a record was inserted in your oc_customer_reward table.

There are multiple ways of solving this problem.

  1. You can modify your delete query to check oc_customer_reward table. This could be something on the lines of:

     DELETE Table1
     FROM Table1
     INNER JOIN Table2 ON Table1.ID = Table2.ID
    
  2. Create a trigger which will delete data in oc_reward table whenever a record is inserted in oc_customer_reward. You can look up triggers here

    CREATE TABLE reward_code_table
       (reward_code INT, random_col VARCHAR(50))
    ;
    
    INSERT INTO reward_code_table
       (`reward_code`, `random_col`)
    VALUES
       (1, 'First code'),
       (2, 'Second code'),
       (3, 'Third code')
    ;
    
    CREATE TABLE insert_code_table
        (customer_code INT, another_random_col VARCHAR(50))
    ;
    
    DELIMITER //
    CREATE TRIGGER del_after_insert
    AFTER INSERT
    ON insert_code_table
    FOR EACH ROW
    BEGIN
       DELETE FROM reward_code_table WHERE reward_code = NEW.customer_code;
    END;
    //
    DELIMITER ;
    
    INSERT INTO insert_code_table(customer_code, another_random_col)
    VALUES (2, "del 2 from reward table");
    

After inserting into one table, it deletes record from the other table.

You can checkout a sample SQLFiddle. Note that I have kept the delimiter as // in the fiddle example

Also consider using prepared statements to prevent basic mysql injections.

Codester
  • 193
  • 6
  • 2nd option is best suited to me but I am in learning of mysql, I don;t have a broad knowledge of MySql, it would be very helpful for me if you give me created trigger for the above code. It;s a humble request to you. – Sandy Jul 25 '15 at 05:18
  • $query3=mysql_query("DELETE from oc_abhireward WHERE Code='$code' after INSERT INTO oc_customer_reward (Code) VALUES ('$code')"); is this is the right sql command to delete the code from oc_abhireward only when code is inserted into oc_customer_reward. – Sandy Jul 25 '15 at 05:54
  • I have added the trigger code Sandy for your reference. – Codester Jul 25 '15 at 06:26
  • May I have to replace the complete code that I explain in the question asked by the code you provide me for creating trigger. – Sandy Jul 25 '15 at 06:38
  • Trigger code is not working in Opencart tpl file as it given error. Could you please give me all this execution with sql statements @Codester – Sandy Jul 25 '15 at 07:00
  • @Sandy, sorry for the late response. You can create the trigger directly in MySQL. Goto the mysql console and simply write the trigger statements, starting from DELIMITER // to DELIMITER ; replacing your correct table names and columns. – Codester Jul 26 '15 at 02:31
  • this code is giving error in mysql console the error is SQL query: CREATE TRIGGER del_after_insert AFTER INSERT ON oc.customer_reward FOR EACH ROW BEGIN DELETE FROM oc.abhireward WHERE code = $code; MySQL said: Documentation #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 6 how to fix this – Sandy Aug 05 '15 at 09:56
  • plz give me reply as soon as possible – Sandy Aug 05 '15 at 11:45
0

What's the relationship between Employee and Customer?

You are querying oc_customer by emp_ID, and getting customer_id from it. So is oc_customer unique on emp_id? If not, then the customer you end up getting (and so the one you'll apply the reward to) is effectively random. Instead, you need to pass in the customer's customer_id rather than the emp_id.

One other thing; You're using the "$POST"ed values directly in the SQL statements. That opens you up to a SQL-Injection attack. Check out How can I prevent SQL injection in PHP?

Community
  • 1
  • 1
racraman
  • 4,988
  • 1
  • 16
  • 16
  • yes emp_ID is stored in the database to allow specific user to use the portal and customer_id is generated when an employee register themselves using their emp_ID – Sandy Jul 25 '15 at 05:04
  • Now what I want you see in the last line of code $query3=mysql_query("DELETE FROM oc_reward WHERE Code='$code'"); so what I want that the code in oc_reward deleted after checking that it is stored in oc_customer_reward and if it is not stored the deletion canceled – Sandy Jul 25 '15 at 05:12
  • OK, so oc_customer is unique on emp_id. In that case, you need to confirm that the INSERT statement worked - either as Codester suggests, and/or checking if mysql_query returns false indicating failure (http://php.net/manual/en/function.mysql-query.php) – racraman Jul 25 '15 at 05:23
  • could someone can give me complete created trigger with the name of tables given in my code as suggested by the Codester. – Sandy Jul 25 '15 at 05:30
0

The best way possible here could be creating a trigger (for deleting the code) that will fired only when the update of points has been made.

CREATE TRIGGER trigger_name
AFTER INSERT 
   ON table_name FOR EACH ROW

BEGIN

   -- logic for deleting the corresponding CODE

END;

Hope this will bring you closer to what you seek.

Ak

akzhere
  • 323
  • 4
  • 11
  • You mean to say that CREATE TRIGGER trigger_name AFTER INSERT ON oc_customer_reward FOR EACH ROW BEGIN DELETE FROM oc_abhireward WHERE Code='$code'; END; but what is the trigger_name should be. Please let me know as soon as possible – Sandy Jul 25 '15 at 06:13
  • Trigger name can any valid database object name. You can give it anything like delete_code_on_update_tg. Also, you have to use the code value from the triggering insert statement. – akzhere Jul 25 '15 at 09:55
  • could someone can tell me how to add if else condition to delete data from oc_reward if it is insert into oc_customer_reward otherwise this is not delete from oc_reward – Sandy Aug 06 '15 at 03:40
  • you guys can see my edited code now, I have create a new table in the database where the code is saving when a customer use coupon code . Please help me in correcting the if statement as it have some mistakes in it. if the if else statement work as select the code from oc_customer_recharge when account recharge is done and delete it from oc_abhireward and if for some reason the code not recharge the account then it would not delete from the oc_abhireward table – Sandy Aug 06 '15 at 05:53
  • Now I am finding a different solution but dont know how to validate it. As in the code you can see that code is selected from oc_abhireward table and emp_ID is selected from oc_customer table and recharge the points into the customer account. How to match the entry of emp_ID from the data saved in oc_customer table at the time of registration – Sandy Feb 17 '16 at 08:57