I have an array called "data":
Array
(
[0] => Array
(
[ID] => 75538
[stock] => In Stock
[Price] => 82.6900
)
[1] => Array
(
[id] => 75536
[stock] => In Stock
[Price] => 72.0000
)
............(cont...)
I am running an update query as follows:
$conn->beginTransaction();
$sql = "UPDATE products SET stock_status = ?,price = ? WHERE id= ?";
try {
$stmt = $conn->prepare($sql);
foreach ($data as $v){
$stmt->execute(array_values($v));
}//end foreach
$conn->commit();
} catch (PDOException $e) {
echo 'Update failed: ' . $e->getMessage();
exit;
}
echo "Database was updated successfully";
$stmt->closeCursor();
$conn = NULL;
I am always getting "Database was updated successfully" message and data is not getting updated. In a empty table too i get the same result. There are no errors logged.
mysql column stock_status is varchar and price is decimal(10,4), id is varchar too... Please ignore the errorinfo part as i need to change it.
Is there anything wrong with my pdo update query ?? Help sought..
Update:
var_dump($data) gives price as string:
array(36828) {
[0]=>
array(3) {
["ID"]=>
string(5) "75538"
["instock"]=>
string(8) "In Stock"
["Price"]=>
string(7) "82.6900"
}
sample DB structure:
CREATE TABLE IF NOT EXISTS `products` (
`product_id` int(5) NOT NULL AUTO_INCREMENT,
`id` varchar(10) COLLATE latin1_general_cs NOT NULL,
`price` decimal(10,4) NOT NULL,
`stock_status` varchar(10) COLLATE latin1_general_cs NOT NULL DEFAULT 'In Stock',
PRIMARY KEY (`product_id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_cs AUTO_INCREMENT=1 ;
Update:
I have tried this too, but still no updates and no errors. Still getting success message.
$stmt = $conn->prepare('UPDATE products SET `stock_status`=:stockstatus,`price`=:price WHERE `id`=:id');
foreach ($data as $v){
$stmt->bindParam(':stockstatus', $v['instock'], PDO::PARAM_STR);
$stmt->bindParam(':price', $v['Price'], PDO::PARAM_INT);
$stmt->bindParam(':id', $v['id'], PDO::PARAM_STR);
$stmt->execute();
Update:
I already have exceptions set in my connection connstring file as:
$conn = new PDO($connStr, $user, $pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
And still getting no error message ????
Update: Issue resolved.
Sorry. My oversight. It was $v['ID'] and not $v['id'] in my code. The important thing was that it was not producing an error.