简体   繁体   中英

Pdo insert query in for loop

Where am i doing wrong? Count($sepetler) => 2 row.İts ok.Not problem here values key in variables truth not problem here.But not working exec.All of code here.I trust to $sepet array and variables but.İnsert statement is problem.Please help me for find eror

    $dbConfig=new DBConfig();
    $baglanti = new PDO("mysql:host=".$dbConfig->host.";dbname=".$dbConfig->dbname, $dbConfig->dbuser, $dbConfig->password);
    $baglanti->exec("SET NAMES utf8");
    $baglanti->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $siparis_kodu=$post["merchant_oid"];
    $sepetGetirSql = "select * from sepetler where siparis_kodu='".$siparis_kodu."'";
    //
    $sepetler = array();


  $queryResultSepetList = $baglanti->query( $sepetGetirSql, PDO::FETCH_ASSOC );
  if ( $queryResultSepetList->rowCount() > 0 ) {
      foreach ( $queryResultSepetList as $row ) {
          array_push( $sepetler, $row );
        }
      }
            $sepet_say=count($sepetler);


            for ($i=0; $i <count($sepetler) ; $i++) {
                $statement = $baglanti->prepare("SELECT urun_adi,urun_fiyati FROM urunler WHERE urun_id = :urun_id");
                $statement->execute(array( ":urun_id" => $sepetler[0]["urun_id"] ));
                $urun_bilgileri = $statement->fetch(PDO::FETCH_ASSOC);

                $query = $baglanti->prepare("INSERT INTO siparisler SET
                  urun_id = ?,
                  miktar = ?,
                  urun_fiyati = ?,
                  siparis_kodu = ?,
                  kargo_takip_no = ?,
                  odeme_durumu = ?,
                  uye_id = ?");
                  $insert = $query->execute(array(
                    $sepetler[$i]["urun_id"], $sepetler[$i]["miktar"], $urun_bilgileri["urun_fiyati"],$sepetler[$i]["siparis_kodu"],"",0,$sepetler[$i]["uye_id"]
                  ));
                  $mesaj="başarılı sipariş";
              }

It looks like you are using the UPDATE syntax for an INSERT statement.

It should be:

INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...); 

or if you are filling all of the columns in order then you can shorten it to:

INSERT INTO table_name VALUES (value1, value2, value3, ...); 
  $prepare = $baglanti->prepare("INSERT INTO siparisler SET urun_id = :urun_id,miktar=:miktar,urun_fiyati=:urun_fiyati,siparis_kodu=:siparis_kodu,kargo_takip_no=:kargo_takip_no,odeme_durumu=:odeme_durumu,uye_id=:uye_id");

  $veriler = [
      "urun_id" =>  $sepetler[$i]["urun_id"],
      "miktar" =>  $sepetler[$i]["miktar"],
      "urun_fiyati" =>  $urun_bilgileri["urun_fiyati"],
      "siparis_kodu" =>  $sepetler[$i]["siparis_kodu"],
      "kargo_takip_no" =>  "",
      "odeme_durumu" => 1,
      "uye_id" => $sepetler[$i]["uye_id"]
  ];
  // verileri çalıştır
  $prepare->execute($veriler);

İt's working for me.Problem is fixed.Thank you all of them

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM