简体   繁体   中英

PHP OOP After migration from PHP5.6 to 7.2 mysql INSERT query doesn't work anymore

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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM