Warning: mysqli_query(): Couldn’t fetch mysqli in C: xampp htdocs aula exercicio update.php on line 11

Asked

Viewed 1,470 times

0

Guys I’m having a problem with this error message,I checked the connection to the database and sure I can register but when it comes time to update it of this error and I can’t find out

Follow the update code:

    ?php 
    include "conexao.php";
    if(!empty($_POST)){
        $id = (isset($_POST['id'])) ? $id = $_POST['id'] : mysqli_close($conect);
        $cpf = (isset($_POST['cpf'])) ? $cpf = $_POST['cpf'] : mysqli_close($conect);
        $nome = (isset($_POST['nome'])) ? $nome = $_POST['nome'] : mysqli_close($conect);
        $email = (isset($_POST['email'])) ? $email = $_POST['email'] : mysqli_close($conect);
        $telefone = (isset($_POST['telefone'])) ? $telefone = $_POST['telefone'] : mysqli_close($conect);
        $sql = "UPDATE cadastro SET cpf = '$cpf',nome = '$nome',email = '$email',telefone = '$telefone'
        WHERE id = '$id' ";
        $query = mysqli_query($conect,$sql);


        var_dump($id);
        var_dump($cpf);
        var_dump($nome);
        var_dump($email);
        var_dump($telefone);
    }
    else{

        header("Location:cadastro.php");
    }

?>

Follows the connection:

<?php 
$host = "localhost";
$user = "root";
$psw= "";
$db = "exercicio";
$conect = mysqli_connect($host,$user,$psw,$db);
$charset = mysqli_set_charset($conect,"utf8");
 ?>
  • You are using the ternary operator as if it were an `if/Else. It works as an if. It tests a condition (the first operand), if it is true, the result of the operation is the first value (after the ?, the second operand), if it is false, then the result is the second value (after the :, the third operand). Read the reply: https://answall.com/a/56814/137387

1 answer

1


You are running the mysqli_close before the query, it doesn’t even make sense to run there, the correct thing is to run it after the mysqli_query, or better after all necessary.

In fact there are a number of errors in your code, until the way you set the variables has no sense, do not forget to escape the variables with mysqli_real_escape_string, it should be like this:

$id = (isset($_POST['id'])) ? mysqli_real_escape_string($_POST['id']) : '';
$cpf = (isset($_POST['cpf'])) ? mysqli_real_escape_string($_POST['cpf']) : '';
$nome = (isset($_POST['nome'])) ? mysqli_real_escape_string($_POST['nome']) : '';
$email = (isset($_POST['email'])) ? mysqli_real_escape_string($_POST['email']) : '';
$telefone = (isset($_POST['telefone'])) ? mysqli_real_escape_string($_POST['telefone']) : '';

$sql = "UPDATE cadastro SET cpf = '$cpf',nome = '$nome',email = '$email',telefone = '$telefone'
    WHERE id = '$id' ";

$query = mysqli_query($conect,$sql);

mysqli_close($conect);
  • thanks fixed this all normal now

Browser other questions tagged

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