我必须更新订单表中的某些行。
我的代码运行完全正常,但是当我检查我的phpmyadmin时,没有更新任何行。我还尝试从同一个表中更新不同的列,但这些列也没有用。
任何人都可以帮助我吗?
(我试图在另一个表中更新一行,但似乎工作正常。)
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>';
}
}
?>
表格结构:
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;
(也许某种&#34;强制更新&#34;,因为我怀疑我的UNIQUE键是否会使行无法更新)?
答案 0 :(得分:1)
解决方案:
进一步调试后,我发现$value['id']
为NULL。
我无法解释原因,但我使用$results[$order]['id']
来获取行的ID。