在表中插入值无法在php中使用

时间:2018-07-16 04:34:22

标签: php pdo

我试图在表中插入值,但我不知道发生了什么事,这完全不起作用,我也看不到任何错误。

我不知道我在哪里做错了,任何人都可以帮助我。

这是我的代码:

Database.php

    <?php
$host = 'localhost';
$db   = 'pes';
$user = 'root';
$pass = '';
$charset = 'utf8';

$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$opt = [
    \PDO::ATTR_ERRMODE            => \PDO::ERRMODE_EXCEPTION,
    \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
    \PDO::ATTR_EMULATE_PREPARES   => false,
];
$pdo = new \PDO($dsn, $user, $pass, $opt);
?>

contentprocess.php

   <?php

class ContentProcess {
    /* Properties */
    private $conn;

    /* Get database access */
    public function __construct(\PDO $pdo) {
        $this->conn = $pdo;
    }

    function ContentProcess() {
        global $session;
        if (isset($_POST['createnewcontent'])) {
            $this->createnewcontent();
        } 
      }


     public function createnewcontent()
    {
        $description = $_POST['description'];
        $typeofpage = $_POST['typeofpage'];
        $time = time();
        $q = "INSERT INTO content VALUES ('', '$description', '$typeofpage', '1', '$time', '1', '$time')";
        $this->conn->query($q);
        header("Location:content.php");
    }

    function getcontentdescription() {
         return $this->conn->query("SELECT description FROM content")->fetchAll();
    }

}

表单代码:

content.php

<?php 
include("include/header.php");
include("include/database.php");
include 'contentprocess.php';
?>
    <div class="modal fade" id="createGroup" tabindex="-1" role="dialog" aria-labelledby="createGroup" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content" id="modal-content">
            <form action="contentprocess.php" id="user-groups-create" class="form-horizontal" method="post">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title" id="myModalLabel">Create a New Content</h4>
                </div>
                <div class="modal-body" id="modal-body">                                   
                    <div class="form-group">
                        <label class="col-sm-4 control-label">Description: </label>
                        <div class="col-md-8">
                            <input type="text" id="description" name="description" class="form-control" placeholder="Description" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label  class="col-sm-4 control-label">Content Type: </label>
                        <div class="col-md-8">
                            <select class="form-control" name="typeofpage">
                                            <option value="2" selected="selected">Text</option>
                                            <option value="1">Image</option>
                                            <option value="4">Video</option>
                                            <option value="7">Audio</option>

                           </select>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <input type="hidden" name="createnewcontent" value="1">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="submit" name="submit"  class="btn btn-primary">Create Content</button>
                </div>

            </form>
        </div>
    </div>
</div>

谁能帮助我我犯了什么错误

1 个答案:

答案 0 :(得分:3)

您尚未创建此类“ ContentProcess”的对象。在文件中进行这些更改。 *确保您的SQL查询正确

content.php

<?php 
include("include/header.php");
include("include/database.php");
require_once('contentprocess.php');
$contentProcess = new ContentProcess();
if(isset($_POST['submit'])){
    $contentProcess->createnewcontent();
}
?>
    <div class="modal fade" id="createGroup" tabindex="-1" role="dialog" aria-labelledby="createGroup" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content" id="modal-content">
            <form action="" id="user-groups-create" class="form-horizontal" method="post">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title" id="myModalLabel">Create a New Content</h4>
                </div>
                <div class="modal-body" id="modal-body">                                   
                    <div class="form-group">
                        <label class="col-sm-4 control-label">Description: </label>
                        <div class="col-md-8">
                            <input type="text" id="description" name="description" class="form-control" placeholder="Description" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label  class="col-sm-4 control-label">Content Type: </label>
                        <div class="col-md-8">
                            <select class="form-control" name="typeofpage">
                                            <option value="2" selected="selected">Text</option>
                                            <option value="1">Image</option>
                                            <option value="4">Video</option>
                                            <option value="7">Audio</option>

                           </select>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <input type="hidden" name="createnewcontent" value="1">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="submit" name="submit"  class="btn btn-primary">Create Content</button>
                </div>

            </form>
        </div>
    </div>
</div>

database.php

<?php
class Database{
    function __construct(){
        //config
        $this->host = 'localhost';
        $this->db = 'pes';
        $this->user = 'root';
        $this->pass = '';
        $this->charset = 'utf8';
        
        //connection
        try{
            $this->db = new PDO("mysql:host=$this->host;dbname=$this->db;charset=$this->charset", $this->user, $this->pass);
            return $this->db;
        }
        catch(PDOException $e){
            echo "Connection failed: " . $e->getMessage();
        }
    }

    public function query($q){
        return $this->db->query($q);
    }
}
?>

contentprocess.php

<?php 
require_once './include/database.php';

class ContentProcess {
    private $conn;

    function __construct() {
        $this->conn = new Database();
    }

    
     public function createnewcontent()
    {
        $description = $_POST['description'];
        $typeofpage = $_POST['typeofpage'];
        $time = time();
        $q = "INSERT INTO content(column1, column2,...) VALUES ('', '$description', '$typeofpage', '1', '$time', '1', '$time')"; // use this instead of $q = "INSERT INTO content VALUES ('', '$description', '$typeofpage', '1', '$time', '1', '$time')";
        $this->conn->query($q);
        header("Location:content.php");
    }

    function getcontentdescription() {
        return $this->conn->query("SELECT description FROM content")->fetchAll();
    }

}