如何通过查询获取数据,以便创建一个与oop相关的类的编辑函数?

tct7dpnv  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(254)

请看帮助。我不知所措!!我正在创建一个博客网站,管理员可以编辑和删除他们的文章。但是,当我通过查询时:

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;
    }
}
?>
nkoocmlb

nkoocmlb1#

首先,我们必须重组你的代码。你的连接类。

<?php

// error class just incase an error occured when trying to connect
class __errorClass
{
    public function __call($meth, $args)
    {
        echo $meth . '() failed! Database connection error!';
    }
}

 class Connectdb{
    private $host = "localhost";
    private $user = "root";
    private $pass = "";
    private $db = "blog";

    public function connect()
    {
        $conn = mysqli_connect($this->host, $this->user, $this->pass, $this->db);

        if ($conn->errorCode == 0)
        {
            return $conn;
        }
        else
        {
            return new __errorClass();
        }
    }
}

?>

下一个是你的发帖课。

<?php

require_once 'conexion.php';
require_once 'BaseDato.class.php';
require_once 'Admin.class.php';

class Posting{

public $titulo; 
public $contenido;
private $conn;

public function __construct($titulo,$contenido) {
    $this->titulo = $titulo;
    $this->contenido = $contenido;
    $db = new Connectdb();
    $this->conn = $db->connect();
}

    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);
    } */

}

?>

最后在editardelete.php文件中。

<?php
// should keep session here!
session_start();

include 'BaseDato.class.php';
include 'Posting.class.php'; 

// for a quick check you can use this function
// would check for titulo in GET, POST and SESSION

function is_set($name)
{
    if (isset($_POST[$name]))
    {
        return $_POST[$name];
    }
    elseif (isset($_GET[$name]))
    {
        return $_GET[$name];
    }
    elseif (isset($_SESSION[$name]))
    {
        return $_SESSION[$name];
    }
    else
    {
        return false;
    }
}

// you have to check if titulo and contenido is set
// this would reduce error level to zero!
$result = [];

if ( is_set('titulo') && is_set('contenido'))
{
    $titulo = is_set('titulo');
    $contenido = is_set('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 

    if (count($result) > 0)
    {
        foreach ($result as $key => $res) {     
            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>

我希望这有帮助。维托里亚快乐!

相关问题