检查表行是否存在?

时间:2015-08-12 12:33:56

标签: php mysql

这里一次只有一个数组。我想检查是否存在任何行,那么该行必须不保存在表中。

function insertcsv(){

            $db = new DB();
            $csvArr = $db->csvToArray($this->csvfile);

            foreach($csvArr as $csvArrData) {
                $checksku = "Select * form product where sku='".$csvArrData['sku']."'";//check this sku is already in table 
                $resultsku = $db->query($checksku);

                if(!empty($resultsku)){
                    echo "<pre>"; print_r($resultsku); exit;
                } else {
                    $sql = "INSERT INTO `product`(`name`, `description`, `short_description`, `sku`, `weight`, `visibility`, `price`, `tax_class`, `image`, `quantity`, `stock_availability`, `category`, `product_status`, `status`, `date_time`) VALUES ('".mysql_real_escape_string($csvArrData['name'])."','".mysql_real_escape_string($csvArrData['description'])."','".mysql_real_escape_string($csvArrData['short_description'])."','".mysql_real_escape_string($csvArrData['sku'])."','".mysql_real_escape_string($csvArrData['weight'])."','".mysql_real_escape_string($csvArrData['visibility'])."','".mysql_real_escape_string($csvArrData['price'])."','".mysql_real_escape_string($csvArrData['tax_class_id'])."','".mysql_real_escape_string($csvArrData['image'])."','".mysql_real_escape_string($csvArrData['qty'])."','".mysql_real_escape_string($csvArrData['is_in_stock'])."','".mysql_real_escape_string($csvArrData['_category'])."','".mysql_real_escape_string($csvArrData['status'])."','Y',now())";

                    $result = $db->query($sql);
                }
            }
            return($result);    
        }

这段代码给了我空白页面。为什么呢?

2 个答案:

答案 0 :(得分:2)

  

这段代码给了我空白页面。为什么呢?

您正在获取空白页面,因为您显示了禁用的PHP运行时错误,但您的脚本&#34;崩溃&#34;因为无效的SQL查询 - &gt;您键入form您需要from

$checksku = "Select * FROM product where sku='".$csvArrData['sku']."'";//check this sku is already in table 

答案 1 :(得分:0)

JavaScript

直到现在,我才注意到它... ...

只需将其复制并粘贴到您的来源。

Regds ..