Я забыл упомянуть, что вы также должны активировать отчеты об ошибках и уровень mysql. Например. установить $mysqliDriver->report_mode
, как показано ниже.
Итак, протестируйте этот код как есть, после изменения учетных данных db. Если это работает, то проблема заключалась в ваших кодах. Если нет, то он лежит где-то еще.
<?php
// Db configs.
define('HOST', 'localhost');
define('PORT', 3306);
define('DATABASE', 'tests');
define('USERNAME', 'root');
define('PASSWORD', 'root');
/*
* Enable internal report functions. This enables the exception handling,
* e.g. mysqli will not throw PHP warnings anymore, but mysqli exceptions
* (mysqli_sql_exception).
*
* MYSQLI_REPORT_ERROR: Report errors from mysqli function calls.
* MYSQLI_REPORT_STRICT: Throw a mysqli_sql_exception for errors instead of warnings.
*
* @link http://php.net/manual/en/class.mysqli-driver.php
* @link http://php.net/manual/en/mysqli-driver.report-mode.php
* @link http://php.net/manual/en/mysqli.constants.php
*/
$mysqliDriver = new mysqli_driver();
$mysqliDriver->report_mode = (MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
// Create a new db connection.
$connection = new mysqli(HOST, USERNAME, PASSWORD, DATABASE, PORT);
// Signalize if a new record could be inserted, or not.
$recordInserted = FALSE;
// Operations upon form submission.
if (isset($_POST['submit'])) {
// Read the posted values.
$name = $_POST['name1'] ?? '';
$surname = $_POST['surname1'] ?? '';
$email = $_POST['email1'] ?? '';
// Validate the name.
if (empty($name)) {
$errors[] = 'Please provide the name.';
} /* Other validations here using elseif statements */
// Validate the surname.
if (empty($surname)) {
$errors[] = 'Please provide the surname.';
} /* Other validations here using elseif statements */
// Validate the email.
if (empty($email)) {
$errors[] = 'Please provide an email address.';
} elseif (
!filter_var($email, FILTER_SANITIZE_EMAIL) ||
!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = 'The email address is not in a valid format.';
} /* Other validations here using elseif statements */
// Insert a new record - if no errors yet.
if (!isset($errors)) {
$sql = 'INSERT INTO interested (
Name,
Surname,
Email
) VALUES (
?, ?, ?
)';
$statement = $connection->prepare($sql);
$statement->bind_param('sss', $name, $surname, $email);
$statement->execute();
// Signalize that the new record was successfully inserted.
$recordInserted = TRUE;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes" />
<meta charset="UTF-8" />
<!-- The above 3 meta tags must come first in the head -->
<title>Demo - Insert</title>
</head>
<body>
<div class="messages">
<?php
if (isset($errors)) {
echo implode('<br/>', $errors);
} elseif ($recordInserted) {
echo 'A new record was successfully inserted.';
}
?>
</div>
<br/>
<form action="" method="post">
<label for="name1">Name</label>
<input type="text" id="name1" name="name1" placeholder="Name" required>
<br/>
<label for="surname1">Surname</label>
<input type="text" id="surname1" name="surname1" placeholder="Surname" required>
<br/>
<label for="email1">Email</label>
<input type="email" id="email1" name="email1" placeholder="Email" required>
<button type="submit" id="submit" name="submit" value="submit">
Submit
</button>
</form>
</body>
</html>