我正在进行高级搜索但遇到错误
致命错误:无法访问空属性
*$this->$Personnummer*
这是我的控制器:
<?php
include 'c:/wamp/www/mvc/model/SearchProcessor.php';
// if the user clicks the submit button, it sends its name values here and if not empty, binds them to $data.
if(isset($_POST['button'])) {
if($_POST['name'] !='');
{
$data['name'] = $_POST['name'];
}
if($_POST['lastname'] !='');
{
$data['lastname'] = $_POST['lastname'];
}
if($_POST['Personnummer_search'] !='');
{
$data['Personnummer'] = $_POST['Personnummer_search'];
}
if($_POST['Kon_search'] !='');
{
$data['Kon_search'] = $_POST['Kon_search'];
}
if($_POST['Anvands_search'] !='');
{
$data['Anvands_search'] = $_POST['Anvands_search'];
}
if($_POST['Lan_search'] !='');
{
$data['Lan_search'] = $_POST['Lan_search'];
}
if($_POST['Processormodell_search'] !='');
{
$data['Processormodell_search'] = $_POST['Processormodell_search'];
}
if($_POST['Sida_search'] !='');
{
$data['Sida_search'] = $_POST['Sida_search'];
}
if($_POST['utlamnat_search'] !='');
{
$data['utlamnat_search'] = $_POST['utlamnat_search'];
}
if($_POST['ProcessorSerie_search'] !='');
{
$data['ProcessorSerie_search'] = $_POST['ProcessorSerie_search'];
}
if($_POST['Tillverkare_search'] !='');
{
$data['Tillverkare_search'] = $_POST['Tillverkare_search'];
}
$displayResults = new SearchProcessor($db,$data);
$Results = $displayResults->getSearchResult();
以下是模型:
<?php
require_once 'Database.php';
//This class is for searching for patient and their processors
class SearchProcessor extends Database {
private $Personnummer;
private $TheKon;
private $TheLan;
private $name;
private $TheLastname;
private $TheProcessor;
private $TheAnvands;
private $TheSida;
private $TheUtlamnat;
private $TheSerienummer;
private $TheTillverkare;
//The constructor of both the parent and child class
function __construct(mysqli $db, $data)
{
parent::__construct($db);
//set data
$this->setData($data);
// get search result
$this->getSearchResult();
}
//Sets the data
function setData($data)
{
$this->name = $data['name'];
$this->TheLastname = $data['lastname'];
$this->Personnummer = $data['Personnummer'];
$this->TheKon = $data['Kon_search'];
$this->TheAnvands = $data['Anvands_search'];
$this->TheLan = $data['Lan_search'];
$this->TheProcessor = $data['Processormodell_search'];
$this->TheSida =$data['Sida_search'];
$this->TheUtlamnat =$data['utlamnat_search'];
$this->TheSerienummer = $data['ProcessorSerie_search'];
$this->TheTillverkare = $data['Tillverkare_search'];
}
//This function searches each column regarding patient and its processors.
function getSearchResult() {
$where = array();
$where[] = "Patient.Patient LIKE '%".$this->$Personnummer."%'"; //<--- ERROR Cannot access empty property
$where[] = "person.Namn LIKE '%".$this->name."%'";
$where[] = "person.Efternamn LIKE '%".$this->TheLastname."%'";
$where[] = "person.Kon LIKE '%".$this->TheKon."%'";
$where[] = "processorpatient.Sida LIKE '%".$this->TheSida."%'";
$where[] = "person.Lan LIKE '%".$this->TheLan."%'";
$where[] = "processorpatient.Tillverkare LIKE '%".$this->TheTillverkare."%'";
$where[] = "processorpatient.Processor LIKE '%".$this->TheProcessor."%'";
$where[] = "processorpatient.Utlamnat LIKE '%".$this->TheUtlamnat."%'";
$where[] = "processorpatient.Anvands LIKE '%".$this->TheAnvands."%'";
$where[] = "processorpatient.Serienummer LIKE '%".$this->TheSerienummer."%'";
if(count($where)) // here it counts the amount of $where and a extends the query to search deeper into the database.
{
$Data = array();
$sql = "Select * from patient left join person on person.Personnummer = patient.Patient left join processorpatient on processorpatient.patientv = patient.Patient
WHERE ".implode(" AND ",$where);
if(!$result = $this->mysqli->query($sql)) {
throw new exception("Error: Can not execute the query.");
} else {
$Count = $result->num_rows;
if($Count>0)
{
for($i=0; $i<$Count; $i++)
{
$Data[$i] = $result->fetch_assoc();
}
}
}
return $Data;
}
}
}
我想要做的是将用户输入$ _POST [&#39;&#39;]分配给控制器中的变量$ data [&#39;&#39;]。然后将它应该与它们自己的私有变量绑定的模型类传递给它,在私有变量中为查询赋值。但我收到错误无法访问模型中的空属性 $this->$Personnummer
答案 0 :(得分:1)
不使用$
访问变量,就像上面定义的一样
$this->Personnummer = $data['Personnummer'];
所以请访问
$where[] = "Patient.Patient LIKE '%".$this->Personnummer."%'";