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");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条