Adding data to the database via the form

I enter the data in the database through the form. I.e., in one file I create a form for input, and use the $_POST method to send it to another file, where the code for inserting the entered data into the database is located. Swears just on the very main line, which is responsible for the insertion.

 $result = mysqli_query("INSERT INTO firma ('name','surname','doljnost') VALUES ('$name','$surname','$doljnost')");

Writes: Warning: mysqli_query() expects at least 2 parameters, 1 given in Z:\home\localhost\www\php\mysql_insert2.php on line 38

Here is such a "thing" scribbled in a file that sends data to the database.

<?php

$link = mysqli_connect('localhost', 'alex', '12345', 'firstbd');

/* проверка соединения */
if (mysqli_connect_errno()) {
    printf("Не удалось подключиться: %s\n", mysqli_connect_error());
    exit();
}

/* изменение набора символов на utf8 */
mysqli_set_charset($link, "utf8");

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

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

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

$result = mysqli_query("INSERT INTO firma ('name','surname','doljnost') VALUES ('$name','$surname','$doljnost')");

if ($result==true)
{
echo "<br>Информация в базу добавлена успешно.";
}
else echo "<br>Информация в базу не добавлена.";

mysqli_close($link);

?>
Author: Nick Volynkin, 2013-05-17

2 answers

Here is the documentation: mysqli_query(). In the procedural style, the function takes two parameters: the connection ID and the request.

Thus, it is worth writing like this:

$query = "INSERT INTO firma (`name`,`surname`,`doljnost`) VALUES ('$name','$surname','$doljnost')";

$result = mysqli_query($link, $query); // ключевой момент - первый параметр

I hope you know that incoming data cannot be substituted without processing.

DB columns are marked with the inverse, and not with the usual apostrophe, i.e. ` instead of '

To find out what happened, use mysqli_error(). Instead of " Information in the database is not added " write:

echo mysqli_error($link);
 3
Author: xEdelweiss, 2013-05-17 10:53:34

Shouldn't it be like this?

$result = mysqli_query($link,"INSERT INTO firma ('name','surname','doljnost') VALUES ('".$name."','".$surname."','".$doljnost."')"); // обновил! Теперь вставятся :)

It seems like you need to add a link to the connect to the database... or am I wrong?

/ / ps while answering edelweiss made it faster :(

 2
Author: Artem, 2013-05-17 11:21:34