update query doesnt update value [on hold]

184
December 24, 2017, at 11:38 AM

I got a table called dupa.

+--------+--------+-----+
| numer  | wynik  | avg |
+--------+--------+-----+
| 1      | 20     | 20  |
+--------+--------+-----+
| 2      | 40     |  x  |
+--------+--------+-----+

I use query1 to get avg= 20, then query2 to insert result = 40. Then I use query3 just to show new numer = 2. Then I calculate new avg but query4 for updating x doesnt work. Its 0 instead of 30.

<?php
    require_once('connection.php');
    $query1 = "SELECT avg FROM dupa ORDER BY numer DESC LIMIT 1";
    $response1 = @mysqli_query($connect, $query1);
    if($response1)
     {while($row0 = mysqli_fetch_array($response1)) 
        {$last_avg = $row0[0];}
    } else {
    echo "Couldnt receive last_avg";
    echo mysqli_error($connect);
    } 
    $numer = $_POST["numer"]?? '';
    $wynik = 40;
    $avg = $_POST["avg"]?? '';
    $query2 = "INSERT INTO dupa VALUES('$numer','$wynik','$avg');";
    $response2 = @mysqli_query($connect, $query2);
    if($response2)
    {
    $query3 = "SELECT numer, wynik FROM dupa ORDER BY numer DESC LIMIT 1";
    $response3 = @mysqli_query($connect, $query3);
    if($response3)
        {
        while($row = mysqli_fetch_array($response3)) 
            {$last_id = $row[0];
            $wynik = $row[1];
            $new_avg = (($last_id-1)*$last_avg+$wynik)/$last_id;
            echo $last_id;}
        $_POST["numer"] = $row['numer'];
        $query4 ="UPDATE dupa SET avg ='$new_avg' WHERE numer ='$last_id'";
        $response4 = @mysqli_query($connect, $query3);
        if($response4) {
                echo $new_avg;
                } else {
                echo "Couldnt update avg";
                echo mysqli_error($connect);
                } 
        } else {
        echo "Couldnt receive numer";
        echo mysqli_error($connect);
        } 
    } else {
    echo "Couldnt insert wynik";
    echo mysqli_error($connect);
    } 
mysqli_close($connect);
?> 
Answer 1

You are trying to update the database on query4 with your statement from query3.

Change

$response4 = @mysqli_query($connect, $query3); 

to

$response4 = @mysqli_query($connect, $query4);
Answer 2

on line $response4 = @mysqli_query($connect, $query3);

change it to $response4 = @mysqli_query($connect, $query4);

you were passing wrong variable in @mysqli_query.

Rent Charter Buses Company
READ ALSO
Should i Bind variables all the time?

Should i Bind variables all the time?

When a User Login to the Website

162
mySQL query that changes team id to team name and makes not null blank

mySQL query that changes team id to team name and makes not null blank

I have made a results table for a football league called 'games'This results table shows the time and date, the home/away team ids and the home/away scores

188
lastInsertId() returns NULL

lastInsertId() returns NULL

i am trying to get the last id inserted in my sql query, but my varieble returns with NULL

155
The json result of my php file contains ints but I need all the values as Strings

The json result of my php file contains ints but I need all the values as Strings

my json result from the mysqlquery in the php-file looks something like this: [{"ID":1,"comment":"Hello"},{"ID":2,"comment":"SecondHello"}] For my purpose I need the IDs also as a StringI cannot/do not want to change the datatype in the database

118