Error mysqli_query () expects at least 2 parameters, 1 given in to Insert data

1

I am making a system in which at the time of entering the data, this error marks me.

  

Error: Warning: mysqli_query () expects at least 2 parameters, 1 given   in

<?php
require("connect_db.php");
if (isset($_POST['descripcion']) && !empty($_POST['descripcion'])) {
    $descripcionTurno = $_POST['descripcion'];
    mysqli_query("INSERT INTO ctg_turno (descripcion) values ('$descripcionTurno'");
    echo '<script>alert("Datos Ingresados Correctamente")</script> ';
}
    
asked by Oscar_DR 01.02.2018 в 23:28
source

3 answers

1

First you need to pass the connection as the first parameter to mysqli_query (style procedure) ,

mysqli_query($con, "INSERT....") { ...}

But the even bigger problem is the concatenation of variables in a statement, thus it is prone to receive attacks from Injection SQL . You should use prepared sentences. (Ejm base)

 $mysqli = new mysqli("localhost", "my_user", "my_password", "midatabase");

if (isset($_POST['descripcion']) && !empty($_POST['descripcion'])) {
    $descripcionTurno = $_POST['descripcion'];
    if ($stmt = $mysqli->prepare("INSERT INTO ctg_turno (descripcion) values (?)")) {
        $descripcionTurno = $_POST['descripcion'];
        /* añadir los valores para los marcadores con la variable descripcionTurno*/
        $stmt->bind_param("s", $descripcionTurno);
        /* ejecutamos la consulta */
        if($stmt->execute()){
            echo "INSERT CORRECTO";
        }
    }
}
    
answered by 01.02.2018 / 23:45
source
2

The function requires two arguments, the first is the connection and the second the SQL.

You have to put the connection to the function as an argument to mysqli_query , for example:

mysqli_query($con,"SELECT * FROM Persons");

$ with - > It is the first parameter, the connection you have established.

"SELECT * FROM Persons" - > It's the SQL you want to run

    
answered by 01.02.2018 в 23:42
1

The error is because you are using the procedural style of the mysqli function as described in the manual. The first parameter must be

  

A link identifier returned by mysqli_connect () or mysqli_init ()

Applies to style by procedures.

I recommend that you also review your work style, it is not advisable to directly pass the user data to the query directly, for that there are the parameterized queries.

    
answered by 01.02.2018 в 23:41