Luke G
Luke G

Reputation: 79

Adding updated data using a form to MySQLi

I am new to PHP and appreciate all the help I receive.

OK so I now understand that simply resubmitting a form does not over write any previous data on the database what was ending before resubmitting the for.

I was wondering how I would set it so that the database updates the data that needs updating with the new data.

My form works 100% fine other then a few things I would like to iron out and this is one of them. I have looked around google and everything but I can't make sense of anything that I find as all answers are different and there is not a set answer to my question.

if(isset($_POST["signupbtn"])) {
    if ($log_username) {
        /// getting data from submitted form into local variables
        $x = preg_replace('#[^a-z 0-9]#i', '', $_POST['xbox']);
        $p  = preg_replace('#[^a-z 0-9]#i', '', $_POST['psn']);
        $s = preg_replace('#[^a-z 0-9]#i', '', $_POST['steam']);
        $ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR'));
        // DUPLICATE DATA CHECKS FOR GAMER PROFILES
        $sqli = "SELECT username FROM player WHERE xbox='$x' LIMIT 1";
        $query = mysqli_query($db_conx, $sqli); 
        $x_check = mysqli_num_rows($query);
        // -------------------------------------------
        if ($x_check > 0){ 
            echo "Xbox Gamer-Tag already linked to a user on this website";
            exit();
        } else if (is_numeric($x[0])) {
            echo 'Xbox Gamer-Tag cannot begin with a number';
            exit();
        }
        $sqli = "SELECT username FROM player WHERE psn='$p' LIMIT 1";
        $query = mysqli_query($db_conx, $sqli); 
        $p_check = mysqli_num_rows($query);
        // -------------------------------------------
         if ($p_check > 0){ 
            echo "PSN User already linked to a user on this website";
            exit();
        } else if (is_numeric($p[0])) {
            echo 'PSN User cannot begin with a number';
            exit();
        }
        $sqli = "SELECT username FROM player WHERE steam='$s' LIMIT 1";
        $query = mysqli_query($db_conx, $sqli); 
        $s_check = mysqli_num_rows($query);
        // FORM DATA ERROR HANDLING
         if ($s_check > 0){ 
            echo "Steam account already linked to a user on this website";
            exit();
        } else if (is_numeric($s[0])) {
            echo 'Steam account cannot begin with a number';
            exit();
        } else  { $sqli = "INSERT INTO player (id, username, xbox, psn, steam, ip, created, lastupdated, notecheck)        
                VALUES ('$id','$log_username','$x','$p','$s','$ip',NOW(),NOW(),NOW())"; 
                $query = mysqli_query($db_conx, $sqli);
                   echo "Gamer Profiles Updated";
                }   

        exit();

            if (!file_exists("p_player/$log_username")) {
                mkdir("p_player/$log_username", 0755);
            } 


     }


}

As you can see that is my current php what is working fine except the fact that I can't update information that has been submitted.

Thank you for reading this and I hope someone is able to provide me with the knowledge that is need to preform this task.

Thanks once again for your time it has taken for you to read this post.

Upvotes: 0

Views: 201

Answers (2)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

I took this from one of my scripts, and is basically done like the following:

The $_POST['query_var']; would come from your form input, resembling:

<input type="text" name="query_var">

Then, the SQL part:

$con=mysqli_connect("xxx","xxx","xxx","xxx");
$query= mysqli_real_escape_string($con, $_POST['query_var']);
$query_check = $con->query("SELECT * FROM `your_table` WHERE `column_to_check` = '". $query ."' "); 

if (mysqli_num_rows($query_check) > 0) 
{ 
echo "Data is the same";
}

else{
// do something else
}

Upvotes: 0

newfurniturey
newfurniturey

Reputation: 38446

I'm not precisely sure "where" in your code you want to perform the actual update since you have several spots where it detects an existing account; however, the overall goal to be achieved can be done with a straightforward UPDATE clause.

Let's assume, for instance, you have the user's id that you want to update. Your MySQL query would then look like UPDATE player SET ... WHERE id = ?. If we want to port that to PHP with the variables you're using, you would use:

$stmt = $mysqli->prepare('
    UPDATE player
    SET xbox = ?, psn = ?, steam = ?, ip = ?, lastupdated = NOW()
    WHERE id = ?
');
$stmt->bind_param('ssssi', $x, $p, $s, $ip, $id);
$stmt->execute();
$stmt->close();

The above creates a prepared statement and binds all appropriate variables.

If you don't know a user's id but you want to update their info when, for instance, they submit a duplicate Steam account ($s), you could use something similar to:

$stmt = $mysqli->prepare('
    UPDATE player
    SET xbox = ?, psn = ?, ip = ?, lastupdated = NOW()
    WHERE steam = ?
');
$stmt->bind_param('ssss', $x, $p, $ip, $s);
$stmt->execute();
$stmt->close();

This is also portable for cases for both xbox and psn; however, I would recommend creating actual user-tracking so other users can't inadvertently overwrite a different user's info (if it will be public-facing, that is).

Upvotes: 1

Related Questions