dongsheng9203
2019-02-04 10:58
浏览 48
已采纳

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?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • 普通网友 2019-02-04 11:41
    最佳回答

    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");
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题