tejas
tejas

Reputation: 27

Update multiple rows in same table in different row

I have to update multiple rows in same table in different row as below table in single query.

enter image description here

Queries:

update util SET util_value='$util_value_desc' where util_head ='wc_contact_us'

update util SET util_value='$util_value_email' where util_head ='wc_email'

update util SET util_value='$util_value_mobile' where util_head ='wc_mobile'

update util SET util_value='$util_value_map' where util_head ='wc_google_map'

also for select query to get data in textfield

$query=mysqli_query($connect,"SELECT util_value FROM util where util_head='wc_contact_us'");

                $row=mysqli_fetch_array($query);


                $util_value=$row['util_value'];
                $util_value_email=$row['util_value'];
                $util_value_map=$row['util_value'];
                $util_value_mobile=$row['util_value'];

Upvotes: 0

Views: 78

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521053

You could use an update with a CASE expression:

UPDATE util
SET util_value = CASE util_head WHEN 'wc_contact_us' THEN ?
                                WHEN 'wc_email' THEN ?
                                WHEN 'wc_mobile' THEN ?
                                WHEN 'wc_google_map' THEN ? END
WHERE
    util_head IN ('wc_contact_us', 'wc_email', 'wc_mobile', 'wc_google_map');

To the four placeholders ? you would bind the following four PHP variables:

$util_value_desc
$util_value_email
$util_value_mobile
$util_value_map

But this is a fairly verbose query, and you might want to stick with separate updates, maybe within a single transaction.

Upvotes: 2

Related Questions