N
N
Nikolai Chakhov2020-06-04 00:29:46
PHP
Nikolai Chakhov, 2020-06-04 00:29:46

DB insert query not working. How to fix?

INSERT query not working

spoiler
$cat = $_POST['cat'];
$title = $_POST['title'];
$keyw = $_POST['keywords'];
$descr = $_POST['description'];
$textt = $_POST['text'];
$hard = $_POST['hard'];
$mini_img = $_POST['mini_img'];
$source = $_POST['source'];
$file = $_POST['file'];
$file_size = $_POST['file_size'];
$demo = $_POST['demo'];
$syntax = $_POST['syntax'];
$view = 0;
$author = "Николай Чахов";

echo "<pre>";
var_dump ($_POST);
echo "</pre>";

$query = "INSERT INTO `post` (`post_id`, `cat`, `title`, `keywords`, `description`, `text`, `hard`, `mini_img`, `author`, `source`, `file`, `file_size`, `demo`, `date', `view`, `syntax`) 
VALUES (' ',$cat,$title,$keywords,$description,$text,$hard,$mini_img,$author,$source,$file,$file_size,$demo, NOW(),$view,$syntax)";

$result = $con->query($query);
if ($result == true){ 	
  echo "<p>Информация занесена в базу данных</p>"; 
} else { 	
    echo "<p>Информация не занесена в базу данных</p>"; 
    echo('Ошибка : ('. $con->connect_errno .') '. $con->connect_error);
}

The request fails and returns no errors.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanatPHP, 2020-06-04
@RuWebSD

No wonder it doesn't work. Everything is wrong here.

  1. A space cannot be inserted into an auto-increment field. It is necessary to insert either null or skip this field altogether.
  2. Variables should not be sent directly, but through substitutions
  3. Errors should never be output manually, but you need to configure the database once so that it reports errors itself

Take the connection code from here
And execute the query itself like this:
All variables must be replaced in the query with question marks,
then prepare the query,
bind variables to it,
and execute
$query = "INSERT INTO `post` (`post_id`, `cat`, `title`, `keywords`, ...) 
VALUES (null,?,?,?, ..., NOW(),?,?)";
$stmt = $con->prepare($query);
$stmt->bind_param("sss...", $cat,$title,$keywords, ...);
$stmt->execute();
echo "<p>Информация занесена в базу данных</p>";

The number of question marks in the query and the number of "s"s in the first parameter of bind_param must match the number of variables.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question