未捕获的argumentCountError

时间:2018-04-30 12:56:47

标签: php forms oop

我在这里有错误,它说我有未被捕获的arugmentcount错误,但我只是按照教程它没有错误,但当我这样做时。我检查了所有可能的错误,甚至是拼写和半冒号

  

致命错误:未捕获ArgumentCountError:函数database :: bind()的参数太少,第15行传入C:\ xampp \ htdocs \ ooplogin \ index.php,C:\ xampp \中至少传递2个htdocs中\ ooplogin \类\ database.php中:35
  堆栈跟踪:
  #0 C:\ xampp \ htdocs \ ooplogin \ index.php(15):database-> bind(':title,:$ title')
  在第35行的C:\ xampp \ htdocs \ ooplogin \ classes \ database.php中抛出#1 {main}

这是我index.php

的代码示例
<?php
require 'classes/database.php';

$database = new database;

$database->query('SELECT * FROM post');
$rows = $database->resultset();

$post = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);

if($post['submit']){
    $title = $post['title'];
    $body = $post['body'];
    $database->query('INSERT INTO post(title, body) VALUE (:title, :body)');
    $database->bind(':title, :$title');
    $database->bind(':body, $body');
    $database->execute();
    if($database->lastInsertId()){
        echo '<p>Post Added!</p>' ;
    }

}

?>
    <h1>Add Post</h1>
    <label>Post Title</label>
    <br />
    <form method="post" action=" <?php $_SERVER['PHP_SELF']; ?>">
        <input type="text" name="title" placeholder="Add Title..">
        <br />
        <label>Post Body</label>
        <br />
        <textarea name="body" cols="30" rows="10"></textarea>
        <br>
        <input type="submit" name="submit" value="submit">
    </form>
    <h1>POSTS</h1>
    <div>
        <?php foreach($rows as $row) : ?>
        <div>
            <h3>
                <?php echo $row['title']; ?>
            </h3>
            <p>
                <?php echo $row['body']; ?>
            </p>
        </div>
        <?php endforeach; ?>
    </div>


and for classes/database.php`

    <?php

class database{
    private $host = 'localhost';
    private $user = 'root';
    private $password = '';
    private $dbname = 'oopdtbs';

    private $dbh;
    private $error;
    private $stmt;



    public function __construct(){
        // Set DSN
        $dsn = 'mysql:host='. $this->host . ';dbname='. $this->dbname;
        // Set Options
        $options = array(
            PDO::ATTR_PERSISTENT        => true,
            PDO::ATTR_ERRMODE       => PDO::ERRMODE_EXCEPTION
        );
        // Create new PDO
        try {
            $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
        } catch(PDOEception $e){
            $this->error = $e->getMessage();
        }
    }

    public function query($query){
        $this->stmt = $this->dbh->prepare($query);
    }

    public function bind($param, $value, $type = null){
        if(is_null($type)){
            switch(true){
                case is_int($value):
                    $type = PDO::PARAM_INT;
                    break;
                case is_bool($value):
                    $type = PDO::PARAM_BOOL;
                    break;
                case is_null($value):
                    $type = PDO::PARAM_NULL;
                    break;
                    default:
                    $type = PDO::PARAM_STR;
            }
        }
        $this->stmt->bindValue($param, $value, $type);
    }

    public function execute(){
        return $this->stmt->execute();
    }

    public function lastInsertId(){
        $this->dbh->lastInsertId();
    }

    public function resultset(){
        $this->execute();
        return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
    }
}

1 个答案:

答案 0 :(得分:0)

您在class Location { private: double x_, y_; public: Location(double x, double y) : x_(x), y_(y) { std::cout << "myconstrct " << std::endl; } Location() { std::cout << "default " << std::endl; x_ = drand48(); y_ = drand48(); } Location(const Location & L) : x_(L.x_), y_(L.y_) { std::cout << "copy " << std::endl; } }; class Salesman { private: Location start_; protected: std::vector<Location> tobevisited_; public: Salesman(const Location & start, std::vector<Location>::iterator it1, std::vector<Location>::iterator it2) { std::cout << "\nInside Willy (until **): " << std::endl; Location start_(start); for(std::vector<Location>::iterator it = it1; it != it2; it++) { std::cout << "*" << std::endl; tobevisited_.push_back(*(it)); } std::cout << "**" << std::endl; } }; 来电中只传递了一个参数。我解决了这个问题,并且我也解决了拼写错误。

替换:

->bind(..)

使用这些行:

 $database->bind(':title, :$title');
 $database->bind(':body, $body');