PHP脚本无法识别缺少的变量
我正在写一个小脚本,将产品从csv文件上传到我的网站。我遇到了一个问题,我的PHP脚本无法识别缺少的变量,php,mysql,mysqli,php-5.4,Php,Mysql,Mysqli,Php 5.4,我正在写一个小脚本,将产品从csv文件上传到我的网站。我遇到了一个问题,我的if($result)回显“hi”会回显,但如果我将if(!result)或if(empty($result)或if(!isset(result))放在productlist.txt的代码列表中,当我有if($result)时,它会回显2个“hi”,但如果我有if($result),则不会回显(!result)没有任何东西是回声,当它应该回声1 hi时 while($data = fgetcsv($handle)) {
if($result)回显“hi”当$result出现时,code>会回显,但如果我将if(!result)
或if(empty($result)
或if(!isset(result))
放在productlist.txt的代码列表中,当我有if($result)时,它会回显2个“hi”,但如果我有if($result),则不会回显(!result)没有任何东西是回声,当它应该回声1 hi时
while($data = fgetcsv($handle)) {
//Gets products to be added from txt file.
$codes = explode("\n", file_get_contents("productlist.txt"));
foreach($codes as $code) {
if($data[3] == $code) {
$name = $data[2];
$model = $data[3];
$description = strip_tags($data[6]);
echo $price = round($data[7] + 20 + (0.05 * $data[7])) - 0.05; echo "</br>";
$image = "data/W_Sexy_Lingerie_Series_Lingerie-Supplies/" . $model . ".jpg";
//Check to see if product already exists
$query = "SELECT `product_id` FROM `oc_product` WHERE `model`='$model'";
$result = $con->query($query);
if(empty($result)) echo "hi";
break;
//Insert product information
$query = "INSERT INTO `oc_product` (`model`, `quantity`, `image`, `price`, `status`) VALUES
('$model', '5', '$image', '$price', 1)";
if (!$con->query($query)) {
echo $con->error;
}
//Get product ID
$query = "SELECT `product_id` FROM `oc_product` WHERE `model`='$model'";
$result = $con->query($query);
$rows = $result->fetch_array();
$product_id = $rows[0];
//Insert product category
$query = "INSERT INTO `oc_product_to_category` (`product_id`, `category_id`) VALUES ('$product_id', 59)";
if (!$con->query($query)) {
echo $con->error;
}
//Insert product description
$query = "INSERT INTO `oc_product_description` (`product_id`, `language_id`, `name`, `description`,"; $query .= " `meta_description`, `meta_keyword`)";
$query .= " VALUES ('$product_id', 1, '$name', '$description', '$description', '$description')";
if (!$con->query($query)) {
echo $con->error;
}
}
}
}
}
newProducts($handle,$con);
while($data=fgetcsv($handle)){
//从txt文件获取要添加的产品。
$code=explode(“\n”,文件获取内容(“productlist.txt”);
foreach($code作为$code){
如果($data[3]==$code){
$name=$data[2];
$model=$data[3];
$description=strip_标签($data[6]);
echo$price=四舍五入($data[7]+20+(0.05*$data[7])-0.05;echo“”;
$image=“data/W_Sexy_内衣_Series_内衣用品/”$model..jpg”;
//检查产品是否已经存在
$query=“从'oc\U product'中选择'product\U id',其中'model'='$model';
$result=$con->query($query);
if(空($result))回显“hi”;
打破
//插入产品信息
$query=“插入到'oc_product'('model'、'quantity'、'image'、'price'、'status')值中
("模型","5","图像","价格"1);;
如果(!$con->query($query)){
echo$con->错误;
}
//获取产品ID
$query=“从'oc\U product'中选择'product\U id',其中'model'='$model';
$result=$con->query($query);
$rows=$result->fetch_array();
$product_id=$rows[0];
//插入产品类别
$query=“插入'oc_product_to_category'('product_id','category_id')值('$product_id',59)”;
如果(!$con->query($query)){
echo$con->错误;
}
//插入产品说明
$query=“插入到'oc_产品描述'('product_id','language_id','name','description',”;$query.=“'meta_描述','meta_关键字');
$query.=“值('$product_id',1',$name','$description','$description','$description')”;
如果(!$con->query($query)){
echo$con->错误;
}
}
}
}
}
新产品($handle,$con);
query()可能会返回不同类型的对象,而不是真/假结果。当出现错误时,query通常会返回null
或false
。var转储向我显示只返回了2$个结果。将else
添加到if
中。你确定你的代码会运行查询3次吗?可能是'if'($data[3]==$code)`仅为真2次(我只是猜测)。
if( $result->num_rows == 0 ) echo "hi";