2

i have created a leaderboard for a website which displays users high scores for a game. but whe the user goes to edit their high score, it doesnt change in the database or on the screen. does anybody know how to update the database using a post method. my code is below.

require_once('../sokodatabase.php');

//require_once('../sokodatabase.php');
//require_once('../sokodatabase.php');
        if(isset($_POST['userId'])){
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
                $query = "
                UPDATE leaderboardhighscores
                SET highScores=".$_POST["highScores"].", rankNo=".$_POST["rankNo"]."
                WHERE userId=".$_POST["userId"];

            var_dump($_POST); 
            echo $query;

            @mysqli_query($dbc, $query);
        }
        }
$manager = new DatabaseManager;
$manager->SelectHighScores();
?>

<form method="post" action="highScores.php">
    high score <input type="text" name="highScores"/>
    rankNo <input type="text" name="rankNo"/>
    userId <input type="text" name="userId"/>
    <input type="submit" value="Submit">
</form>  
2
  • possible duplicate of PHP & MySQL does not update database Commented Jun 26, 2015 at 5:35
  • You getting anything in your $_POST array? Commented Jun 26, 2015 at 6:56

2 Answers 2

0

You have to provide attention to SQL injections!

Normally, you check for the submit button:

<input type="submit" name="submit" value="Submit">

Then

if(isset($_POST['userId'])){
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {

goes to:

if(isset($_POST['submit'])){

If your query does not work, you can make die($query) to see it and perform it via phpMyAdmin. Or you can use mysqli_error to display any occured error after executing it.

Please note, that with your code only numeric values are possible. If your fields are not numeric, you should use this:

$query = "
            UPDATE leaderboardhighscores
            SET highScores='".mysqli_real_escape_string($dbc, $_POST["highScores"])."', rankNo='".mysqli_real_escape_string($dbc, $_POST["rankNo"])."'
            WHERE userId=".intval($_POST["userId"]);
Sign up to request clarification or add additional context in comments.

Comments

0

Need name for the submit input type in-order to submit the form...

Like

 <input type="submit" name="userId" value="Submit">

 if(isset($_POST['userId']))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.