PNewCode Posted December 5, 2023 Share Posted December 5, 2023 Hello. It's been a while since I've been here and I hope everyone is doing well. My task that I'm taking on is adding a value to all entries based on username. I added the new column and need to add that in for a different project. But I haven't started the other one yet because I need to get this done first. I tried to google this and I keep getting results that aren't what I need (IE How to insert multiple rows, or how to replace values in phpmyadmin, etc) To be more specific, lets say user "joe" has 200 entries. Joe is a user that has a reqid that is the number 75 Now I've added the column "reqid" I need to add the number 75 in the column reqid, to all the entries made be Joe The column is INT(255) Note: Each user has a different name, and I already have a working way to add the reqid to the database from this point on. So what I need to do is edit all the past entries before today with their reqid's I don't have anything that I have tried because I'm running in circles on what to do. If anyone has an example script that will get me in the right direction, or a link where I can see how this is done, I would appreciate it so very much. Note 2: Ultimately I would like to make a form where I can just search a name and add that in for all of that users entries, but if this can be done in phpmyadmin in a batch update that will work too (the search doesn't seem to allow to replace just one user to change (example 0 to 75) Quote Link to comment https://forums.phpfreaks.com/topic/317509-inserting-multiple-values-for-one-user-in-new-column/ Share on other sites More sharing options...
Solution kicken Posted December 5, 2023 Solution Share Posted December 5, 2023 Sounds like you just want an update query. For example update users_table set reqid=75 where username='joe' If that's not what you want, then you may need to try and explain again differently. Quote Link to comment https://forums.phpfreaks.com/topic/317509-inserting-multiple-values-for-one-user-in-new-column/#findComment-1613328 Share on other sites More sharing options...
PNewCode Posted December 5, 2023 Author Share Posted December 5, 2023 (edited) 19 minutes ago, kicken said: update users_table set reqid=75 where username='joe' This looks like what I need. So below is what I made, though all of the colums with the name JOE still shows zero. I'll add a screen shot Note: Joe was just an example. In the screen shot below, I'm attempting to update all that has the name "MetalHead" with "75" <?php error_reporting(E_ALL); ini_set('display_errors', '1'); $servername = "removed for posting"; $username = "removed for posting"; $password = "removed for posting"; $dbname = "removed for posting"; $sql = "SELECT * FROM nametable"; $sql= "UPDATE nametable SET reqid= '75' WHERE name='MetalHead' "; ?> The screen shot of the DB EDIT: No errors are showing on the screen Edited December 5, 2023 by PNewCode Quote Link to comment https://forums.phpfreaks.com/topic/317509-inserting-multiple-values-for-one-user-in-new-column/#findComment-1613329 Share on other sites More sharing options...
PNewCode Posted December 5, 2023 Author Share Posted December 5, 2023 @kicken WOW I can't believe I didn't make the DBconnection hahaha. Okay, so with your answer and adding the DB connection it works. Thanky you so much! // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "UPDATE nametable SET reqid='75' WHERE name='MetalHead'"; if ($conn->query($sql) === TRUE) { echo "Record updated successfully"; } else { echo "Error updating record: " . $conn->error; } $conn->close(); Quote Link to comment https://forums.phpfreaks.com/topic/317509-inserting-multiple-values-for-one-user-in-new-column/#findComment-1613331 Share on other sites More sharing options...
Barand Posted December 5, 2023 Share Posted December 5, 2023 The correct way to do it is use a JOIN in a query when you need the reqid, not to duplicate values from one table into another. 1 Quote Link to comment https://forums.phpfreaks.com/topic/317509-inserting-multiple-values-for-one-user-in-new-column/#findComment-1613335 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.