NeonSteamPunk Posted September 6, 2018 Share Posted September 6, 2018 I am trying to mod a subscribe script for a build I am working on but am having trouble figuring out how to REPLACE or UPDATE a data entry.The script searches the db for a user key, if not found it INSERTS the data, if found it returns a message that the users already been subscibed. I would like to make it replace the users information with a updated value as this is being moded to make a game server data base .The hope was the spot for email would really be their score and when they gain a point the script sends the new score amount to the data base which should replace or update the information. below is the script im trying to mod. Any help would be great. <?php // We get the $connection variable from our config file include 'config.php'; //Check to see if a POST request has been submitted. if(!empty($_POST)) { //A POST request was submitted, so define the variables. // The users Key $userKey = $_POST['uid']; // The users name $name = $_POST['name']; // the users submitted email adress $email = $_POST['email']; // First we do a query on the table using their uid $query = "SELECT * FROM subscribe WHERE uid = '$userKey'"; $result = mysqli_query($connection, $query); // Here, we do a row check to see if they already exist $row_count = mysqli_num_rows($result); // if the do not currently exist if(!$row_count) { //Create the query and attempt to add the user. $sql = "INSERT INTO subscribe (name, uid, email) VALUES ('$name', '$userKey', '$email')"; $result = mysqli_query($connection, $sql); if($result) { //Tell the user they have been added. echo "Thank you $name, your subscription has been created.!"; } else { ((( THIS IS THE PART I LIKE TO CHANGE TO UPDATE THE USERS DATA NOT TELL THEM THEIR SUBSCRIBED. i like to keeo their name and user id but update the email row)) // Tell the user that there was an issue. // We do this in case there are any issues with the script or the data connection echo "Sorry, there seems to be an issue in getting you subscribed!\n"; } } else { // Tell the user that they are already subscribed $query = "SELECT * FROM subscribe WHERE uid = '$EMAIL'"; $replace = '$EMAIL'; echo "Hello $name, updated."; } } else { //A POST request was not submitted, so display an error. echo "Sorry! You're not allowed to view this page."; } // Now be a good developer and close the mysql connection mysqli_close($connection); ?> Link to comment Share on other sites More sharing options...
Wulfie Reanimator Posted September 6, 2018 Share Posted September 6, 2018 This forum is for LSL, you'd be better off posting PHP questions to StackOverflow or some other PHP-centric forum, but this should explain everything you need: https://www.w3schools.com/php/php_mysql_update.asp 1 Link to comment Share on other sites More sharing options...
Joeey Aura Posted September 6, 2018 Share Posted September 6, 2018 SQL has a very handy function called ON DUPLICATE KEY UPDATE it will insert into the database or if the key is already there it will update what you want it to update it would look like this $sql = "INSERT INTO subscribe (name, uid, email) VALUES ('$name', '$userKey', '$email') ON DUPLICATE KEY UPDATE name='$name', email='$email'"; you don't update the uuid of course as i assume that would be your primary key also note that no WHERE is needed with the UPDATE as it already knows the key to update from the INSERT 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Please take a moment to consider if this thread is worth bumping.
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now