Answer the question
In order to leave comments, you need to log in
How to stop query to MySQL database?
Let's say we have a code:
$title = $_POST['title_news'];
$firstDescription = $_POST['first_content'];
$secondDescription = $_POST['second_content'];
$added = $_POST['added'];
if(isset($_POST['title_news']) && !empty($_POST['title_news'])) {
}else{
echo "Заполните названия новости" . "</br>";
}
if(isset($_POST['first_content']) && !empty($_POST['first_content'])) {
}else{
echo "Заполните описания новости" . "</br>";
}
if(isset($_POST['second_content']) && !empty($_POST['second_content'])) {
}else{
echo "Заполните полное описания новости";
}
if(isset($_POST['added']) && !empty($_POST['added'])) {
}else{
echo "Заполните поле Автор";
}
$sql = "
INSERT INTO news (title, first_description, second_description, added)
VALUES ('$title', '$firstDescription', '$secondDescription', '$added')
";
if(!$connection) {
die('Ошибка при соединении: ' . mysqli_error());
}
$res = mysqli_query($connection, $sql)
or die('Ошибка при выполнении запроса к базе данных.');
mysqli_close($connection);
Answer the question
In order to leave comments, you need to log in
If you have this code outside of a function/class, then it will be enough to add the "exit" command to the content check logic, where an error occurred. In your logic, these are blocks in which "echo" with an error message. The algorithm will be as follows:
- if an error occurred while checking the content, then display a message and exit
- if the content was checked and there were no errors, then add a record to the database
Put on error $err=1; and check: if($err) { run the query; }
You can also write instead of echo - die() ;
hmm ... turn on the head.
$error=0 ; add at the beginning
$error =1; we add to each block that displays an error message
, and we execute the request if $error == 0;
Are you sure that programming is your thing? Maybe quit before it's too late?
ZY.Moderators, give me the opportunity to evaluate questions ....
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question