H
H
HeartOfProgrammer2015-09-29 15:46:57
PHP
HeartOfProgrammer, 2015-09-29 15:46:57

How to enter the data that was received through the $_POST method?

There are 2 files, add_news.php and processing.php.
in add_news.php:

<div class="form_add_text">
    <h2>Форма добавления новости</h2>
  </div>
  <div class="add_news">
      <form method="post" action="processing.php">
        <div class="add_title_news">
          <input type="text" name="title_news" maxlength="75" style="width: 990px; height: 24px">
        </div>
        <div class="editor_main">
          <textarea name="first_content"></textarea>
        </div>
    			<div class="info_edit">
          <h3>Полное описания новости</h3>
        </div>
    			<div class="editor_secondary">
    				<textarea name="second_content"></textarea>
    			</div>
    			<input type="submit">
      </form>
  </div>

in processing.php:
$id = $_POST['id'];
  $title = $_POST['title'];
  $firstDescription = $_POST['first_description'];
  $secondDescription = $_POST['second_description'];
  $added = $_POST['added'];
  $date = $_POST['date'];

  $sql = "
    INSERT INTO news(id,title,first_description,second_description,added,date)
    VALUES($id,$title,$first_description,$second_description,$added,$date)
  ";

  if(!mysqli_query($connection,$sql)) {
    echo "Ошибка при добавлении данных!";
  }else {
    echo "Данные добавлены!";
  }

  $result = mysqli_query($connection,$sql);

  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 "Заполните полное описания новости";
  }

In my understanding: Filling the visual editor "TinyMce" and after clicking the submit button, all data is sent via the $_POST method to the processing.php page. After that, everything is processed there and entered into the database. But this procedure did not work for me. Please poke your nose at the problem.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Vasiliev, 2015-10-01
@HeartOfProgrammer

1. Perhaps in the database one / several fields require mandatory filling, and the data does not come in completely => it is necessary to check the data before adding it to the database
2. The Id field, most likely, in the database has the AUTO_INCREMENT parameter, which in turn will not meaning.
3. Before using data from incoming parameters (GET, POST, COOKIE), it is necessary to check them. Delete special. symbols.
4. You, when checking for a request error, are already running a request. That is, in the code it will be executed twice.
5. Some html input field names do not match indexes in $_POST array.
ps file, it's very easy to check which parameters came in and which didn't, like this:
print_r($_POST);

T
TheMrViper, 2015-09-29
@TheMrViper

1 Validate incoming data after adding to db? cool
2 2x addition to db, not bad either.
3 Getting id from post is even better.
answers:
1 data needs to be checked before adding to the database
2

if(!mysqli_query($connection,$sql)) {
    echo "Ошибка при добавлении данных!";
  }else {
    echo "Данные добавлены!";
  }

  $result = mysqli_query($connection,$sql);

replaced by:
if(!mysqli_query($connection,$sql)) {
    echo "Ошибка при добавлении данных!";
  }else {
    echo "Данные добавлены!";
  }

3 The ID should be issued by the database itself, not by you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question