What's the best way, or, how would you do to have a field in PHP+SQL formatted as AB00000 where the first inserted should be AB00001 and so on. I have a web page in PHP+SQL to create a form and insert it into a table wich one of the columns is the "reference_nr" and my whole code is already made using the ID (AutoIncrement) so I can't use that for that table.
What I would need is something that would always write the last used maybe to another different table and before INSERT into form's table I would SELECT the last value and increment + 1 and INSERT with the result of that math operation.
Do I need another table or can I select directly from the form's table?
Do you think it give me problems if two persons try to INSERT the
form at the same time?
SQL brings that already with it:
Use the SQL-Function LAST_INSERT_ID() similar to the example in the source page I linked you below. Let's assume table foo has an auto-increment field "id":
INSERT INTO
foo (auto, text, somevalue)
VALUES
(NULL,'text', 2); # id was generated hoer
Then you do:
UPDATE
foo
SET
somevalue = somevalue + 1
WHERE
id = LAST_INSERT_ID()
Source: http://dev.mysql.com/doc/refman/5.7/en/getting-unique-id.html
If your problem is how to get an autoincrement value for a string column with prefix like AB00000, autoincrement fields are only numeric.
If the field have a fixed format you can create a plain insert using the id value from:
SELECT CONCAT('AB', LPAD( MAX(SUBSTRING(id,3)+1 ,5,'0') from table
And use this value directly in your insert to avoid transaction problems
Related
I want to have the image I'm uploading have the ID of the row it belongs to as the name (e.g. 42.jpg) to ensure uniqueness.
Is it possible to get this in the middle of the query? Or do I have to get the ID after then update the row?
You could first insert an empty row (with a null image and filename.) You can retrieve the last inserted ID with LAST_INSERT_ID().
After that, you can update the row with the image and the file name based on the ID.
If id is AUTO_INCREMENT column in the table you're inserting... no, the automatically assigned value is not available during statement execution. And the value isn't available when a BEFORE INSERT trigger is fired.
The value assigned to the row is only available after the INSERT statement completes.
Immediately following a successful insert, SELECT LAST_INSERT_ID() (equivalent library function) can retrieve the value assigned.
As another option, you could consider emulating an Oracle sequence object, using a separate table with an AUTO_INCREMENT column, and LAST_INSERT_ID. If you get that as a unique id before you do the INSERT, you could supply the value in the INSERT.
I have a tree and I want to I have a table which has an id also, I want to use this id by adding to another column.
clearly,
I want to add id column'value to parent_root column.
sql = "INSERT INTO agac_menu (id,isim,aciklama,parent_id,parent_root,yetki,sira,tip,created)
VALUES (NULL,'".$isim."','".$aciklama."','".$parent_id."','".$parent_root."','".$yetki."','".$sira."','".$tip."','".date('Y-m-d H:i:s')."')";
If it was MySQL (yes, I know it was more generic) you could use a trigger: MySQL Triggers last_insert_id()
You can't do this in the same query--the auto incremented id doesn't exist until the record is inserted. After your query runs in php, grab the last insert id and run an update query.
Firstly please excuse my lack of knowledge for SQL, I have only done basic inserts before.
I am currently improoving a little system that I have, and I want to insert some data that is obtained via _GET (php) into two tables. My problem is as follows.
My first table (table_one) has an auto incrementing value called "id", which I need to obtain, and post over to my second table (table_two).
Because of the way data will be updated at the later date, the ID in table two, is a reference to the ID that is automatically generated upon insert in table one (hence no ID in the code below). (I will be using the ID in table one to do a for loop for each matching ID instance in table_two)
How can I run one query to update one table, then update the 2nd with the unique id obtained from the first table?
My current code is this...
INSERT INTO table_one (valueone,valuetwo,valuethee) VALUES ('$v1','$v2','$v3')
you can use mysql_insert_id() built in command of php this will give you the id of the recently inserted data
mysql_query("insert into.... ");
$a = mysql_insert_id();
mysql_insert_id() after first query will give you id
I want to insert some data that is obtained via _GET
that's wrong. this data should be obtained via POST
Expanding on #Ujjwal's answer, you can do the same just using SQL.
INSERT INTO table1 (x,y) VALUES ('x','y');
INSERT INTO table2 (t1_id, z) VALUES (LAST_INSERT_ID(), 'z');
See: http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html
I was wondering how can I select a value from a database that a user just entered it into and then add it to another mysql table all in the same script before the script is finished running.
You're probably looking for an insert ... select statement.
If you're talking about adding a value that a user just entered into a form, to something, and then putting that into the database, you should do the addition while in PHP. There's no point in going to the database after you've just inserted the value for this purpose.
If I'm misunderstanding something, please elaborate your question and let us know WHY you would want to figure out a just-inserted database value and do an operation on it, rather than trying to do it before you insert in the first place.
Also, if it's a fairly simple modification consider using an UPDATE statement, not a select --> insert.
Like nash said, you perform a select.
But to get the data from the row that the user just entered, you'll need:
mysql_insert_id()
Which grabs the last ID inserted (this is assuming you have an increment id column)
So assuming just entered his first and last name in a form, you'd insert his first and last name in the database(which i assume you know how since the title of this question is "SELECT a value from MySQL database"), you can get what he just entered by:
$last_id = mysql_insert_id();
If there are no rows on that table yet, then this will return 1. $last_id is now 1 (one).
To select:
SELECT * FROM users WHERE userID = "$last_id"
this will grab what the user just inserted....however, this seems pointless as you can use the variables from the form he just filled
enter code here
In the PHP MySQL module, you normally perform a mysql_select_db() to switch database.
You can insert your data into tables in different databases by switching between them with that function.
However, you can insert data into any table of any database (which the user has access to) by prefixing the database name to the table like so:
INSERT INTO test_db.test_table (`column1`,`column2`) VALUES ('abc',123);
You can use that also to insert data from one table into another using:
INSERT INTO `db1`.`myTable` (`column1`,`column2`) SELECT `column1`,`column2` FROM `db2`.`myTable` WHERE `id`= 5
The WHERE id part should obviously match the id of a row in db2.myTable
If you use doctrine you have the inserted data in the object representing the table and in addition you have primary key assigned for the record inside the object.
Con is doctrine is huge database abstraction layer, so if your application is not big doctrine is hammer for mosquito.
what is the structure of your database? The names of your tables, columns?
Some tutorial that you may want to look at: (grabbed from google)
http://www.phpf1.com/tutorial/php-mysql-tutorial.html
In theory you perform a select, take the data you need and perform an insert.
I'm trying to keep the database tables for a project I'm working on nice and normalized, but I've run into a problem. I'm trying to figure out how I can insert a row into a table and then find out what the value of the auto_incremented id column was set to so that I can insert additional data into another table. I know there are functions such as mysql_insert_id which "get the ID generated from the previous INSERT operation". However, if I'm not mistaken mysql_insert_id just returns the ID of the very last operation. So, if the site has enough traffic this wouldn't necessarily return the ID of the query you want since another query could have been run between when you inserted the row and look for the ID. Is this understanding of mysql_insert_id correct? Any suggestions on how to do this are greatly appreciated. Thanks.
LAST_INSERT_ID() has session scope.
It will return the identity value inserted in the current session.
If you don't insert any rows between INSERT and LAST_INSERT_ID, then it will work all right.
Note though that for multiple value inserts, it will return the identity of the first row inserted, not the last one:
INSERT
INTO mytable (identity_column)
VALUES (NULL)
SELECT LAST_INSERT_ID()
--
1
INSERT
INTO mytable (identity_column)
VALUES (NULL), (NULL)
/* This inserts rows 2 and 3 */
SELECT LAST_INSERT_ID()
--
2
/* But this returns 2, not 3 */
You could:
A. Assume that won't be a problem and use mysql_insert_id
or
B. Include a timestamp in the row and retrieve the last inserted ID before inserting into another table.
The general solution to this is to do one of two things:
Create a procedural query that does the insert and then retrieves the last inserted id (using, ie. LAST_INSERT_ID()) and returns it as output from the query.
Do the insert, do another insert where the id value is something like (select myid from table where somecolumnval='val')
2b. Or make the select explicit and standalone, and then do the other inserts using that value.
The disadvantage to the first is that you have to write a proc for each of these cases. The disadvantage to the second is that some db engines don't accept that, and it clutters your code, and can be slow if you have to do a where on multiple columns.
This assumes that there may be inserts between your calls that you have no control over. If you have explicit control, one of the other solutions above is probably better.