Jump to content

Recommended Posts

Is there a more efficient way of doing this?

 

$database->processQuery("UPDATE `users` SET `username` = ? WHERE `username` = ? LIMIT 1", array($_POST['new'], $_POST['old']), false);
$database->processQuery("UPDATE `posts` SET `username` = ? WHERE `username` = ? LIMIT 1", array($_POST['new'], $_POST['old']), false);
$database->processQuery("UPDATE `threads` SET `username` = ? WHERE `username` = ? LIMIT 1", array($_POST['new'], $_POST['old']), false);
$database->processQuery("UPDATE `messages` SET `creator` = ? WHERE `creator` = ? LIMIT 1", array($_POST['new'], $_POST['old']), false);
$database->processQuery("UPDATE `replies` SET `username` = ? WHERE `username` = ? LIMIT 1", array($_POST['new'], $_POST['old']), false);

Link to comment
https://forums.phpfreaks.com/topic/264860-shortening-all-these-queries/
Share on other sites

In a second thought...

 

what exactly the column 'username' (and creator) represent in each table?... is that a literal like 'johndoe'?  ... if that is the case, seems to me that you have a design problem with no normalized tables.

 

In a normalized model you should store in all those tables just a FK (foreign key) pointing to a record in other table holding that key and the username, therefore if the username need to be changed will be neccesary to update just one table... in your case, the users table should be the only one holding the username and the others the PK of the users table.

 

You users table should look like:

id        INT    (PK)

username VARCHAR

... others fields

 

and per example your table posts

id    INT  (PK)

userid  INT (FK to the users PK)

... others fields 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.