我正在编写一个小脚本来将产品从 csv 文件上传到我的网站。我一直有一个问题,if($result) echo "hi";
当 $result 存在时我会回显,但是如果我在没有结果时输入或不回显...在我来自 productlist.txt 的代码列表中,我有 3 个产品代码if(!result)
,if(empty($result)
2if(!isset(result))
其中在数据库中,当我有 if($result) 时它会回显 2 'hi' 但如果我有 if(!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);