2016-05-16 10 views
-1

Ich muss einige Zeilen in der Tabelle Bestellungen aktualisieren.PDO PHP - Aktualisierung Zeile funktioniert nicht - kein Fehler

Mein Code läuft völlig in Ordnung, aber wenn ich meine phpmyadmin überprüfe, werden keine Zeilen aktualisiert. Ich habe auch versucht, eine andere Spalte aus der gleichen Tabelle zu aktualisieren, die auch nicht funktionierte.

Kann mir jemand helfen?

(Ich habe versucht, eine Zeile in einer anderen Tabelle zu aktualisieren, und das schien gut zu funktionieren.)

PHP:

<?php 
    require '../app/db.php'; 
    require '../app/init.php'; 
    if (!isset($_SESSION['loggedin']) || !$_SESSION['loggedin']) { 
     header('Location: '.ADMIN_URL); 
     exit; 
    } 
    //Get all orders 
    $sth = $db->prepare("SELECT id, invoice_id, order_id, order_completed_at, total_shipping, shipping_tax, owner_site_name, export_csv FROM orders WHERE id > 1573"); 
    $sth->execute(); 
    $results = $sth->fetchAll(PDO::FETCH_ASSOC); 
    $results = json_decode(json_encode($results), 1); 
    foreach ($results as $order => $value) { 
     $csv = json_decode($value['export_csv'], 1); 
     $total_shipping = number_format($value['total_shipping']+$value['shipping_tax'], 2, ',', '.'); 
     $date = date("d-m-Y", strtotime(explode(' ', $value['order_completed_at'])[0])); 
     $shipping = $date.';-'.$value['invoice_id'].';0;"1050";"";"'.$value['owner_site_name'].' (ID: '.$value['order_id'].')";'.$total_shipping.';"DKK";100,00;"Salg";"";0;'.$date.';0,00;;"";"";0,00;0;"";0;"";"";"";"";"";0;0,00;"";"";"";"";"";0'; 
     $csv['separated']['shipping'] = $shipping."\n"; 
     $joins = explode("\n", $csv['joined']); 
     foreach ($joins as $join => $value) { 
      $explode = explode(';', $value); 
      if (isset($explode[3])) { 
       if ($explode[3] == '"1040"') { 
        $joins[$join] = $shipping; 
       } 
      } 
     } 
     $csv['joined'] = implode("\n", $joins); 
     $csv = json_encode($csv); 
     //Update export_csv 
     $sth = $db->prepare("UPDATE `orders` SET `export_csv` = :csv WHERE `id` = :id"); 
     $sth->bindParam(':csv', $csv); 
     $sth->bindParam(':id', $value['id']); 
     $res = $sth->execute(); 
     if (!$res) { 
      echo 'Failed for order #'.$value['id'].'<br>'; 
     } 
    } 
    ?> 

Tabellenstruktur:

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; 
    SET time_zone = "+00:00"; 

    -- 
    -- Database: `xxxx` 
    -- 

    -- -------------------------------------------------------- 

    -- 
    -- Struktur-dump for tabellen `orders` 
    -- 

    CREATE TABLE `orders` (
    `id` int(11) NOT NULL, 
    `invoice_id` int(11) NOT NULL, 
    `owner_site_id` int(11) NOT NULL, 
    `owner_site_url` text NOT NULL, 
    `owner_site_name` text NOT NULL, 
    `order_id` int(11) NOT NULL, 
    `order_created_at` datetime NOT NULL, 
    `order_updated_at` datetime NOT NULL, 
    `order_completed_at` datetime NOT NULL, 
    `status` varchar(255) NOT NULL, 
    `currency` varchar(255) NOT NULL, 
    `total` double NOT NULL, 
    `subtotal` double NOT NULL, 
    `total_tax` double NOT NULL, 
    `total_shipping` double NOT NULL, 
    `shipping_tax` double NOT NULL, 
    `cart_tax` double NOT NULL, 
    `total_discount` double NOT NULL, 
    `shipping_methods` text NOT NULL, 
    `payment_details` text NOT NULL, 
    `billing_address` text NOT NULL, 
    `shipping_address` text NOT NULL, 
    `total_line_items_quantity` int(11) NOT NULL, 
    `note` text NOT NULL, 
    `customer_ip` varchar(255) NOT NULL, 
    `customer_id` int(11) NOT NULL, 
    `view_order_url` text NOT NULL, 
    `line_items` mediumtext NOT NULL, 
    `shipping_lines` text NOT NULL, 
    `tax_lines` text NOT NULL, 
    `fee_lines` text NOT NULL, 
    `coupon_lines` text NOT NULL, 
    `export_csv` text NOT NULL, 
    `proforma_text` text NOT NULL, 
    `updated_at` datetime NOT NULL, 
    `created_at` datetime NOT NULL 
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

    -- 
    -- Begrænsninger for dumpede tabeller 
    -- 

    -- 
    -- Indeks for tabel `orders` 
    -- 
    ALTER TABLE `orders` 
    ADD PRIMARY KEY (`id`), 
    ADD UNIQUE KEY `unique_order_id` (`order_id`,`owner_site_id`); 

    -- 
    -- Brug ikke AUTO_INCREMENT for slettede tabeller 
    -- 

    -- 
    -- Tilføj AUTO_INCREMENT i tabel `orders` 
    -- 
    ALTER TABLE `orders` 
    MODIFY `id` int(11) NOT NULL AUTO_INCREMENT; 

(Vielleicht eine Art von "force-update", weil ich meine UNIQUE-Schlüssel verdächtige, um die Sache zu machen, die die Zeilen nicht zur Verfügung stellt, um zu aktualisieren)?

+2

Irgendwelche Fehler? Nichts in den Fehlerprotokollen? –

+1

Es gibt keine Fehlerprüfung, um gesehen zu werden. Ist 'PDO :: ERRMODE_EXCEPTION' aktiviert? – mario

+0

Nein - nichts. Ich habe versucht, das $ sth-> rowCount() auszuführen, das 0 zurückgab, so dass die Zeilen nicht betroffen sind. – RasmusBS

Antwort

0

Lösung:

Nach weiteren Debuggen fand ich, dass die $value['id'] NULL war.

Ich kann nicht erklären, warum, aber ich habe stattdessen $results[$order]['id'] verwendet, um die ID für die Zeile zu holen.