简体   繁体   English

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

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

I'm quite new to PHP OOP.我对 PHP OOP 很陌生。 But after migration from 5.6 to PHP7.2 and MSQL to MSQLi the INSERT query does not work anymore.但是在从 5.6 迁移到 PHP7.2 并将 MSQL 迁移到 MSQLi 之后,INSERT 查询不再起作用。

ClassDBCon.inc.php 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 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.在同一个 ClassProjects 中,我使用 SELECT 和 UPDATE 查询,这很好用。

Any suggestions?有什么建议?

You are mixing oop and procedural versions together and that is the reason why it does not work.您将 oop 和程序版本混合在一起,这就是它不起作用的原因。

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 语句中,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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