请看帮助。我不知所措!!我正在创建一个博客网站,管理员可以编辑和删除他们的文章。但是,当我通过查询时:
Fatal error: Uncaught Error: Call to a member function query()
on null in C:\xampp\htdocs\tp02\TP2PHP\Posting.class.php:22
Stack trace:
'#0' C:\xampp\htdocs\tp02\TP2PHP\editardelete.php(15):
Posting->getData('SELECT * FROM b...')
'#1' {main} thrown in
C:\xampp\htdocs\tp02\TP2PHP\Posting.class.php on line 22
加上一些未定义的索引。
Notice: Undefined index: titulo in C:\xampp\htdocs\tp02\TP2PHP\editardelete.php on line 10
Notice: Undefined index: contenido in C:\xampp\htdocs\tp02\TP2PHP\editardelete.php on line 11
Notice: Undefined property: Posting::$conn in C:\xampp\htdocs\tp02\TP2PHP\Posting.class.php on line 22
我猜是我的关系。请帮忙谢谢posting.class.php
<?php
require_once 'conexion.php';
require_once 'BaseDato.class.php';
require_once 'Admin.class.php';
class Posting extends Connectdb {
public $titulo;
public $contenido;
public function __construct($titulo,$contenido) {
$this->titulo = $titulo;
$this->contenido = $contenido;
}
public function getData($query) {
$result = $this->conn->query($query);
if ($result == false) {
return false;
}
$rows = array();
while ($row = $result->fetch_assoc()) {
$rows[] = $row;
}
return $rows;
}
public function execute($query) {
$result = $this->conn->query($query);
if ($result == false) {
echo 'Error: cannot execute the command';
return false;
} else {
return true;
}
}
public function delete($id, $table) {
$query = "DELETE FROM blogtp_1 WHERE id = $id";
$result = $this->conn->query($query);
if ($result == false) {
echo 'Error: cannot delete id ' . $id . ' from table ' . $table;
return false;
} else {
return true;
}
}
/*public function escape_string($value)
{
return $this->conn->real_escape_string($value);
} */
}
?>
另一个页面是xcalled editardelete.php:
<?php
// including the database connection file
require_once 'conexion.php';
include 'BaseDato.class.php';
include 'Posting.class.php';
//datos de la conexion
$conexion = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
session_start();
$titulo = $_POST['titulo'];
$contenido = $_POST['contenido'];
$posting = new Posting($titulo,$contenido);
//fetching data in descending order (lastest entry first)
$query = "SELECT * FROM blogtp_1 ORDER BY id DESC";
$result = $posting->getData($query);
//echo '<pre>'; print_r($result); exit;
?>
<html>
<head>
<title>Homepage</title>
</head>
<body>
<table width='80%' border=0>
<tr bgcolor='#CCCCCC'>
<td>titulo</td>
<td>contenido</td>
</tr>
<?php
foreach ($result as $key => $res) {
//while($res = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>".$res['titulo_del_post']."</td>";
echo "<td>".$res['contenido_del_post']."</td>";
echo "<td><a href=\"editar.php?id=$res[id]\">Editar</a> </td>";
}
?>
</table>
</body>
</html>
这是我的连接类:
<?php
class Connectdb{
private $host;
private $user;
private $pass;
private $db;
protected function connect(){
$this->host = "localhost";
$this->user = "root";
$this->pass = "";
$this->db = "blog";
$conn = mysqli_connect($this->host, $this->user, $this->pass, $this->db);
return $conn;
}
}
?>
1条答案
按热度按时间nkoocmlb1#
首先,我们必须重组你的代码。你的连接类。
下一个是你的发帖课。
最后在editardelete.php文件中。
我希望这有帮助。维托里亚快乐!