【问题标题】:Not able to insert values into Database using oop concept in php无法使用 php 中的 oop 概念将值插入数据库
【发布时间】:2017-04-18 05:24:07
【问题描述】:

dbclass.php

   <?php  
class DB{
    public static function connect(){
        $conn = mysqli_connect("localhost","root","yash","sample"); 
        return $conn;
    }
}
$dbb = new DB;
$dbb->connect();
?>

classone.php

<?php 
include('dbclass.php');
class Books {
    private  $title;
    private  $price;
    private  $conn;
    function __construct($title,$price){
        $this->title = $title;
        $this->price = $price;
    }
    function getDetails(){
        echo $this->title."</br>";
        echo $this->price;
    }
    public function insertbook(){
        $conn = DB::connect();
        $q1 = "INSERT INTO sbook (title,price) VALUES ($this->title,$this->price)";
        $run = mysqli_query($conn,$q1);
    }

}
$physics =  new Books("physics",20);
$physics->getDetails();
$physics->insertbook();
?>

即使在mysqli_query 中传递了$conn 变量后,我也无法在数据库中插入值。 无法弄清楚,发生了什么。

【问题讨论】:

标签: php database login insert


【解决方案1】:

你应该准备你的 SQL 查询,如下所示

$q1 = "INSERT INTO sbook (title,price) VALUES ('$this->title','$this->price')";

您还应该使用您的私人班级成员$conn 作为$this-&gt;conn

$this->conn = DB::connect();

【讨论】:

  • 感谢它的工作! :),但是由于 $this->title 给了我准确的值,那么在这里使用 ' ' 有什么意义?
  • 当我们使用 $this->conn = DB::connect() FYI 时它不起作用。
  • 您还需要将mysqli_query 中的内容更新为$run = mysqli_query($this-&gt;conn,$q1);
猜你喜欢
  • 2012-01-18
  • 1970-01-01
  • 2015-09-23
  • 2016-02-25
  • 2011-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多