K
K
khomaldi2018-07-01 15:40:03
PHP
khomaldi, 2018-07-01 15:40:03

Why doesn't insert into work with php?

Hello. I'm trying to write data to the database through php, but the record is not added. Please tell me what is wrong.
html form:

<form method="POST" action="<?=$_SERVER['REQUEST_URI']?>">
    <input name="title" type="text" placeholder="Заголовок"/>
    <input name="intro" type="text" placeholder="Краткое описание"/>
    <input name="full" type="text" placeholder="Полный текст"/>
    <input type="submit" value="Отправить"/>
</form>

PHP
if (isset($_POST['title']) && isset($_POST['intro']) && isset($_POST['full']))
{
    // Переменные с формы
    $title = $_POST['title'];
    $intro = $_POST['intro'];
    $full = $_POST['full'];

    // Параметры для подключения
    $db_host = "localhost";
    $db_user = "login"; // Логин БД
    $db_password = "password"; // Пароль БД
    $db_table = "articles"; // Имя Таблицы БД

    // Подключение к базе данных
    $mysqli = new mysqli("localhost", "login", "password", "articles");

    $query = "INSERT INTO articles (title, intro, full) VALUES ('$title','$intro', '$full')";

    $mysqli->query($query);
}

SOLUTION:
The error was in and in$db_table = "articles";
$mysqli = new mysqli("localhost", "login", "password", "articles");

You need to write the name of the database instead of articles (this is the name of the table). (for example, user32234. usually matches the username). And the table name is specified in the query"INSERT INTO articles

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Nemiro, 2018-07-01
@khomaldi

$db_table = "articles"; // Database table name
No, not the table name, but the database name ( $dbname ).
if (!$mysqli->query($query)) {
    printf("Errormessage: %s\n", $mysqli->error);
}

See also: SQL Injection

D
Dmitry, 2018-07-01
@php10

1. Start by checking the connection
2. Don't write like that. Use prepared statement and PDO

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question