H
H
HeartOfProgrammer2015-11-04 12:22:35
MySQL
HeartOfProgrammer, 2015-11-04 12:22:35

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);

You need to do this: When entering data into the fields, the data was checked, and if the check is true, then the data was entered into the database. And it turns out that the content did not pass the test and is entered into the database.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey, 2015-11-04
@HeartOfProgrammer

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

I
Ivanq, 2015-11-04
@Ivanq

Put on error $err=1; and check: if($err) { run the query; }
You can also write instead of echo - die() ;

R
Ruslan Fedoseev, 2015-11-04
@martin74ua

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 question

Ask a Question

731 491 924 answers to any question