How to Save multiple images in a single column in mysql - php

I can save an image with a text in a database mysql cepandant, I would like to record select several images at once by saving all these images in a colone of my table by separating them by visas. How can I do this please?
Any help will be appreciated!
It's my HTML code:
<form action="saveimage.php" method="POST" enctype="multipart/form-data">
<input type="text" name="name" placeholder="name image" required>
<input name="image[]" multiple required type="file"/>
<button type="submit" value="Upload">Valider</button>
</form>
It's my PHP code:
include("conn.php");
$name = $_POST['name'];
$image = addslashes(file_get_contents($_FILES['image']
['tmp_name']));
$query = "INSERT INTO images(name,image) values('$name','$image')";
$result = mysqli_query($conn,$query);
if ($result) {
echo 'success';
}else{
echo 'error';
}
What I want is that when the user clicks on 'send' the image colone fills with multiple images separated by commas.

As far as I remember MySQL does not implement the ARRAY data type for columns.
The options I see are:
Create a 1:N relationship between the main table, and an "images" table. Each row of the images table will hold one image using a BLOB data type.
Use a single BLOB column in your table and somehow serialize all images into a single byte stream. This is, however, cumbersome.
I would go for option #1, since it's simpler to implement.

Usually you save images as blob in th database. It is not a varchar datatype, so that this in my opinion very difficult to realize that because you have to exclude that a comma byte is not in the image. You should change your design that it stores multiple images in their own row with a key that identifies it as belonging together. Of course Ou have to change also every access to that rable

Related

how to insert default image into database with PHP

UPDATED:
I have a form which have image upload field which have not required field. Now i want to if anyone submitting form without uploading image then a default image will be inserted into database. How can i do that with PHP?
<form method="POST" action="{{ route($base_route.'.eventEdit', ['event_id' => $event->event_code]) }}" accept-charset="UTF-8" id="general_form" novalidate="novalidate">
<input name="name" type="text" value="something" class="name">
<input name="name" type="file" value="DefaultImageName" class="image">
<button class="btn btn-default"> Submit </button>
</form>
you could put a defult value in the image column in your database, this way all users will get this image, unless you specify a different image, because it will override it...
this method worked for me...
With jQuery you are not able to do this, you can just send data to the server and process it on serverside. Eg. Save to database.
jQuery is a Javascript library, running in your web-browser. It does not have access to your database and should not have access to your database.
Your application server has access to your database and there you should default the image.
If your image is a field of a table where you insert anyway and you want to specify a default image when a record is inserted, then you need to alter your table and specify a default value for your images. In this case your problem can be solved solely by doing such an alter.
If your case is not as simple as above, then on your server where you insert the images you may check whether the image was posted and if not, then insert a default image.
Also, you duplicate the name of your tags, you might want to specify different values for the name attribute for your two inputs.
$imageName = "default.jpg";
if (isset($_POST["name_of_the_image_file"])) {
//move functionality of the image file
$imageName = pathinfo($_FILES['picture']['name'], PATHINFO_FILENAME);
}
$sql = "UPDATE table_name SET name='Doe' and image_field='$imageName' WHERE id='$id'";
$conn->query($sql);

Uploading an image file, resizing it then inserting it into a table

I've looked at this PHP file upload/resize method, but I have two questions.
When the image file is uploaded to the directory, the file is the name of the image uploaded from the person's device. I'd prefer it to be some kind of ID / number so that, for example, if person A uploads an image from their device called image1.jpg and so too does another person, it won't overwrite the original.
I know that imgur uses a 7-character string with numbers 0-9 and letters a-Z, so maybe it could generate something similar but also check if it already exists, and if it does, generate another. I'm not sure how to achieve this kind of thing.
I also want to insert the image that's been uploaded into a table.
How could I achieve these two things using the method I linked?
<?php
if(isset($_POST['submit']))
{
$newname = time().'_'.$_FILES['image']['name'];/*Rename image - New unique name of image*/
move_uploaded_file($_FILES['image']['tmp_name'],'upload/'.$newname);/*upload image*/
/*Now use this $newname and use it for insert into database*/
}
?>
<form method="post" enctype="multipart/form-data">
<input type="file" name="image">
<input type="submit" name="submit" value="submit">
</form>

