Y
Y
Yevhenii K2015-10-26 18:47:53
PHP
Yevhenii K, 2015-10-26 18:47:53

The script does not add news to the database, what's the problem?

Good afternoon! I wrote a script for adding news to the database, but it does not work and does not display errors. The connection to the database is working, the data is being transferred to the post and can be displayed. Please help.
Script

<?php
include ("db.php");

class News extends DB {
  public $title;
  public $content;
  public $res_link;
  public $res_name;
  public $date;
  public $category;
  public $try_connect;
  public $update;

  public function __construct(Array $a) {
    $this->title = $a['title'];
    $this->content = $a['content'];
    $this->category = $a['category'];
    $this->res_link = $a['resource_link'];
    $this->res_name = $a['resource_name'];
    $this->date = ('Y-m-d');
    $this->try_connect = self::obj()->connect();

  }

  public function addNews() {
    $new_news = "INSERT INTO `news` VALUES ('', '$this->title', '$this->content','$this->category', '$this->date', '$this->res_name', '$this->res_link')";
    $this->update = $this->try_connect->exec($new_news);
  }
  
  public static function getNews($id) {
    $cat_news = "SELECT * FROM `news` WHERE `news`.`category` = $id";
    $getnews = DB::obj()->connect()->query($cat_news);
      if(is_null($getnews)) {
        return [];
      }
    return $getnews->fetchALL(PDO::FETCH_ASSOC);
  }

  public static function getNewsById($id) {
    $id_news = "SELECT * FROM `news` WHERE `news`.`id` = $id";
    $getnews_id =DB::obj()->connect()->query($id_news);
      if(is_null($getnews)) {
        return [];
      }
    return $getnews->fetchALL(PDO::FETCH_ASSOC);
  }
}
$new= new News($_POST);
$new->addNews();
//header("Location: http://site.loc/add_news_form.html");

Input form
<div class="wrap-admin">
<div class="addnews">
<form action="add_news_script.php" method="POST">
  <p>Введите заголовок</p>
  <input name="title" type="text" size="60">
  <p>Введите ссылку на источник</p>
  <input name="resource_link" type="text" size="60">
  <p>Введите название сайта-источника</p>
  <input name="resource_name" type="text" size="60">
  <p>Введите название рубрики</p>
  <select name="category">
    <option value="1">Политика</option>
    <option value="2">Экономика</option>
    <option value="3">Культура</option>
    <option value="4">Спорт</option>
  </select>
  <p>Введите текст новости</p>
  <textarea name="content" cols="60" rows="40"></textarea>
  
  <input type="submit" value="Опубликовать новость">
</form>
</div>
<div class="addnews">
  <form action="#" method="POST">
    <p>Введите название рубрики</p>
    <input name="category" type="text">
    <input type="submit" value="Создать рубрику">
  </form>
</div>
</div>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dimonchik, 2015-10-26
@dimonchik2013

fill in the values

public function __construct(Array $a) {
    $this->title = $a['title'];
    $this->content = $a['content'];
    $this->category = $a['category'];
    $this->res_link = $a['resource_link'];
    $this->res_name = $a['resource_name'];
    $this->date = ('Y-m-d');
    $this->try_connect = self::obj()->connect();

  }

type
public function __construct(Array $a) {
    $this->title = 'ебаный';
    $this->content = 'скрипт';
    $this->category = 'который';
    $this->res_link = 'нихуя';
    $this->res_name = 'не работает';
    $this->date = ('Y-m-d');
    $this->try_connect = self::obj()->connect();

  }

and do
it get into the base?

I
Ilya Beloborodov, 2015-10-26
@kowap

INSERT INTO `news` (title,content,category,date, res_name, res_link) VALUES ('', '$this->title', '$this->content','$this->category', '$this->date', '$this->res_name', '$this->res_link')

In the first brackets are the names of the fields, in the second, the info itself, in which the listed fields will be written. it is important to follow the sequence

A
Aleksey Ratnikov, 2015-10-26
@mahoho

If I understand everything correctly, then $stmt = DB::obj()->connect()->query($query);
Returns a PDOStatement object. Execute an INSERT through this method, and then see what will be in $stmt->errorInfo()[2]. If INSERT does not work, then there is an error in the query.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question