How to update two MySQL tables with PHP?

database, mysql, php

Solution

You could just run two queries changing both names:

<?php
mysql_query("UPDATE categories SET category_name='" . $catname . "' WHERE category_name='basic'");
mysql_query("UPDATE product SET category_name='" . $catname . "' WHERE category_name='basic'");
?>

If you want to do it in one string you can do this with multiple queries, a joined query or a stored procedure.

Multiple queries in one string:

<?php
    mysql_query("
        mySqlQuery(UPDATE categories SET category_name='" . $catname . "' WHERE category_name='basic';)
        mySqlQuery(UPDATE product SET category_name='" . $catname . "' WHERE category_name='basic';)
    ");
?>

Joined query:

<?php
mysql_query("
    UPDATE `product`,`categories`
    SET `product`.`category_name` = '" . $catname . "',
        `categories`.`category_name` = '" . $catname . "'
    WHERE `items`.`id` = '" . $catname . "'
");
?>

If none of the above work I suggest changing your tables so there isn't a matching name but a matching ID.

You'll get the following:

Categories Table -
id | category_name
1  : basic
2  : midlevel
3  : highlevel
4  : profesional

Product Table - 
id | category_ID | product
1  : 1           : adsl 1G
2  : 2           : adls 2G
3  : 3           : adsl 3G
4  : 3           : adsl 4G

Problem

I Have 2 tables in my db that each have a column named Catergory. ``` Categories Table - id | category_name 1 : basic 2 : midlevel 3 : highlevel 4 : profesional Product Table - id | product | category_name 1 : adsl 1G : basic 2 : adls 2G : midlevel 3 : adsl 3G : highlevel 4 : adsl 4G : profesional ``` When i update the *category_name* in the `categories` table i would like to change it in the `Products` table as well, but i am not sure on how to do this. I couldnt find and answer because i dont know how to phrase it correctly. Any help would be appreciated. What my update.php looks like now: ``` db_update("UPDATE category SET `category_name` = '".$_POST['category_name']."' WHERE category_id = '".$id."'"); ``` what is should look like:??? {code here} Note: "FIXED - I forgot to send my old value to the save.php file so the $id never knew what to update".

Original source