How to get all values in a colum listed in MySql - php

I am a newbie in php and mysql - but almost managed to display data from DB in a html page
I have a table with rows which I managed to read and showed on a page.
All the rows in a burst, - no filtering, nothing!
Now I need to create a drop down list of a column values so that one of them can be selected to show a report it in a table list format in html.
The data table consists of entries from different handheld machines. One column in each row contains the IDs of the machine sending the row/record.
First I need to get all the values in a particular column - say 'MachineID'. But I need to skip repeating entries . One machine will be sending many entries/records.rows, but we need to skip those repetitions while preparing this machine list.
once the drop down list is ready, I can select one and call another php page which shows only data from that machine only.
How to create this list first?
If I were you I would like to create the same using a dynamic generated form. My code is following :
<body><div id="foo">
<form method="POST" action="">
<?php
$sql="SELECT * FROM table_name";
$result = mysqli_query($connection, $sql) or die ("error selecting from table".mysqli_error($connection));
echo "<select name='dropdown' value='' class ='f' ><option>-------------select platform-------------</option>";
while($row=mysqli_fetch_array($result))
{
echo "<option value = ".$row['id'].">".$row['your_column_name']."</option>";
}
?>
</select><br><br>
<input type= "submit" id= "submit_button" value= "submit form">
</form></div></body>
check your connection variable. and other variables.
hope it helps :) if it does than don't forget to green it :P

Getting the image file into the file field while updating record

if(isset($_GET['edit']))
{
$Id=$_GET['edit'];
//Get Form Data from Data base against the Id
$Edit_Query="SELECT * FROM products WHERE id='$Id'";
$Result=mysql_query($Edit_Query);
while($Row=mysql_fetch_array($Result))
{
$Id=$Row['id'];
$Product_Name=$Row['product_name'];
$Product_Price=$Row['price'];
$Product_Details=$Row['details'];
$Category=$Row['category'];
$Subcategory=$Row['subcategory'];
$File_Name=$Row['product_picture'];
}
Now I echoed all variable into my input fields...
Please note that ['product_picture'] is the file name only ... physical file is uploaded in to my uploads folder...
Now i want to get that physical file automatically into my file field...so if user just change other fields and hit update button ... it will update the rest of the record and file should be remained as it is in the folder as well as db with that particular $ID.
I tried <input type="file" name="product_picture" value="<php echo $File_Name; ?>">
but it is not even echoing the file name....
In simple words its like User want to update the record but its not necessary that he must have to update the file too.... it should be on choice ..
Try this,
Use a hidden field to store the image ID in,
<input type="hidden" name="image_id" value="image_id">
Then you can use it to assign the new image the previous ID and not have to update the SQL database.

php upload file multiple

I have a multiple upload textbox that I generate based on questions in my database. Right now my problem is that at the input, I have written <input type="file" name="file[<?php echo $id;?>]"/>, and when i want to save it, I write the program as $_FILES['file']['name'][$start]; where $start is a number I declared to match the question number. For example, if the file[123] is in the textbox, the post field when saving the information is also $_FILES['file']['name'][123].
But when I try to find if the textbox file[123] sent something like $_FILES['file']['name'][$start]<>"", it posts nothing.
This is the code when saving the information:
<input multiple name="file[<?php echo id;?>]" type="file" size="50" maxlength="100">
if ($_FILES['file']['name'][$start]<>""){
$fname=$_FILES['file']['name'][$start];
$tandatangan=$fname;
mysql_query("UPDATE answer SET document='$tandatangan' WHERE user_id='$id_user' and id_question='$id_soalan' ");
move_uploaded_file($_FILES["file"]["tmp_name"],"../files/$tandatangan");
}

Categories