0

I am writing a little script to upload products from a csv file to my website. I have been having an issue where my if($result) echo "hi"; does echo when the $result is present, but if i put if(!result) or if(empty($result) or if(!isset(result)) will not echo when there is no result... In my list of codes from productlist.txt I have 3 product codes, 2 of which are in the database, when I have if($result) it echos 2 'hi' but if I have if(!result) nothing is echoes, when it should be echoing 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);
Melbourne2991
  • 11,707
  • 12
  • 44
  • 82

1 Answers1

0
if( $result->num_rows == 0 ) echo "hi";
furas
  • 134,197
  • 12
  • 106
  • 148
  • Add `else` to that `if`. Are you sure your code run query 3 times ? Maybe ` if($data[3] == $code)` is true only 2 times (I'm only guessing). – furas Jul 14 '13 at 12:46