how to insert default image into database with PHP - 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);

Related

How to Save multiple images in a single column in mysql

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

if isset not working with auto image upload

I have a form that uses JQuery to auto submit the form immediately after the file has been selected.
The image is uploaded and displays correctly but no data is inserted into the table.
I have tried removing the 'if isset' to test the SQL inserts data correctly and it does but obviously the if statement is needed so it doesn't insert data to the table every time the page is loaded.
I assume the problem is because I have removed the forms submit button however if I add it back in the form no longer auto submits after image has been selected.
$('#file').change(function() {
$('#target').submit();
});
if(isset($_FILES['file'])){
DB::query('INSERT INTO images VALUES(\'\', :image, :img_id)',array(':image'=>$image, ':img_id'=>$userid));
}
<form id="target" action="upload.php" name="target" method="POST" enctype="multipart/form-data">
<img src="uploads/profileu<?=$userid?>u.jpg?=<?php echo rand() . "\n";?>">
<label class="btn btn-primary glyphicon glyphicon-pencil">
<input type="file" id="file" name="file" onchange="form.submit()" style="display:none">
</label>
</form>
I should also note the images are uploaded to and displayed from a local folder (working correctly) and the the SQL is to simply insert a 1 or 0 into database (user has image or does not have image)
The underlying issue was that I had the form action="upload.php" which is where the PHP for uploading images was all stored. Which meant I had to move my if(isset) statement to the top of that page instead of the page where the form was. Now it works perfectly without a submit button.
Because you're inserting $image and $userid you should check if those variables are set.
Like so:
if (isset($image) && isset($userid)) {
// insertion code
}
You have to use $_FILES['fileName'] not $_FILE
To Check if a file has been successfully uploaded use.
if($_FILES["fileName"]["error"] == 0){ ... }
Along with this you can check if the uploaded file is an image you can check it as:
if(getimagesize($_FILES["fileName"]["tmp_name"])){
//Its an Image
}
else
{
//Invalid Image
}

Retriving url from database and declare to input type image

<div class="form-group"> <input type="file" name="Upload" id="Upload"> </div>
I do have a submit button. While updating an image, I want to retrieve URL link "IMG/4.jpg" from database to default value of input. <?PHP echo $opened['IMG'];?>is my PHP code. I want to show something by default while I am updating the post!.
We have <input type="file"> so it will show a button for image upload and the name of image on the right.
For default value there is no any image value. When i press the button i can choose image from file, and it will show name of chosen image. So here is question: how can i give image name from PHP since it has value in database like "IMG/4.jpg"?

"Undefined index" only for "file" field in HTML form

This was working but stopped afterwards.
Other fields have no issue submitting.
When I press submit the other fields are added to the database but this one is not.
To prevent confusion the value that matters is photo_url not photo_dir.
Further clarification: photo_dir is the value of a drop down menu with a compass direction. All I want is to store the file name in the DB under column photo_url.
This is the form field
<label for="photo_url">Upload:</label>
<input type="file" name="photo_url">
This is the SQL
$sql="INSERT INTO photo(photo_project_id,photo_section,photo_subsection,photo_date,photo_post,photo_desc,photo_url,photo_dir)VALUES('$_POST[photo_project_id]','$_POST[photo_section]','$_POST[photo_subsection]','$_POST[photo_date]',now(),'$_POST[photo_desc]','$_POST[photo_url]','$_POST[photo_dir]')";
The file is also being uploaded to the server which is working without issue. Though I would like to be able to rename them to datetime() but that is a topic for a different day.
You can access to file element with $_FILE like this
$_FILES['photo_dir']
in the first step print_r this because it has another child
First of all your should have this attribute enctype="multipart/form-data" for using input type file, Should look like this :
<form action="" enctype="multipart/form-data">
After form submit, check :
if(isset($_FILES['photo_url'])){
$file_name = $_FILES['photo_url']['name'];
}
Rewrite query then :
$sql="INSERT INTO photo(photo_project_id,photo_section,photo_subsection,photo_date,photo_post,photo_desc,photo_url,photo_dir)VALUES('$_POST[photo_project_id]','$_POST[photo_section]','$_POST[photo_subsection]','$_POST[photo_date]',now(),'$_POST[photo_desc]','$_POST[photo_url]','$file_name')";

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.

Categories