Hey all,
I'm in the process of updating my site and need to update the users id# in one column. I have about 110 users, so i'm trying to get a php script to update them for me instead of running sql commands for each.
What I came up with so far is a combination of stuff I found googling this. I was wondering if someone could help me straighten out this code. Basically, I want to put in the users old user id along with their new one, hit update and have it search and replace the old with the new. I know I will have to do this for each user, but I don't mind that.
here is the code I put together so far.
Thanks for any help with this.
I'm in the process of updating my site and need to update the users id# in one column. I have about 110 users, so i'm trying to get a php script to update them for me instead of running sql commands for each.
What I came up with so far is a combination of stuff I found googling this. I was wondering if someone could help me straighten out this code. Basically, I want to put in the users old user id along with their new one, hit update and have it search and replace the old with the new. I know I will have to do this for each user, but I don't mind that.
here is the code I put together so far.
Code:
<?php
$hostname = "localhost"; // usually is localhost
$db_user = "******"; // change to your database password
$db_password = "******"; // change to your database password
$database = "*******"; // provide your database name
$db_table = "users"; // leave this as is
# STOP HERE
####################################################################
# THIS CODE IS USED TO CONNECT TO THE MYSQL DATABASE
$db = mysql_connect($hostname, $db_user, $db_password);
mysql_select_db($database,$db);
?>
<html>
<head>
<title>User Changer</title>
</head>
<body>
<?php
if (isset($_REQUEST['Submit'])) {
# THIS CODE TELL MYSQL TO INSERT THE DATA FROM THE FORM INTO YOUR MYSQL TABLE
$sql = "UPDATE $db_table
SET userid = REPLACE(userid, "old_user", "new_user")
WHERE column LIKE "%old_user%";";
if($result = mysql_query($sql ,$db)) {
echo '<h1>Thank you</h1>Your info has been changed in this database<br><br>';
} else {
echo "ERROR: ".mysql_error();
}
} else {
?>
<h1><hr>
<form method="post" action="">
Old User Number:<br>
<input type="text" name="old_user">
<br>
New user Number: <br>
<input type="text" name="new_user">
<br><br>
<input type="submit" name="Submit" value="Submit">
</form>
<?php
}
?>
</body>
</html>