A
A
Alexey Alyushenko2016-02-01 23:13:30
PHP
Alexey Alyushenko, 2016-02-01 23:13:30

Can you help me with PDO, LastInsertId and INSERT ON DUPLICATE KEY UPDATE?

Hello,
I am making a request:

$param = 1;
$sql = "INSERT INTO orders(param,unic_cell) VALUES (?,1) ON DUPLICATE KEY UPDATE param=?";
$query = $pdo->prepare($sql);
$query->execute(array($param,$param));
  echo $pdo->lastInsertId();

In general, the essence of the question:
1) When a line is inserted, the number of the inserted line is displayed, everything is OK.
When the line is updated:
2) $param = 1 again, then 0 is displayed, here too ok
3) $param != 1, then the line number is displayed, why is this happening?
Those. if the updated string is not equal to the new one, then it seems to be deleting and inserting a new one, why is this happening?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2016-02-01
@Rsa97

If `param` is not a unique field, then ON DUPLICATE KEY will never work and a new row will be inserted each time, and if unique, then UPDATE param=? will rewrite the string, giving it a new id.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question