Answer the question
In order to leave comments, you need to log in
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");
<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
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();
}
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();
}
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')
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 questionAsk a Question
731 491 924 answers to any question