PHP + SQL UPDATE PROBLEM

Asked

Viewed 326 times

0

Good morning, you guys. I am making a simple quiz for my web class and I have a problem to update SQL via PHP. The database is set right, select works but the update is inert in the system. No error message and not working. Can someone give me a light please? And taking advantage, as I select the user id that is active in the session?

    // Exibe número de respostas certas e total de perguntas
    echo "<div id='results'>Você acertou $totalCorrect / 5</div>";

    // Faz os pontos serem multiplicados por 10
    $totalpoints = $totalCorrect * 10;

    // Exibe o total de pontos ganhos
    echo "<div id='results'>Você ganhou $totalpoints PONTOS</div>";

    // Seleciona o usuario e exibe o saldo de pontos          
    $pontos = mysql_query("SELECT points FROM users WHERE idu=1");
    $row = mysql_fetch_array($pontos);
    $result_pontos = $row['points'];

    // Exibe o total de pontos acumulados
    echo "<div id='results'>Você tem $result_pontos PONTOS ACUMULADOS</div>";

    // Soma pontos acumulados mais os adquiridos          
    $soma = $result_pontos + $totalpoints;

    // Adiciona os pontos no banco de dados
    $sql = ("UPDATE users SET points='$soma' WHERE idu=1") or die(mysql_error());
  • you could include the error message that is shown?

  • No error message. It loads the page and does not update.

2 answers

4


From what I checked in update you just added the $sql variable you need to use the mysql_query function for the procedure to take effect

$sql = mysql_query("UPDATE users SET points='$soma' WHERE idu=1") or die(mysql_error();
  • It worked now. Thank you

  • Flw, I’m glad I helped. The hardest mistakes to find are the subtle ones.

3

Nothing happens because the update was not executed in the database. Only a string with an sql command was assigned to the variable $sql, you need to execute this instruction with mysql_query.

$sql = "UPDATE users SET points='$soma' WHERE idu=1");
mysql_query($sql) or die(mysql_error());

The mysql_* functions have already been deprecated and deprecated, if this is a new project, I suggest you use mysqli or PDO as the database connection API.

Recommended reading:

Why should we not use mysql type functions_*?

Mysqli vs PDO - which is the most recommended to use?

How to prevent SQL code injection into my PHP code

  • It worked. I’ll study more about PDO.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.