我对PHP OOP还是很陌生。但是在从5.6迁移到PHP7.2以及从MSQL迁移到MSQLi之后,INSERT查询不再起作用。
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;
}
}
在相同的ClassProjects中,我使用SELECT和UPDATE查询,那很好。
有什么建议吗?
答案 0 :(得分:2)
您将oop版本和过程版本混合在一起,这就是它不起作用的原因。
要使其正常工作,您需要这样编写:
$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);
编辑:
如果要存储通知,则将其添加到如下的if语句中:
if($this->connect()->query($sql) {
MsgBox("Project successfully created");
}