首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何通过查询获取数据,以便使用面向对象的类创建编辑函数?

如何通过查询获取数据,以便使用面向对象的类创建编辑函数?
EN

Stack Overflow用户
提问于 2018-07-29 06:51:02
回答 1查看 132关注 0票数 0

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

代码语言:javascript
复制
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

加上一些未定义的索引。

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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);
    } */
}
?>

这是另一个名为editardelete.php的页面:

代码语言:javascript
复制
<?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>

这是我的connection类:

代码语言:javascript
复制
<?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;
    }
}
?>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-29 07:28:07

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

代码语言:javascript
复制
<?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();
        }
    }
}


?>

接下来是你的Next类。

代码语言:javascript
复制
<?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文件中。

代码语言:javascript
复制
<?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>

我希望这能帮到你。编码快乐,维多利亚!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51575847

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档