Error executing UPDATE

-1

When running UPDATE the following errors occurred:

Notice: Undefined variable: mysqli in C:\xampp\htdocs\guaraparivirtual\count\count.php on line 17

Fatal error: Uncaught Error: Call to a member function query() on null in C:\xampp\htdocs\guaraparivirtual\count\count.php:17 Stack trace: #0 {main} thrown in C:\xampp\htdocs\guaraparivirtual\count\count.php on line 17

Code:

 <?php include ("../incluir/conexao.php"); ?>

<?php
$id = $_GET["id"];


                $propaganda_count=mysqli_query($con,"select click,link FROM bannersclick where id='$id'");
                while($count_click=mysqli_fetch_array($propaganda_count)){

                    $clickget = $count_click['click'];
                    $linkget = $count_click['link'];
                }

$clickget = $clickget + 1;

    $sqli = "UPDATE bannersclick SET click = '$clickget' where id = '$id'";
    $mysqli->query($sqli);

header( "Location: $linkget" , TRUE , 302 );
?> 

Line 17 of the errors is: $mysqli->query($sqli);

I tried to solve it, but I did not understand the reason for the error.

    
asked by anonymous 05.07.2017 / 13:16

1 answer

5

By error, it seems that no connection was started in the $mysqli variable. And by looking at the rest of the added code, the variable $mysqli does not exist until you use it on line 17, in the previous query, you use the $conn variable to do the search.

Without changing the code too much, instead of using a variable that does not exist, use the connection variable you used in the previous query:

<?php include ("../incluir/conexao.php"); ?>

<?php
    $id = $_GET["id"];

    $propaganda_count=mysqli_query($con,"select click,link FROM bannersclick where id='$id'");

    while($count_click=mysqli_fetch_array($propaganda_count)){
        $clickget = $count_click['click'];
        $linkget = $count_click['link'];
    }

    $clickget = $clickget + 1;

    $sqli = "UPDATE bannersclick SET click = '$clickget' where id = '$id'";
    $conn->query($sqli);

    header( "Location: $linkget" , TRUE , 302 );
?> 

Pay attention to what the documentation says:

  

It is possible to switch between styles at any time. Mixing both styles is not recommended for code clarity and coding style reasons.

That is, it is not recommended to use both modes at the same time, for reasons of organization and code clarity.

Source: PHP Docum

    
05.07.2017 / 13:21