1

I am making a website for a cars show, i want to store images in the database (just the URL) and what i want to do is for all the images to be added to the same cell in the table.

then at retrieval time, i want to use the explode() command in php so i can seperate each URL and use a loop to display them.

the problem i am facing it i do not know what i should use for a delimiter, i cannot use anything that can be used in windows, mac or Linux which can be used in a file name, and i am afraid of using a system reserved key and cause a problem. i am also concerned about the data type that will hold this information, i am thinking TEXT is best here but i heard many saying it causes problem.

to be clear, the idea is:

when someone uploads 3 images, the images will be uploaded into a folder, then the names will be taken and put into 1 string (after the directories names are added) with a separator between them that then will be stored in the database. Then, i take that string, use explode() and store the separated data in an array and use a loop to display an image with the source being the stored data in the array.

i need a special delimiter or another way... can someone help me do this or tell me another way of saving the images somehow without a potential risk! i have seen many website which uses dynamic bullet points (lists) but i was never able to get a code or even an idea of how to do them.

EDIT:

The current way i am using is having 10 rows, 1 for each image.. but the problem here is that the user will not be able to add more than 10 images, and if he has less than 10 images then there will be few empty images being displayed. (i know a solution for the empty images, but it is impossible to add more images..)

18
  • 1
    Why would you want to store them in a one string, just store them in separate database rows. Commented Nov 23, 2014 at 18:50
  • you can use image in row base Commented Nov 23, 2014 at 18:51
  • at first you told be why are you store in one string...? Commented Nov 23, 2014 at 18:52
  • 1
    then you can use serialize object in store database Commented Nov 23, 2014 at 18:53
  • 1
    php.net/manual/en/function.serialize.php Commented Nov 23, 2014 at 18:56

4 Answers 4

2

You can to use any type of
serialization(serialize, json_encode), when put your array and
deserialization(unserialize, json_decode), when want to use it.

But! I advice you to create a new table for your images, with car_id field, for example.
Then you can just join it and get it all.

It can be CarImages ('id', 'car_id', 'image_file') Also I recommend to add foreign key constraint on CarImages.car_id->Cars.id, then your orphaned images will cascade removed, when cars will removed.

Storing of serialized values is always bad idea.

Sign up to request clarification or add additional context in comments.

5 Comments

so for each car a new table is created then rows can be added to them as needed.. and when the car is deleted the table get automatically dropped? but wouldn't creating thousands and more of tables create a mess and cause DBMS (MySQL) to slow down? the website might become big and the amount of cars will reach many thousands if not 100 thousands.
@bakriawad, you won't create a table per car, just one table, which holds the images and the corresponding key to the car. Your query will look like this: SELECT cars.car_id, images.image_id, images.name FROM cars INNER JOIN images ON (cars.car_id = images.car_id) then you can loop over all saved images per car
You need only two tables: Cars for cars information(one per row), and CarImages for images(one row per image, any rows per car)...
Millions rows of carimages will works good, databases designed for this... Deserialization will eat more server resources, than simple datarows accessing
that seems like a good idea.. not my idea of professionalism but it sounds good though. thanks!
1

If you can't store one row per image on a separate table for any technical debt reason, then you should json_encode the array on images paths and store the result in database.

3 Comments

or serialize, or each other correct serialization function
so the concept here is: take an array, mush it up into a another form and store it in database which you can just decode it again to make the array... am i right here?
Right, the important thing is to use a reliable serialisation method. As you pointed out, an url really can contain anything. By wrapping them up with json, you can retrieve the value with ease. Some databases like postgresql have a json type.
1

Solution one :

Create a table called Images contains 3 columns (id,image_path,user_id) and everytime the user uploads an image insert it into this table, and in your script if you want to display the uploads for a specified user get it by the user_id :

$user_id = 1 // The specified user id;
mysqli_query($connect, "SELECT * FROM images WHERE user_id = '$user_id'");
    // And Loop here trough images

Solution Two :

Inserting images paths into one column.

Table files contains 1 column called path

Inserting the values to the files table :

$array = array(
    '/path/to/file/1',
    '/path/to/file/2',
    '/path/to/file/3'
);

foreach($array as $path) {
    $path = $path . '|';
    mysqli_query($connect, "INSERT INTO files (path) VALUES ('$path');
}

Now display the results :

$query = mysqli_query($connect, "SELECT path FROM files");
$row = mysqli_fetch_assoc($query);
$paths = explode('|', $row['path']);
    foreach($paths as $path) {
    echo $path . '<br>';
}

1 Comment

for updating i could take them, explode, alter what i need to be altered then reassemble and update...
0

If you do not change your database then you should try.I think below link useful for you json json-encode serialize

you can use anyone.

If You design Your Tables like

 Table-user

  user_id
  username

another table for user images

Table-images

serial_num
user_id
image_url

then you can store many images for 1 user.here user_id of images table actually the foreign key of user table's user_id. You are using relational database so it's good for you otherwise you can use nosql database

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.