简体   繁体   中英

Updating a sql database using php

What I am trying to do is updating multiple rows of mysql table having same id. I searched for many solutions but couldn't find appropriate one. I have a table that look likes below:

+----+--------+--------+--------------+----------+
| id | emp_id | job_id | basic_salary |   stat   |
+----+--------+--------+--------------+----------+
| 62 |    186 |     30 |         8400 | Active   |
| 64 |    110 |      8 |        12542 | Inactive |
| 65 |    110 |      5 |        12542 | Active   |
+----+--------+--------+--------------+----------+

So I want to update this table having id = 64 and emp_id = 110 as Active which should also update other rows with emp_id 110 as Inactive if they are Active so that only one record with emp id 110 remains active and rest inactive .

Will kindly appreciate the help.

Can try with these simple queries :-

UPDATE <table_name> SET `stat` = "Active" WHERE id = 64 AND emp_id = 110

and then :

UPDATE <table_name> SET `stat` = "Inactive" WHERE id <> 64 AND emp_id = 110

You can do like this

// first find all the records for the employee with status as Active and make them Inactive.
    $query1 = mysql_query("select * from table_employee where emp_id = 110 and status = 'Active' ");
    if($query1 && mysql_num_rows($query1) > 0)
    {
         while(mysql_fetch_array($query1))
         {
              mysql_query("update table_employee set status = 'Inactive' where emp_id = 110");
         }
    }

// then make the record Active that you you want to
$query2 = mysql_query("update table_employee set status = 'Active' where emp_id = 110 and id = 64");

One thing you could try is to store the active id in a boolean value (if you're altering the table scheme). Then you could check the active by searching employee ID and boolean value true.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM