PHP OOP从PHP5.6迁移到7.2后,mysql INSERT查询不再起作用

PHP OOP从PHP5.6迁移到7.2后,mysql INSERT查询不再起作用

问题描述:

I'm quite new to PHP OOP. But after migration from 5.6 to PHP7.2 and MSQL to MSQLi the INSERT query does not work anymore.

ClassDBCon.inc.php

class Dbh {
  private $dbhost;
  private $dbuser;
  private $dbpass;
  private $dbname;

  protected function connect() {
    $this->dbhost = "127.0.0.1:3307";
    $this->dbuser = "root";
    $this->dbpass = "mypassword";
    $this->dbname = "mydatabase";

    $conn = new mysqli($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname);
    if(mysqli_connect_errno())
    {
        echo "failed to connect to mysql:" . mysqli_connect_error();
    }

    return $conn;
  }

}

ClassProjects.inc.php

    class Projects extends Dbh{

        public function CreateNewProject($projectnr,$projectname,$iprange,$language,$pm,$hwe,$swe,$amount_cpu,$amount_hmi) {

            //Add new project
            $sql = $this->connect()->query("INSERT INTO tbl_projects(no,name,status,ip,language,pm,hwe,swe,amount_cpu,amount_hmi)
            VALUES('$projectnr','$projectname','1','$iprange','$language','$pm','$hwe','$swe','$amount_cpu','$amount_hmi')");

            MsgBox("Project successfully created");

            return $sql;
        }
}

In the same ClassProjects i use the SELECT and UPDATE query and thats works fine.

Any suggestions?

You are mixing oop and procedural versions together and that is the reason why it does not work.

Do get it to work you need to write it like this :

$sql = "INSERT INTO tbl_projects(no,name,status,ip,language,pm,hwe,swe,amount_cpu,amount_hmi)
        VALUES('$projectnr','$projectname','1','$iprange','$language','$pm','$hwe','$swe','$amount_cpu','$amount_hmi')";

$this->connect()->query($sql);

Edit:

If you want to store a notification add it into if statement like this instead:

if($this->connect()->query($sql) {
    MsgBox("Project successfully created");
}