LAST_INSERT_ID not working on UPDATE - php

if I use this SQL:
UPDATE formulare SET EV_id=59, EV_status=5 WHERE EV_id=57 AND ID_uziv=12;SELECT LAST_INSERT_ID();
I will get 0 as last insert id.
I'm using php mysqli_insert_id and here is said that:
The mysqli_insert_id() function returns the ID generated by a query
on a table with a column having the AUTO_INCREMENT attribute.
If the last query wasn't an INSERT or UPDATE statement
or if the modified table does not have a column with the AUTO_INCREMENT attribute,
this function will return zero.
my table formualre has auto increment column, so I don't know wher the problem is

LAST_INSERT_ID() won't work if no new auto increment value was created.
The solution is something like this:
UPDATE formulare
SET EV_id=LAST_INSERT_ID(59),
EV_status=5
WHERE EV_id=57
AND ID_uziv=12;
SELECT LAST_INSERT_ID();
Note: I guess, that EV_id is the auto_increment primary key.
Otherwise you should do a query like:
UPDATE formulare
SET key_col = LAST_INSERT_ID(key_col),
EV_id=59,
EV_status=5
WHERE EV_id=57
AND ID_uziv=12;
SELECT LAST_INSERT_ID();

From the documentation :
The ID generated for an AUTO_INCREMENT column by the previous query on
success, 0 if the previous query does not generate an AUTO_INCREMENT
value, or FALSE if no MySQL connection was established.
As your update didn't create a new record, it didn't generate any AUTO_INCREMENT value.

UPDATE query updates the existing record, it doesn't return any new ID.
mysqli_insert_id retrieves the ID generated for an AUTO_INCREMENT column by the
previous query (usually INSERT).
There was no INSERT query, that's the reason, you won't get any Id after executing UPDATE query.
For more info, refer mysql_insert_id

Related

How safe is to use mysqli_insert_id() and if not what should I use?

I have a very simple question I want to know the id of the last query that I inserted in my MySql DB.
It's safe and efficient to use mysqli_insert_id() and if not what should I use?
Yes, it's the recommended way to retrieve an ID value generated by an insert when using auto-increment. But it has limitations, which are documented. Did you read the documentation?
The mysqli_insert_id() function returns the same value that would be returned by the builtin SQL function LAST_INSERT_ID().
Note that it only works in the same session after you insert and generate an ID. You can't get the last ID generated in a previous session.
So for example it doesn't work at all in phpMyAdmin, because the "shared nothing" nature of PHP is that every query executes in a different session.
It also only works if your primary key is an auto-incrementing key, and if your last insert used the auto-increment mechanism. Even if your table has an auto-increment, you can override that by specifying a value. Then the last insert id is meaningless. Demonstration:
mysql> create table t (id serial primary key);
mysql> insert into t set id = 42;
mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 0 |
+------------------+
Why does this return 0? Because my insert did not generate an ID, it just used the ID value I specified in the insert.
It only returns the latest ID generated. If you do another subsequent insert that generates an ID, it'll return the more recent generated ID.
It doesn't tell you which table the ID was generated by. It's up to you to know which table you most recently inserted into, which used auto-increment to generate an ID.
So in conclusion, yes, it's the right function to use, but you do need to understand how it works and you need to know how to use it.
According to the documentation:
The mysqli_insert_id() function returns the ID generated by a query (usually INSERT) on a
table with a column having the AUTO_INCREMENT attribute. If no INSERT
or UPDATE statements were sent via this connection, or if the modified
table does not have a column with the AUTO_INCREMENT attribute, this
function will return zero.
That said, if the table you are persisting using mysqli (INSERT or UPDATE) has a primary key column with the AUTO_INCREMENT attribute it will be safe to use mysqli_insert_id().

Using php mysql set auto_increment primary value into other field

how can i set id primarykey auto_increment same value that automatically generating into my id field will also generate in other field in same table.
How could it possible?
Use mysql_insert_id which returns last auto increment ID...
1) insert into first table, use mysql_insert_id() to get auto increment ID
2) after getting auto increment ID from 1st table, insert on second table with the ID you got.
for more refer http://php.net/manual/en/mysqli.insert-id.php

MySQL get Primary Key after Insert Statment

I need to use the following insert statement, and the primary key field is auto increment. I need to return the new primary key value after the insert. Preferably using one statement to do the insert and return primary key value.
$query = $database->connection->prepare("INSERT INTO accounts (created, priviledge, password, token, idle) VALUE ('$created', '1', :password, '$token_encrypt', '$idle')");
$query->bindParam(":password", $password_hash);
$query->execute();
As soon as you're using PDO you need to use
$database->connection->lastInsertId()
And it guarantees you to return the value from the current session. So answering to your question from comments - there is no chance you get the wrong value from a concurrent session.
Use mysql_insert_id(); to get the ID generated in the last query.
It retrieves the ID generated for an AUTO_INCREMENT column by the previous query (usually INSERT).
assume your primary field name is id then try this code
SELECT max(id) FROM tableName
or try this
LAST_INSERT_ID();
DELIMITER $$
DROP TRIGGER IF EXISTS `TR_AI_mytable`;
CREATE TRIGGER `TR_AI_mytable` AFTER INSERT
ON `mytable` FOR EACH ROW
BEGIN
SET #mylastPK = new.PKField;
END;
$$

How to get MySQL value from certain row?

I have a table with 5 rows. Every time a user enters data into a form, it is entered into the table. My first column is called id and holds the number of the post. What I want to do is get the value of id from the previous row, add one to it and set it as the value in the current post's id field. How do I do this?
Just set that field as primary key and auto-increment, it will automatically do this for you. You won't have to fetch the previous row and add that field value to next one.
The SQL query you need is:
SELECT max(id)
FROM tableName;
Set attribute auto increment for "ID" field in the table that contains 5 columns.
You can use sql query like
"INSERT INTO my_table (id auto_increment,primary key(id))";
then you can get...
and eachtime you need not worry to insert id ,it will automatically increments
I would not recomend doing this as it could lead to a race condition.
Change the table structure and set the id field to be the primary key and set it to auto increment. This way anytime a new row is added, it will auto-magically be assigned the next ID.
see this answer on details of how to set auto increment.
here is the query to alter your table and it will set your field or column as primary key and also auto increment it.
ALTER TABLE tbl ADD id INT PRIMARY KEY AUTO_INCREMENT;

How to insert new auto increment ID

I found the mysql_insert_id function to retrieve the last auto generated ID.
Should I be using mysql_insert_id +1 to add a new ID or is there a call for adding a new unique ID?
Using NULL for id:
INSERT INTO `database`.`table` (`id`, `user`, `result`) VALUES (NULL, 'Alice', 'green')");
OR not specifying id at all:
INSERT INTO `database`.`table` (`user`, `result`) VALUES ('Alice', 'green')");
Either way works just fine, more of a preference but personally I chose the second as its less typing.
If your id field is set to auto increment, you don't have to add an ID at all. It will be incremented and added automatically.
AUTO_INCREMENT in MySQL does exactly what it sounds like. When you insert a new record it will automatically generate a new ID for you. You do not need a separate call.
Insert a new record and set the auto-increment column to NULL, or just omit it entirely (which is implicitly setting it to NULL - it has the same result). The column will be set to the next auto-increment value instead of NULL.
When you delete a row and you insert again an another row, the new inserted id is not the same as what you delete before you insert again. example you have 3 row and the id value is 1, 2, 3, when you delete 3 then insert again, the id result is 4. And when you try to delete 2, the id result when you try insert again is 5.

Categories