A
A
Alexfixer2019-11-19 22:04:05
CodeIgniter
Alexfixer, 2019-11-19 22:04:05

How to prohibit writing data from the form to the DB. if such a slug already exists in the DB?

Tell me how to change the code so that when entering a slug in the form, if it exists in the database, then a message would be displayed that such a slug is in the database. already exists, create a new one...
Controller:

public function create() {

    if(!$this->dx_auth->is_logged_in()) {
      show_404();
      // $this->load->helper('url_helper');
      // redirect('/', 'location');
    }

    $this->data['title'] = "Добавить пост";

    //получаем данные из формы:
    if($this->input->post('slug') && $this->input->post('title') && $this->input->post('text') && $this->input->post('author')) {

    //тут должны быть проверки на введённые пользователем данные, защита от скриптов
    //и прочей дичи  	
    //слаг = данные из формы из поля слаг, также с заголовком и текстом
      $slug = $this->input->post('slug');
      $title = $this->input->post('title');
      $text = $this->input->post('text');
      $author = $this->input->post('author');
      
      
      // отправляем в модель данные 
      if($this->userlk_model->setUserPosts($slug, $title, $text, $author)) {

      $this->load->view('templates/header', $this->data);
      $this->load->view('user_lk/success', $this->data);
      $this->load->view('templates/footer');
    }

    } else {
      $this->load->view('templates/header', $this->data);
      $this->load->view('user_lk/create', $this->data);
      $this->load->view('templates/footer');
    }

  }

Model:
public function setUserPosts($slug, $title, $text, $author) {
    $data = array(
      'title' => $title,
      'slug' => $slug,
      'text' => $text,
      'author' => $author
    );

    return $this->db->insert('users_posts', $data);

  }

  public function updateUserPosts($slug, $title, $text) {
    $data = array(
      'title' => $title,
      'slug' => $slug,
      'text' => $text
    );

    return $this->db->update('users_posts', $data, array('slug' => $slug));

  }

create.php view:
<p><a href="/user_lk" class="btn btn-default pull-right btn_home_posts_down">Вернуться назад...</a></p>
<h2>Предложить пост</h2>
<form action="/user_lk/create" method="post" enctype="multipart/form-data">
  Введите название поста на латинице<span class='form_message_style'>*</span>
  <input class="form-control input-lg" type="input" name="slug" placeholder="slug"><br>
  Введите название поста на русском языке<span class='form_message_style'>*</span>
  <input class="form-control input-lg" type="input" name="title" placeholder="Название"><br>
  Автор вы (по умолчанию)
  <input class="form-control input-lg" type="input" name="author" placeholder="Автор вы" value="<?php echo $this->dx_auth->get_username(); ?>" readonly="readonly"><br>
  <textarea class="form-control input-lg" name="text" id="editor" placeholder="текст поста"></textarea>
  <input class="btn btn-default" type="submit" name="submit" value="Добавить пост">
</form>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg, 2019-11-20
@Alexfixer

CodeIgnore . The developers suggest using a validator. See the official documentation. Field uniqueness is one of the checks that comes out of the box.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question