E
E
Evgeny2017-11-27 19:42:38
PHP
Evgeny, 2017-11-27 19:42:38

How to fix mysql database connection error?

Hello!
I'm trying to run PHP on openserver.
Created a database using phpmyadmin.
Once I tried, I can not get a connection to the MySQL database in any way

<?php
$first_name         = $_POST['firstname'];
$last_name          = $_POST['lastname'];
$when_it_happened  = $_POST['whenithappened'];
$how_long          = $_POST['howlong'];
$how_many          = $_POST['howmany'];
$alien_description = $_POST['aliendescription'];
$what_they_did     = $_POST['whattheydid'];
$fang_spotted      = $_POST['fangspotted'];
$email             = $_POST['email'];
$other             = $_POST['other'];

$dbc = mysqli_connect('127.0.0.1:3306', 'mysql', 'mysql', 'aliendatabase')
    or die ('Ошибка соединения с MySQL - сервером.');

$query = "INSERT INTO aliens_abduction (first_name, last_name, when_it_happened, how_long, " .
    "how_many, alien_description, what_they_did, fang_spotted, other, email) " .
    "VALUES ('$first_name', '$last_name', '$when_it_happened', '$how_long', '$how_many', " .
    "'$alien_description', '$what_they_did', '$fang_spotted', '$other', '$email')";

$result = mysqli_query($dbc, $query) or die ('Ошибка при выполнении запроса к базе данных.');

mysqli_close($dbc);
?>

Always returns "Error while executing database query". How to understand what could be the reason?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Danil Isaev, 2019-09-11
@acdaedasd

if(!isset($_SESSION[ 'admincp' ]) && !isset($_SESSION['login'])))

R
Rsa97, 2017-11-27
Myagkikh @myagkikh_evgeniy

Anything. See the error in mysqli_error() .
It can be assumed that one of the inserted data contains a single quote. Since you are not using placeholders, or at least escaping, any special character in the data can cause an error.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question