This question already has answers here:
PHP/MySQL insert row then get 'id'
(10 answers)
Closed 9 years ago.
I'm running a MySQL INSERT query where I insert some data into the database.
Each row has a unique ID generated each time a new entry is added. I'd like to get this ID straight after I insert the data.
PHP Code:
$addGiveawayToDb = mysql_query("INSERT INTO $table(orientation, title, color)
VALUES ('$orientation', '$title', '$color')")
or die(mysql_error());
//Here I need to get the row id of the above data...
Thanks!
Try like
$addGiveawayToDb = mysql_query("INSERT INTO $table(orientation, title, color)
VALUES ('$orientation', '$title', '$color')")
or die(mysql_error());
echo "Inserted Id is ".mysql_insert_id();
Makesure that you have an auto increment field in your table.And also try to avoid mysql_* functions due to they are deprecated.Instead use mysqli_* functions or PDO statements.
See this LINK
Related
This question already has answers here:
How do I get the last inserted ID of a MySQL table in PHP?
(16 answers)
How to include a PHP variable inside a MySQL statement
(5 answers)
Closed 3 years ago.
I have a MYSQL database query nested in a foreach statement like this:
foreach ($_POST['articles'] as $article => $number) {
$sql_update2 = "INSERT INTO testmerrill.Device_Article_Connection (device_id, article_id)
VALUES ((MAX (device_id) FROM testmerrill.Devices),'{$_POST['articles'][$article]}))'";
query_db1($sql_update2);
}
The above query runs after this one:
$sql_update1 = "INSERT INTO testmerrill.Devices (device_name, device_manufacturer)
VALUES ('{$device_name}', '{$device_manufacturer}')";
query_db1($sql_update1);
This page is the create page of a CRUD program I am writing. I have two tables I am working with here: Devices and Device_Article_Connection. What I am trying to do is insert into my Device_Article_Connection table multiple select statement options the user posted on submit, calling a query for as many options the user chose to include as associated with a new 'device'. I know my current solution is probably not a very elegent way to do it, but thats where I'm at at the moment. The difficulty is that I do not know the id of the new device the user just created, which I need in order to associate it with the articles the user chose to associate with it. I am trying to find the device id using the MAX function (because the last id the user just added should be the largest), but I can't seem to get that to work, there is some syntax error that I have not been able to pinpoint.
I am thankful for any suggestions.
If you use mysqli for a database connection, you could use insert_id to solve this problem:
$conn = new mysqli($server, $user, $pass, $db);
$q = $conn->prepare("INSERT INTO testmerrill.Devices (device_name, device_manufacturer) VALUES (?, ?)"); // Prepare the query
$q->bind_param("ss", $device_name, $device_manufacturer); // Bind variables to prevent SQL injection
$q->execute();
$last_id = $q->insert_id; //Represents the last inserted id
This question already has answers here:
PHP/MySQL insert row then get 'id'
(10 answers)
Closed 9 years ago.
I am inserting record into database using the insert query as
INSERT INTO task_record(assigned_by,handle_by) VALUES('$assigned_by', '$handle_by')
adding this record assign a unique ID to the each record i want to get that ID as soon as the record is inserted.
Is there any simple way to modify the query or i have to use one more query of select which is not a fine option
If your interfacing with mysql directly execute SELECT LAST_INSERT_ID() after the insert query.
with the now obsolote mysql extension you could execute mysql_insert_id();
with the mysql improved extension use mysqli_insert_id();
Try with $this->db->insert_id() like
echo "The Last Insert Id is ".$this->db->insert_id();
Or like
echo "The Last Insert Id is ".$this->insert_id();
Or even simply
echo "The Last Insert Id is ".mysql_insert_id();
But try to avoid using mysql_* functions due to they are deprecated.Instead use either mysqli_* functions or PDO statements.
EDIT:
$query = "SELECT LAST_INSERT_ID() task_id";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$id = $row['task_id'];
}
echo $id;
This question already has answers here:
Retrieving the last inserted ids for multiple rows
(2 answers)
Closed 9 years ago.
MySQL code is something like:
INSERT INTO table(name, value) VALUES ('name1', 'value1'), ('name2', 'value2'), ('name3', 'value3')
Basically, multiple inserts in the same SQL statement.
How can I get the IDs of the inserted values. I assume mysql_insert_id() combined with the number of inserts isn't safe since someone else might insert something at the same time.
Is there another way?
You either need to insert them one at a time or figure out the id's with a followup query.
INSERT INTO table(primkey, value) VALUES ('pk1', 'val1'), ('pk2', 'val2');
SELECT FROM table id, primkey where primkey in ('pk1', 'pk2');
This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
mySQL return index after insert
Hy,
Is there a way that after inserting something in mysql database (INSERT INTO table_name VALUES () ...) to retrieve ID of that insert for further manipulation ?
The old way is to make the insert and then query a SELECT on the table name . But is there any way to do this in a single query ?
Thank you very much
In PHP, call mysql_insert_id(). Or directly in MySQL, call the LAST_INSERT_ID() function.
// PHP
echo mysql_insert_id();
// PHP MySQLi
echo $mysqli->insert_id;
-- in MySQL
SELECT LAST_INSERT_ID();
you have a function called
$lastId = mysql_insert_id();
for more info: http://php.net/manual/en/function.mysql-insert-id.php
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is there a way to return the id of a row that was just created in MySQL with PHP?
hi, i have an inset command such as
insert (name) values($name);
where i have id column as outoincrement how can i get the id of the inserted record after inserting directly
If you are working with mysql_* function, you'll have to use mysql_insert_id() (quoting) :
Retrieves the ID generated for an
AUTO_INCREMENT column by the
previous query (usually INSERT).
With mysqli, you'll use mysqli::insert_id().
And, with PDO, you'll call PDO::lastInsertId().
mysql_query("Your query here");
$last_id = mysql_insert_id();