Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 正在将重复记录插入数据库_Php - Fatal编程技术网

Php 正在将重复记录插入数据库

Php 正在将重复记录插入数据库,php,Php,我试图弄明白,为什么有时会将重复的产品插入数据库。我就是没找到。下面是代码 这是存储产品数据的会话转储的外观: array (size=5) 'totalcount' => int 8 'totalprice' => int 11592 '54e22beb92d83' => array (size=7) 'pieces' => string '1' (length=1) 'id' => string '41' (leng

我试图弄明白,为什么有时会将重复的产品插入数据库。我就是没找到。下面是代码

这是存储产品数据的会话转储的外观:

array (size=5)
  'totalcount' => int 8
  'totalprice' => int 11592
  '54e22beb92d83' => 
    array (size=7)
      'pieces' => string '1' (length=1)
      'id' => string '41' (length=2)
      'name' => string 'Chardonnay 2009' (length=15)
      'specs_price' => int 0
      'price' => int 599
      'total' => int 599
      'code' => string '' (length=0)
  '54e22bf562387' => 
    array (size=7)
      'pieces' => string '2' (length=1)
      'id' => string '42' (length=2)
      'name' => string 'Chardonnay 2010' (length=15)
      'specs_price' => int 0
      'price' => int 499
      'total' => int 998
      'code' => string '' (length=0)
  '54e22bf79c386' => 
    array (size=7)
      'pieces' => string '5' (length=1)
      'id' => string '43' (length=2)
      'name' => string 'Chardonnay 1993' (length=15)
      'specs_price' => int 0
      'price' => int 1999
      'total' => int 9995
      'code' => string '' (length=0)
我认为这是不言自明的,但可以肯定的是,数组由totalcount、totalprice、sale_代码(如果存在)组成,其他键是basket中的产品

以下是订单结束时将数据保存到数据库的流程代码:

foreach ($_SESSION[self::BASKET_IDENTIFIER] as $key => $values) {
    if ($key != 'totalcount' && $key != 'totalprice' && $key != 'sale_code') {
        $spec = '';
        $speccost = 0;
        if (isset($values['spec']) && is_array($values['spec'])) {
            foreach ($values['spec'] as $id => $value) {
                $specname = $this->mysqli->query("SELECT `name` FROM `products_types_properties` WHERE `id`='$id'")->fetch_assoc()["name"];
                $spec .= "<br>$specname: $value";
            }
            foreach ($values['spec_cost'] as $id => $value) {
                $speccost += $value;
            }
        } else {
            $spec = null;
        }
        if (isset($values['voucher_price'])) {
            $voucher_price = "'$values[voucher_price]'";
        } else {
            $voucher_price = "NULL";
        }
        $query = "INSERT INTO `orders_products`
                            (
                            `order_id`,
                            `product_id`,
                            `pieces`,
                            `spec`,
                            `spec_costs`,
                            `voucher_price`,
                            `price`
                            ) VALUES (
                            '$order_id',
                            '$values[id]',
                            '$values[pieces]',
                            '$spec',
                            '0',
                            $voucher_price,
                            '$values[price]'
                            )";
        $this->mysqli->query($query);
        if ($this->mysqli->error) {
            echo $this->mysqli->error;
            exit;
        }
    }
    }
foreach($\u会话[self::BASKET\u标识符]作为$key=>$value){
如果($key!='totalcount'&&&$key!='totalprice'&&$key!='sale\u code'){
$spec='';
$speccost=0;
if(isset($values['spec'])和&is_数组($values['spec'])){
foreach($value['spec']作为$id=>$value){
$specname=$this->mysqli->query(“从`products\'types\'properties`WHERE`id`='$id')中选择`name`)->fetch_assoc()[“name”];
$spec.=“
$specname:$value”; } foreach($value['spec\u cost']作为$id=>$value){ $speccost+=$value; } }否则{ $spec=null; } 如果(isset($values['凭证价格])){ $COUSERY_price=“$values[COUSERY_price]”; }否则{ $凭证价格=“空”; } $query=“插入到`订单\产品中”` ( `订单号`, `产品id`, `件`, `规格`, `规格成本`, `优惠券价格`, `价格` )价值观( “$order_id”, “$values[id]”, “$values[件]”, “$spec”, '0', $U价格, “$价值[价格]” )"; $this->mysqli->query($query); 如果($this->mysqli->error){ echo$this->mysqli->error; 出口 } } }
真正奇怪的是,相同的订单在发送到电子邮件的PDF文件中得到了很好的处理,但它从相同的数据中不知何故将不良记录插入数据库

这是生成PDF的代码:

protected function PDF($ordernum) {
    $basket = new Basket();
    $basket->recount();
    $delivery = new Delivery($this->delivery);
    $pdf = new mPDF("utf8");
    $country = $this->mysqli->query("SELECT `name` FROM `countries` WHERE `id`='$this->country'")->fetch_assoc()["name"];
    $css = file_get_contents(PATH . "/config/pdf_template/template.css");
    $html = file_get_contents(PATH . "/config/pdf_template/template.html");
    $html = str_replace("{{cislo_objednavky}}", $ordernum, $html);
    $html = str_replace("{{jmeno_prijmeni}}", "$this->name $this->surname", $html);
    $html = str_replace("{{ulice}}", $this->street, $html);
    $html = str_replace("{{psc}}", $this->zip, $html);
    $html = str_replace("{{mesto}}", $this->city, $html);
    $html = str_replace("{{stat}}", $country, $html);
    $html = str_replace("{{doprava}}", getCzechPriceFormat($this->delivery_price[$this->delivery]), $html);
    $html = str_replace("{{nazev_dopravy}}", $delivery->getTitle(), $html);
    $totalprice = $basket->totalprice + $this->delivery_price[$this->delivery];
    if ($totalprice < 0) {
        $totalprice = 0;
    }
    $html = str_replace("{{celkem}}", getCzechPriceFormat($totalprice), $html);

    $parts = "";
    $parts .= "<table width='100%'>";
    $parts .= "<tr>";
    $parts .= "<th>Název</th>";
    $parts .= "<th>Počet ks</th>";
    $parts .= "<th>Cena za kus</th>";
    $parts .= "<th>Celkem</th>";
    $parts .= "</tr>";
    foreach ($_SESSION[self::BASKET_IDENTIFIER] as $key => $values) {
        if (!in_array($key, Basket::$banned_slugs)) {
            $spec = '';
            if (is_array($values['spec'])) {
                foreach ($values['spec'] as $id => $value) {
                    $specname = $this->mysqli->query("SELECT `name` FROM `products_types_properties` WHERE `id`='$id'")->fetch_assoc()["name"];
                    $spec .= "<br>$specname: $value";
                }
            } else {
                $spec = null;
            }
            $part = "";
            $part .= "<tr>";
            $part .= "<td><strong>$values[name]</strong>$spec</td>";
            $part .= "<td>$values[pieces]</td>";
            $part .= "<td>" . getCzechPriceFormat($values['price']) . "</td>";
            $part .= "<td>" . getCzechPriceFormat($values['total']) . "</td>";
            $part .= "</tr>";
            $parts .= $part;
        }
    }
    $parts .= "</table>";

    $html = str_replace("{{products}}", $parts, $html);
//      var_dump($html);exit;

    $pdf->WriteHTML($css, 1);
    $pdf->WriteHTML($html);
    $pdf->Output(PATH . "/pdf/orders/$ordernum.pdf");
}
受保护函数PDF($ordernum){
$basket=新篮子();
$basket->重新计票();
$delivery=新交货($this->delivery);
$pdf=新的强制性公积金(“utf8”);
$country=$this->mysqli->query(“从`countries`中选择`name`其中`id`='$this->country'”)->fetch_assoc()[“name”];
$css=file_get_contents(路径“/config/pdf_template/template.css”);
$html=file_get_contents(路径“/config/pdf_template/template.html”);
$html=str_replace(“{{cislo_objednavky}}},$ordernum,$html);
$html=str_replace(“{{jmeno_prijmeni}}},“$this->name$this->name”,$html);
$html=str_replace(“{{ulice}},$this->street,$html);
$html=str_replace(“{{psc}},$this->zip,$html);
$html=str_replace(“{{mesto}},$this->city,$html);
$html=str_replace(“{{stat}}”,$country,$html);
$html=str_replace(“{{doprava}}”,getCzechPriceFormat($this->delivery)[$this->delivery]),$html);
$html=str_replace(“{nazev_dopravy}},$delivery->getTitle(),$html);
$totalprice=$basket->totalprice+$this->delivery_price[$this->delivery];
如果($totalprice<0){
$totalprice=0;
}
$html=str_replace(“{{celkem}”),getCzechPriceFormat($totalprice),$html);
$parts=“”;
$parts.=”;
$parts.=”;
$parts.=“Název”;
$parts.=“Počet ks”;
$parts.=“塞纳扎库斯”;
$parts.=“Celkem”;
$parts.=”;
foreach($\u会话[self::BASKET\u标识符]作为$key=>$value){
if(!in_数组($key,Basket::$banked_slug)){
$spec='';
if(是_数组($values['spec'])){
foreach($value['spec']作为$id=>$value){
$specname=$this->mysqli->query(“从`products\'types\'properties`WHERE`id`='$id')中选择`name`)->fetch_assoc()[“name”];
$spec.=“
$specname:$value”; } }否则{ $spec=null; } $part=“”; $part.=”; $part.=“$values[name]$spec”; $part.=“$value[件]”; $part.=''.getCzechPriceFormat($values['price']); $part.=''.getCzechPriceFormat($values['total']); $part.=”; $parts.=$part; } } $parts.=”; $html=str_replace(“{{products}}”,$parts,$html); //变量转储($html);退出; $pdf->WriteHTML($css,1); $pdf->WriteHTML($html); $pdf->Output(路径“/pdf/orders/$ordernum.pdf”); }

有人能帮我弄清楚吗?我真的找不到原因,这很遗憾,因为整个系统都是我写的。提前谢谢。

您可以在数据库中设置
唯一的
约束或
主键
吗?可以。我忘了提到,ID总是唯一的(它是主键和自动增量),只是数据重复。