PHP and MySQL to populate table from unknown ID - php

I have three tables: unitpost, file, and postfile with postfile being the table that links files to a particular post.
I have created a script that uploads the files and posts however I need to populate the postfile table with both the unitpostID and fileID, however once the file is uploaded, the ID is chosen at random. How can I then populate the postfile table with the fileID that was just uploaded?
Hope this made sense!!
Any help appreciated.

Use LAST_INSERT_ID() to obtain id of last insert operation.

Use AUTOINCREMENT for the primary keys, thus the IDs won't be random, and you will be able to use them either from MySQL or from PHP.
Afterwards use mysqli_insert_id() (or PDO::lastInsertId if you use PDO) from php to get the last insert id, or, as #Nelson stated, the MySQL function directly.

Related

how to increase performance of mysql database insertion

working on the PHP project related to web scraping and my aim is to store the data into the mysql database,i'm using unique key index on 3 indexes in 9 columns table and records are more than 5k.
should i check for unique data at program level like putting values in arrays and then comparing before inserting into database ?
is there any way so that i can speed up my database insertion ?
Never ever create a duplicate table this is a anti SQL pattern and it makes it more difficult to work with your data.
Maybe PDO and prepared statement will give you a little boost but dont expect wonders from it.
multible INSERT IGNORE may also give you a little boost but dont expect wonders from it.
You should generate a multiinsert query like so
INSERT INTO database.table (columns) VALUES (values),(values),(values)
Keep in mind to keep under the max packet size that mysql will have.
this way the index file have to be updated once.
You could create a duplicate of the table that you currently have except no indices on any field. Store the data in this table.
Then use events to move the data from the temp table into the main table. Once the data is moved to the main table then delete if from the temp table.
you can follow your updates with triger. You should do update table and you have to right trigger for this table.
use PDO, mysqli_* function, to increase insertion into database
You could use "INSERT IGNORE" in your query. That way the record will not be inserted if any unique constraints are violated.
Example:
INSERT IGNORE INTO table_name SET name = 'foo', value = 'bar', id = 12345;

MYSQL - Find the ID of the record just added

OK, so here is my scenario - you may disagree with what I'm attempting to do but I have my reasons.
The user is able to upload various information to the database, I also want them to be able to upload a picture at the same time. Now for various reasons I want to store the image with the file name the ID number of the record I'm adding (I have an ID column as the primary which auto-increments). Obviously I would have to store the file extension in the database too.
Now is there a way I can add the record and then know what ID was set so that I can save the image? I don't want to query and select the highest ID as that could go wrong if two people were to submit the form at the same time.
Any ideas?
LAST_INSERT_ID() will give you the ID that was auto-generated:
... returns a BIGINT (64-bit) value representing the first
automatically generated value that was set for an AUTO_INCREMENT
column by the most recently executed INSERT statement to affect such
a column. For example, after inserting a row that generates an
AUTO_INCREMENT value, you can get the value like this:
mysql> SELECT LAST_INSERT_ID();
-> 195
After running an insert in PHP, you can get the AUTO_INCREMENT ID from $mysqli->insert_id (where $mysqli is a MySQLi connection object), $pdo->lastInsertId (where $pdo is a PDO connection object), or mysql_insert_id() (if you're still using MySQL, which you shouldn't be) without having to run another query.

getting id of last query when multiple queries are made

In php have a form with a textarea and a file upload. In mysql I have table 1 with rows for each uploaded file having data like name, size, etc... and I have table 2 with the data entered in the textarea and a column with the id of the file uploaded.
I'm using mysql_insert_id to get the id of the last inserted row in table 1 (the id of file uploaded). Then I insert that id in table 2.
Assuming that multiple uploads are made at the same by different users will mysql_insert_id return the proper id each time? What happens if 2 users upload at the exact time.. Or there is a timeout.. I guess I'm asking if there's a better way to insert a newly created id to 2 tables at the same time?
It returns the id of inserted row for the session from which the insert has been made. So yes, you're doing it just fine.
mysql can't give twice the same value if of course you have the auto-increment on the primary key of the table
Per the php docs (mysqli::$insert_id) last_insert_id function returns the ID generated by a query on a table with a column having the AUTO_INCREMENT attribute. Thats for the specific resource you are using; other resources will not affect each other

Entry current id

I have an application which keeps database of all nearby restaurants.
When adding a new entry, there is an option to add a few images of your restaurant.
Now the problem:
I'd like to sort the different restaurant images into separated folders, named by restaurant id stored in mysql auto increment ID. The only problem here is, that i dont know that id in advance.
Form example:
text input - title
text input - address
text input - phone
file input - image
file input - image
So, what should I do now?
I. Get the last id, lock the table, create folder named by id, store images inside, store information to mysql database including image paths, unlock table.
or
II. Store all information excluding images paths to mysql database, use PHP mysql_insert_id, create folder named by id, store images inside and store images paths to mysql database.
or
III. Better solution?
This kind of thing is usually done with your option II. Store in the database the main row of information, get the last insert id via mysql_insert_id() or the native MySQL LAST_INSERT_ID() function, then proceed to store rows in any related tables if necessary and create the image directory to store filesystem data.
Assuming the image paths you intend to store in the database will go into a different table with a one-to-many relationship back to the main restaurant table, you'll need to know the last insert id to insert them anyway. Don't worry much about doing it in multiple operations -- that's exactly the reason most RDBMS have a function like LAST_INSERT_ID().
If you are using the autoincrement column. You can right after your insert statement call the last_insert_id() function to retrieve the id of the last inserted record.
See this link for documentation:
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id
Its important that you do this within the same transaction/connection otherwise the value might be erroneous.
I don't see a need to use the restaurant's id to organize the pictures - especially since you get into the timing issues you describe. I'd create another unique id - call it picture_folder_id or something - and use that to name the folder for the pictures. As long as you enforce uniqueness on that id, you won't get any collisions, and you won't have any timing problems or locks.

How to display an incremental value that was just created in mysql

I have a PHP script that creates an entry into a mysql database. The PHP inserts all of the data except the primary key, which mysql automatically increments. The problem is that i want to insert information into two tables, and these tables must be able to associate. Is there a way to have PHP create an entry in one table in mysql, then figure out the incremental primary key value from that first table, in order for it to insert into the second mysql table as a reference?
Yes. This is certainly doable. The function/method you use to get the auto-incremented value that was just inserted will depend on the way you access MySQL from PHP. If you're using the mysql_ functions, use mysql_insert_id(). If you're using the mysqli_ functions (or OO versions), use mysqli_insert_id(). If you're using PDO, use PDO::lastInsertId(). In all cases, the function delegates to the MySQL function last_insert_id() which is local to the connection so you need not worry about concurrent threads interfering with each other.
You can use:
mysql_insert_id()
This will return the id of the previous query. See below:
http://php.net/manual/en/function.mysql-insert-id.php
To get the unique ID inserted for the first table you can use LAST_INSERT_ID() and save that as reference for your second table:
SELECT LAST_INSERT_ID();
Use this as another option I suppose. For PHP you can use mysql_insert_id as suggested :)

Categories