Im trying to write a function that updates a mysql table if it excist, or creates it if it does not.
My script still creates a new entry instead of updating.
Here is my sql structure:
CREATE TABLE IF NOT EXISTS `loan` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`bank_id` int(11) DEFAULT NULL,
`name` longtext COLLATE utf8_unicode_ci,
`rate` decimal(8,3) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `loan_0b0af02d` (`bank_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
Here is my php script:
$item_insert_sql = "INSERT INTO loan(id, bank_id, name, rate) VALUES ('" . $id . "', '" . $bank_id . "', '" . $name . "', '" . $rate . "'), primary key (id), ON DUPLICATE KEY UPDATE (name='$name', rate='$rate')";
$insert_item = mysql_query($item_insert_sql, $db);
What do I need to do, to fix this?
Your insert statement is incorrect. You can’t define primary keys in an insert, and your
on duplicatesyntax is bad as well:If you had even minimal error handling on the query() call, you’d have gotten the error messages: