Error while running UPDATE

Asked

Viewed 59 times

-1

When executing the 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, but did not understand the reason for the error.

  • 1

    Did you start an instance of $mysqli? By error, it seems not.

1 answer

5


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

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 clarity of code.

Source: PHP document

  • Connection started because it runs a routine above this code.

  • 1

    @Gladison for the error, the variable was not started, add the rest of the routine then to evaluate better, because only analyzing by the past information, this answer would solve.

  • I added the full code.

  • 1

    @Gladison according to the issue, the problem is what I mentioned, you’re using an uninitiated variable. $mysqli does not exist in your code until line 17. You are mixing procedural mode with OO mode, so the problem.

  • how to solve? What is the best way to structure this code?

  • @Gladison depends on the preference of the programmer, I particularly prefer the OO because I am already used to this paradigm. But there has to be consistency with the rest of the code, if it is already procedural, insert OO can make it difficult to read.

  • 1

    What’s with the downvote? If I don’t know what’s wrong with the answer, there’s no way I can fix it and it’ll still be wrong if the vote is for that reason.

  • I did not give a downvote, where was that? I voted as a useful response and tb as in "V" as a response.

  • @Gladison I know, someone had denied, the comment was for the person who denied informing the problem, but unfortunately she preferred to omit.

Show 5 more comments

Browser other questions tagged

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