Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我如何通过查询获取数据,以便创建一个与OOP相关的类的编辑函数?_Php_Mysql_Oop - Fatal编程技术网

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

Php 我如何通过查询获取数据,以便创建一个与OOP相关的类的编辑函数?,php,mysql,oop,Php,Mysql,Oop,请帮忙。我不知所措!!我正在创建一个博客网站,管理员可以编辑和删除他们的文章。但是,当我通过查询时: 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):

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

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


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

}

?>

下一个是你的发帖课

<?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>

最后在editardelet.php文件中


主页
提特罗
孔蒂多

我希望这有帮助。维多利亚快乐

我可以看到Connectdb类吗?我把它放在了名为connection的底部。属性
$this->conn
在哪里声明和初始化?将
public$conn
添加到
Connectdb
类中,并在
connect
方法中初始化它。在
Posting
conn
中从未定义或传递到。我根本找不到您在哪里使用
Connectdb
。您从不调用
Posting::connect
,此方法不应返回
$conn
,而应将其设置为私有参数。@Jeff这是几个问题之一。:)大部分我都同意。只是Connectdb的扩展让我没有按+1。谢谢你的工作,那是我迟迟不愿做的1.谢谢你的帮助。是的,肯定存在结构性问题。它起作用了。非常感谢。
<?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>