PHP + SQL UPDATE PROBLEM

0

Good morning personal dawn. I am doing a simple quiz for my web class and am having a problem to do an update in SQL via PHP. The database is configured correctly, select works but the update is inert to the system. No error message and not working. Can someone give me a light please? And taking advantage of, how do 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());
    
asked by anonymous 10.04.2015 / 03:54

2 answers

3

So I checked in the 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();
    
10.04.2015 / 04:14
2

Nothing happens because the update did not run on the database. Only a string with a sql command is assigned the variable $sql , you need to execute this statement 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 not we use functions of type mysql_ *?

MySQL vs PDO - Which is the most recommended to use?

How to prevent SQL injection in my PHP code

    
10.04.2015 / 04:14