Jump to content
NeonSteamPunk

PHP script help

Recommended Posts

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);

?>

Share this post


Link to post
Share on other sites

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

 

 

  • Like 1

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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...