People my code keeps giving the error: Notice: Undefined variable: connects in C: xampp htdocs network profile.php on line 25

Asked

Viewed 797 times

-2

My code and this one

<?php
    include("header.php");

    $id = $_GET["id"];
    $saberr = mysqli_query($conecta, "SELECT * FROM users WHERE id='$id'");
    $saber = mysqli_fetch_assoc($saberr);
    $email = $saber["email"];

    if ($email==$login_cookie) {
        header("Location: myprofile.php");
    }

    $pubs = mysqli_query($conecta, "SELECT * FROM pubs WHERE user='$email' ORDER BY id desc");

    if (isset($_POST['add'])) {
        add();
    }

    function add(){
        $login_cookie = $_COOKIE['login'];
        if (!isset($login_cookie)) {
            header("Location: login.php");
        }
        $id = $_GET['id'];
        $saberr = mysqli_query($conecta, "SELECT * FROM users WHERE id='$id'");
        $saber = mysqli_fetch_assoc($saberr);
        $email = $saber['email'];
        $data = date("Y/m/d");

        $conf = mysqli_query($conecta, "INSERT INTO amizades ('de', 'para', 'data') VALUES ('$login_cookie','$email','data')") or die(mysqli_error($conecta));
        if ($conf) {
            header("Location: profile.php?id=".$id);
        }else{
            echo "<h3>Erro ao enviar pedido...</h3>";
        }
    }

    if (isset($_POST['cancelar'])) {
        cancel();
    }

    function cancel(){
        $login_cookie = $_COOKIE['login'];
        if (!isset($login_cookie)) {
            header("Location: login.php");
        }
        $id = $_GET['id'];
        $saberr = mysqli_query($conecta, "SELECT * FROM users WHERE id='$id'");
        $saber = mysqli_fetch_assoc($saberr);
        $email = $saber['email'];

        $ins = "DELETE FROM amizades WHERE 'de'='$login_cookie' AND para='$email'";
        $conf = mysqli_query($conecta, $ins) or die(mysql_error());
        if ($conf) {
            header("Location: profile.php?id=".$id);
        }else{
            echo "<h3>Erro ao cancelar pedido...</h3>";
        }
    }

    if (isset($_POST['remover'])) {
        remove();
    }

    if (isset($_POST['chat'])) {
        header("Location: chat.php?from=".$id);
    }

    function remove(){
        $login_cookie = $_COOKIE['login'];
        if (!isset($login_cookie)) {
            header("Location: login.php");
        }
        $id = $_GET['id'];
        $saberr = mysqli_query($conecta, "SELECT * FROM users WHERE id='$id'");
        $saber = mysqli_fetch_assoc($saberr);
        $email = $saber['email'];

        $ins = "DELETE FROM amizades WHERE 'de'='$login_cookie' AND para='$email' OR `para`='$login_cookie' AND de='$email'";
        $conf = mysqli_query($conecta, $ins) or die(mysqli_error());
        if ($conf) {
            header("Location: profile.php?id=".$id);
        }else{
            echo "<h3>Erro ao eliminar amizade...</h3>";
        }
    }

    if (isset($_POST['aceitar'])) {
        aceitar();
    }

    function aceitar(){
        $login_cookie = $_COOKIE['login'];
        if (!isset($login_cookie)) {
            header("Location: login.php");
        }
        $id = $_GET['id'];
        $saberr = mysqli_query($conecta, "SELECT * FROM users WHERE id='$id'");
        $saber = mysqli_fetch_assoc($saberr);
        $email = $saber['email'];

        $ins = "UPDATE amizades SET 'aceite'='sim' WHERE 'de'='$email' AND para='$login_cookie'";
        $conf = mysqli_query($ins) or die(mysqli_error());
        if ($conf) {
            header("Location: profile.php?id=".$id);
        }else{
            echo "<h3>Erro ao eliminar amizade...</h3>";
        }
    }
?>
  • 1

    $connects is included in header.php? If so, make sure it’s global. If not, try declaring $connects within your scope of that page and see if the error persists, please. From what I saw, $connects is not stated in this scope.

  • 1

    Was it just that mistake? If $conecta nothing in the code will work because it is simply the connection variable to the database.

  • I took out the $connects but it appears these errors Warning: mysqli_query() expects at least 2 Parameters, 1 Given in C: xampp htdocs rede profile.php on line 25 Warning: mysqli_fetch_assoc() expects Parameter 1 to be mysqli_result, null Given in C: xampp htdocs rede profile.php on line 26 Warning: mysqli_query() expects at least 2 Parameters, 1 Given in C: xampp htdocs rede profile.php on line 31 Notice: Undefined variable: connect in C: xampp htdocs rede profile.php on line 31 Warning: mysqli_error() expects Parameter 1 to be mysqli, null Given in C: xampp htdocs rede profile.php on line 31

2 answers

0

This variable $conecta seems to me to have no value assigned to her, put a $conecta = mysqli_connect("localhost","my_user","my_password","my_db");, and see if the bug has been fixed.

Note: Don’t forget to change my_user, my_password, my_db for your data.

0

Are you saying that the connected variable has no assigned value. If you remove this variable and pass the code "mysqli_query("SELECT * FROM users WHERE id='$id'");" will receive the following error:

Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\xampp\htdocs\rede\profile.php on line 25

Which means the function mysqli_query() expected 2 parameters and you only passed 1.


Because that mistake?

The mysqli_query() function needs 2 mandatory parameter.

  1. The connection. Where you specify the mysql connection you will use.(In your case, the $connects variable that passed empty but should contain the connection)
  2. A Query. Where you pass the command you will use in the database.

Browser other questions tagged

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