我想制作一个首先检查重复类别的输入框。如果找到重复的类别,则返回false
但如果找不到重复,则将新的类别名称插入数据库。
正确检查重复并正确插入类别但显示此错误:
Catchable fatal error: Object of class mysqli_result could not be converted to
string in C:\wamp\www\ecommerce\class\category.php on line 23
Call Stack
# Time Memory Function Location
1 0.0006 137488 {main}() ..\catadd.php:0
2 0.0146 176512 Category->insertCat() ..\catadd.php:15
以下是导致错误的方法:
public function insertCat($catName)
{
$catName = $this->fm->validation($catName);
$catName = mysqli_real_escape_string($this->db->link, $catName);
if(empty($catName)){
$msg = 'Cat name can not empty';
return $msg;
}else{
$dcatName = "select * from tbl_category where catName='$catName'";
$rowCount = $this->db->select($dcatName);
$catRow = mysqli_num_rows($rowCount);
if( $catRow > 0 ){ /* <---- Line 23 ----- */
echo 'Cat Name already Exist';
}else{
$query = "INSERT INTO tbl_category (catName) VALUES ('$catName')";
$catResult= $this->db->insert($query);
if($catResult){
$msg = 'Cat name Updated';
return $msg;
return $msg;
}else{
$msg = 'Not updated';
return $msg;
}
}
}
}
select
方法如下:
// Select or Read data
public function select($query){
$result = $this->link->query($query) or die($this->link->error.__LINE__);
if($result->num_rows > 0)
{
return $result;
}
else
{
return false;
}
}
在编译时,我收到了这个警告:
警告:mysqli_num_rows()要求参数1为mysqli_result,第22行的C:\ wamp \ www \ ecommerce \ class \ category.php中给出布尔值($ catRow = mysqli_num_rows($ rowCount);是第22行)< / p>
答案 0 :(得分:1)
考虑您的select
方法如下:
public function select($query)
{
$result = $this->link->query($query) or die($this->link->error.__LINE__);
if($result->num_rows > 0){
return $result;
} else {
return false;
}
}
...此方法已检查行数,因此此行:
$catRow = mysqli_num_rows($rowCount);
...不需要,此外,如果select()
方法没有返回任何行,则会导致错误,它会返回false
,当您尝试从{{{{}}引用方法时会产生错误1}}返回。你应该能够做到:
false
修改强>
我应该做的一点是你应该修改你的# This will either return the resource or false so you don't need
# to count rows
$rowCount = $this->db->select($dcatName);
if($rowCount) {
方法(以及你拥有的任何其他查询方法)来接受一个数组作为第二个参数。第二个数组应该让方法知道您需要select()
,以便您可以使用该功能。这可能是SQL注入漏洞:
bind_param