使用PHP mysqli oops将记录插入数据库时​​出现问题

时间:2016-07-23 12:14:47

标签: php jquery mysql ajax oop

我正在使用PHP mysqli来访问并将记录插入到数据库中并且还准备好了语句,但某处有一个错误我无法弄清楚...指出错误将非常有帮助

mailer.php

/Users/yatish/Library/Developer/CoreSimulator/Devices/65048208-DC46-4B30-9526-470D6D8081D3/data/Containers/Data/Application/2E7EFDA0-9C0C-4440-962D-610C2AC18DDD/Documents

此外,我正在使用包含表单和脚本的外部文件中的ajax调用

的index.php

<?php 

class Submit {

    const DB = 'localhost',
          USER = 'test',
          PASS = '123456',
          DB_NAME = 'testing';


  private $mysql;


  public function __construct() {

      $this->mysql = new mysqli(self::DB , self::USER , self::PASS , self::DB_NAME);

      if ($this->mysql->connect_errno) {
          echo "Error: " . $this->mysql->connect_error;
          echo "<br>";
          echo "Error code: " . $this->mysql->connect_errno;
      }


    }

    public function addRecord($record) {

        $status = false;

        $query = "INSERT INTO mytable (name,message) VALUES (?,?)";
        $stmt = $this->mysql->prepare($query);

        if ( $stmt ) {

            $stmt->bind_param('ss', $record->name , $record->message);

            if ($stmt->execute()) {
                $status = ($stmt->affected_rows == 1) ? true : false;

                $stmt->fetch_object();
                $stmt->close();
            }
        }

        return $status;
    }

}


$submit = new Submit();

$result = null;

if (isset($_POST['submit']) ) {
    $name = isset($_POST['name']) ? trim($_POST['name']) : '';
    $message = isset($_POST['message']) ? trim($_POST['message']) : '';

    $result = $submit->addRecord($name,$message);

    if ($result) {
        echo "Message Saved";


    }
}

1 个答案:

答案 0 :(得分:0)

你给addRecord()方法提供了两个参数,但它只需要1.但是,似乎它需要一个你没有初始化的对象,所以我调整它,所以它需要你给它的两个参数。

public function addRecord($name, $message) {

    $status = false;

    $query = "INSERT INTO mytable (name,message) VALUES (?,?)";
    $stmt = $this->mysql->prepare($query);

    if ( $stmt ) {

        $stmt->bind_param('ss', $name , $message);

        if ($stmt->execute()) {
            $status = $stmt->affected_rows === 1;
        }
    }
    return $status;
}

我还删除了方法中的一些不必要的步骤:

$status = ($stmt->affected_rows == 1) ? true : false;
$status = $stmt->affected_rows === 1; 

比较本身将返回一个布尔值,因此不需要使用显式结构。

$stmt->fetch_object();
$stmt->close();

在不使用对象的情况下获取对象是一种浪费。 当离开方法的范围时,垃圾收集器将取消设置stmt。

测试功能的代码:

class Submit {

const DB = 'localhost',
      USER = 'test',
      PASS = '123456',
      DB_NAME = 'testing';
  private $mysql;

  public function __construct() {
      $this->mysql = new mysqli(self::DB , self::USER , self::PASS , self::DB_NAME);
      if ($this->mysql->connect_errno) {
          echo "Error: " . $this->mysql->connect_error;
          echo "<br>";
          echo "Error code: " . $this->mysql->connect_errno;
      }
    }

    public function addRecord($name, $message) {
        $status = false;
        $query = "INSERT INTO mytable (name,message) VALUES (?,?)";
        $stmt = $this->mysql->prepare($query);
        if ( $stmt ) {
            $stmt->bind_param('ss', $name , $message);
            if ($stmt->execute()) {
                $status = $stmt->affected_rows === 1;
            }
        }
        return $status;
    }
}


$submit = new Submit();

$result = null;
$name = "dsfdsf";
$message = "message";
$result = $submit->addRecord($name,$message);
var_dump($result); // bool(true)