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');
Related
This question already has answers here:
Update a column in MySQL
(5 answers)
Closed 6 years ago.
Imagine I have some code like this:
$sql = "INSERT INTO tablename (column1, column2, column3) VALUES
('1','2','3')";
How would I convert this into an UPDATE query? I did some searching, but
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
doesn't make much sense to me. Thanks
INSERT and UPDATE queries look similar but they do different things.
Inserts => Inserts new nonexisting data into table
Update => Updates existing data inside of the table
That is why the UPDATE query has WHERE clause
So, you can use UPDATE to modify a row which is already inserted.
I don't know what exactly you are asking, because you are not clear in your question. But according to my thinking you want to know the difference.
Update query edit or update the data in the database. Like this
update table_name
set 'name'=something
where 'id'='some_id'
for more examples and practice go HERE
Insert insert query is just making a new data entry in the database in a table with a unique id, precisely.
HERE is the link for insert example.
This question already has an answer here:
MySQL and the chance of the wrong id being returned by LAST_INSERT_ID()
(1 answer)
Closed 8 years ago.
I've a database with hundreds of records which inserted in each seconds, I have the below insert query and I want to know the insert id of this query:
$currQuery = $pdo->prepare("INSERT INTO some_table (...... )");
$currQuery->execute("....");
$queryInsertId = $pdo->lastInsertId();
I just want to know that is it safe to use lastInsertId() while I having hundreds of new records in every seconds? any ideas?
Yes it is safe. It returns the last insert id for the connection.
This question already has answers here:
Insert multiple rows with PDO prepared statements
(4 answers)
Closed 8 years ago.
Ok so i need to insert multiple rows into a database using MySql from one form, each row is a result. The was i started doing it is the following:
INSERT INTO results (1,2,3,4,5,6,7) VALUES (1,2,3,4,5,6,7), (8,9,10,11,12,13,14)
And so on, my question is:
I don't know how many rows of results a user will want to insert ( a maximum of 15 will apply). Is their a more practical way of achieving the same result? Or am I better off sticking to the way i originally planned?
1°) Use prepared statement (like in Java):
(For safe code, always use prepared statement and avoid generate SQL)
$stmt = $db->stmt_init();
$stmt->prepare("INSERT INTO foo_table (id, firstname, lastname) VALUES(?, ?, ?)");
foreach($myarray as $row)
{
$stmt->bind_param('iss', $row['id'], $row['firstname'], $row['lastname']);
$stmt->execute();
}
$stmt->close();
To understant "iss" param in bind_param: http://uk3.php.net/manual/fr/mysqli-stmt.bind-param.php
2°) Generate batch insert:
Batch inserts with PHP
This question already has answers here:
How to avoid duplicates while updating a MySQL database?
(5 answers)
Closed 8 years ago.
I am storing number of check boxes with delete ,add,view options in database,
if you check on that options,it storing some values.But if i click again that will store again and again.
how to avoid this duplicate values
You can set the column containing unique value as UNIQUE or do a condition :
if SELECT Query return this value , don't insert , else insert.
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