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

-2

My code is the one here

<?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>";
        }
    }
?>
    
asked by anonymous 06.11.2018 / 01:15

2 answers

1

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

Note: Do not forget to swap my_user , my_password , my_db for your data.

    
06.11.2018 / 01:57
0

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

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

That is to say that the function mysqli_query() expected 2 parameters and you only passed 1.

Why this error?

The mysqli_query () function needs 2 mandatory parameters.

  • The connection. Where you specify the mysql connection that will use (In your case, the $ connect variable that passed empty but should contain the connection)
  • The Query. Where do you pass the command that you will use in the database.
  • 06.11.2018 / 02:51