how to insert multiple images to database in one row - php

I have made up this html form
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
<input type="file" name="image1" /><br/>
<input type="file" name="image2" /><br/>
<input type="submit" name='submit' value="upload" />
</form>
this is my php code
<?php
include "conf/connect.php";
if (isset($_POST['submit'])){
$uploadpath1 = 'upload/';
$image1_name = $_FILES['image1']['name'];
$image1_size = $_FILES['image1']['size'];
$image1_type = $_FILES['image1']['type'];
$image1_url =
$image1_temp_name = $_FILES['image1']['tmp_name'];
$uploadpath1 = $uploadpath1. time() . basename($image1_name);
$image1_url = 'http://'.$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['REQUEST_URI']), '\\/').'/'.$uploadpath1;
////
if(empty($errors)) {
move_uploaded_file($image1_temp_name, $uploadpath1);
$success[] = 'Uploaded!';
}
}
///
if (isset($_POST['submit'])){
$uploadpath2 = 'upload/';
$image2_name = $_FILES['image2']['name'];
$image2_size = $_FILES['image2']['size'];
$image2_type = $_FILES['image2']['type'];
$image2_temp_name = $_FILES['image2']['tmp_name'];
$uploadpath2 = $uploadpath2. time() . basename($image2_name);
$image2_url = 'http://'.$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['REQUEST_URI']), '\\/').'/'.$uploadpath2;
////
if(empty($errors)) {
move_uploaded_file($image2_temp_name, $uploadpath2);
$success[]= 'Uploaded';
}
}
if(isset($_POST['submit'])){
$id = $_GET['id'];
$table = 'products';
mysqli_query($connect, "UPDATE `$table` SET `image1` = $uploadpath1, `image2` = $uploadpath2 WHERE `id` = $id");
}
?>
All in : image_multi.php
So when i post submit .. the images uploaded successfully but nothing updated in my table
my table
I run this link : mydomainname.com/image_multi.php?id=1
images uploaded but not appears in database at all
Thanks

Check type and length for fields 'image1' and 'image2' in your table

the problem is definitely in the mysql statement, you didn't quote the filenames that's why the update doesn't run through. in the future simply check the db log for errors.
to fix this should work
mysqli_query($connect, "UPDATE `$table` SET `image1` = '$uploadpath1', `image2` = '$uploadpath2' WHERE `id` = $id");
notice the ' enclosing your $vars, that's only for when you need strings in the rows - which you clearly need. I'm not sure the Id should be string, check to see if it's numeric.

Related

Is it possible to insert image into database without using forms in php

i'm using a the following code to insert image into the database
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="image">
<button type="submit" name="upload">Upload</button>
</form>
<?php
if(isset($_POST['upload']) && isset($_FILES['image']))
{
if(getimagesize($_FILES['image']['tmp_name'])==FALSE)
{
echo "Please select an image";
}
else
{
$image = addslashes($_FILES['image']['tmp_name']);
$name = addslashes($_FILES['image']['name']);
$image = file_get_contents($image);
$image=base64_encode($image);
}
}
//img_name(datatype: varchar) and img_content(datatype: longblob)
$SQL = "INSERT INTO image_db (img_name, img_content) VALUES ('$name', '$image')";
$result = mysqli_query($db_handle, $SQL);
?>
Is it possible to assign an image path from a folder in the local storge to a php variable and insert it into the database? I dont want to insert the path but i want to store the image in the variable in the specified path and insert it the same way as shown above.
For example:
<?php
$image = "c:\images\default.png"
$name = "default.png"
$image = addslashes($image);
$name = addslashes($name);
$image = file_get_contents($image);
$image=base64_encode($image);
$SQL = "INSERT INTO image_db (img_name, img_content) VALUES ('$name', '$image')";
$result = mysqli_query($db_handle, $SQL);
?>
Please help me out.
Thanks in advance

Cannot add post and images value into database

i'm trying to add text and images using
Image is successfully added into folder path. but the value of text and images did not add into database
<?php
function insertpost(){
global $connect;
if(isset($_POST['sendpost']))
{
$title = mysqli_real_escape_string($connect,$_POST["title"]);
$target_image = "images/".basename($_FILES['post_image']['name']);
$post_image = $_FILES['post_image']['name'];
$insert_post_and_image = "INSERT INTO table(title, image) VALUES ('$title','$post_image')";
mysqli_query($connect, $insert_post_and_image);
if(move_uploaded_file($_FILES['post_image']['tmp_name'], $target_image))
{
echo "<h3>Posted to timeline!</h3>";
}
}
}
?>
html code
<form action="" method="post" id="form" enctype="multipart/form-data">
<input type="text" name="title"/>
<input type="file" name="post_image"/>
<input type="submit" name="sendpost" value="POST"/>
</form>
any solution? thanks
I think you have mistake in table name in your query .Just replace table with your actual table name.For example if you have defined table name as user then your query must be
$insert_post_and_image = "INSERT INTO user(title, image) VALUES ('$title','$post_image')";
Also you can check error using
if (mysqli_query($connect, $insert_post_and_image)) {
echo "success";
} else {
echo "Error: " . mysqli_error($connect);
}
*correction
Hi, already found the answer . i'm not sure if it's correct or not . please advise. thanks :)
<?php
function insertpost(){
global $connect;
if(isset($_POST['sendpost']))
{
$title = mysqli_real_escape_string($connect,$_POST["title"]);
$target_image = "images/".basename($_FILES['post_image']['name']);
$post_image = $_FILES['post_image']['name'];
$insert_post_and_image = "INSERT INTO table(title, image) VALUES ('$title','$post_image')";
$result = mysqli_query($connect, $insert_post_and_image);
if($result)
{
move_uploaded_file($_FILES['post_image']['tmp_name'], $target_image);
echo "<h3>Posted to timeline!</h3>";
}
}
}
?>

php - warning when inserting JPEG into SQL Database

I'm trying to upload an image into an SQL server using a simple form however I'm being faced with the following warning, which also prevents me from uploading the image:
Warning: odbc_exec(): in C:\wamp\www\inputform\index2.php on line 10
Line 10: $sql_run = odbc_exec($con, $sql);
The code is:
<?php
require ('connect.inc.php');
if(ISSET($_POST['submit'])){
$imagename = $_FILES["image"]["name"];
$imagedata = file_get_contents($_FILES["image"]["tmp_name"]);
echo $imagename." ".$imagedata;
$sql = "INSERT INTO test.dbo.images (imageid, imagedata) VALUES('','$imagedata')";
$sql_run = odbc_exec($con, $sql);
echo "Query with the following details has been executed: <br>".$imagename;
}
else{
echo 'Is not set';
}
?>
<form action="index2.php" method="POST" enctype="multipart/form-data">
Image: <input type="file" name="image"><br>
<input type="submit" value="Submit" name="submit">
</form>
The file connect.inc.php consists of the code below:
<?php
$serverName="TESTSERV\SQLEXPRESS";
$dsn='odbc-test';
$user = 'user';
$password = 'password';
$db = 'test';
if(!$con = odbc_connect($dsn, $user, $password)){
echo "Not Connected";
}
?>
Thanks in advance.
J
The query looks fine so the only logical explanation is that the datatypes you are trying to insert are not same. Make sure that imageid and imagedata datatypes are varchar
EDIT: Make sure that the datatypes are same. here is the updated answer.
<?php
require ('connect.inc.php');
if(ISSET($_POST['submit'])){
$imagename = $_FILES["image"]["name"];
$imagedata = (binary)file_get_contents($_FILES["image"]["tmp_name"]);
echo $imagename." ".$imagedata;
$sql = "INSERT INTO test.dbo.images (imageid, imagedata) VALUES('','.$imagedata.')";
$sql_run = odbc_exec($con, $sql);
echo "Query with the following details has been executed: <br>".$imagename;
}
else{
echo 'Is not set';
}
?>
note: $imagedata = (binary)file_get_contents($_FILES["image"]["tmp_name"]);

<input type = "file"> EMPTY

i have this problem regarding file upload on php.
I always get this error msg.
Warning: file_get_contents(): Filename cannot be empty in
C:\xampp\htdocs\omf2\emprecords\add8.php on line 25
this is my line 25
$data = $con->real_escape_string(file_get_contents($_FILES['uploaded_file']['tmp_name']));
But still saves the info on my database.
What i am trying to do is save the rest of the records on my database even if not selecting a file to upload. And yes the records are saved and the Attachment field (mediumblob) is [BLOB - 0 B]
Question: How can i eliminate the error/warning message? (because everything is really fine)
<meta http-equiv="refresh" content="2;URL='emphistory.php'">
<?php
{
echo "<center><font color='#AAA' size='3'><br/>Record Added!</center>";
}
?>
<?php
$con=mysqli_connect("localhost","root","","dbomf");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM valueholder");
$row = mysqli_fetch_array($result);
$count = '';
$IDNUM = $row['Val'];
$NS = addslashes($_POST ['NS']);
$ad = addslashes($_POST ['ad']);
$hr = addslashes($_POST ['HR']);
$name = $con->real_escape_string($_FILES['uploaded_file']['name']);
$data = $con->real_escape_string(file_get_contents($_FILES['uploaded_file']['tmp_name']));
include ('../dbconn.php');
$query = "INSERT INTO tblemphist1 VALUES
('".$count."', '".$IDNUM."', '".$NS."', '".$ad."', '".$hr."', '".$data."', '".$name."')";
$result = $db->query($query) or die($db->error);
$db->close();
here
<form method="post" action="add8.php" enctype="multipart/form-data">
<td><strong>Attachment</strong></td>
<td>:</td>
<td><input type="file" name="uploaded_file"></td>
</tr>
</form>
<input type = "file">
should be
<input name="uploaded_file" type = "file">
also form method should be post and use enctype='multipart/form-data
<form action="" method="post" enctype="multipart/form-data">
<input name="uploaded_file" type = "file">
</form>
also check
$name = ''; $data = '';
if ((is_uploaded_file($_FILES['uploaded_file']['tmp_name']) && !($_FILES['uploaded_file']['error'])) {
$name = $con->real_escape_string($_FILES['uploaded_file']['name']);
$data = $con->real_escape_string(#file_get_contents($_FILES['uploaded_file']['tmp_name']));
}
include ('../dbconn.php');
$query = "INSERT INTO tblemphist1 VALUES ('".$count."', '".$IDNUM."', '".$NS."', '".$ad."', '".$hr."', '".$data."', '".$name."')";
$result = $db->query($query) or die($db->error);
Use an if statement. For example:
if (!empty($_FILES)) {
$data = $con->real_escape_string(
file_get_contents($_FILES['uploaded_file'] ['tmp_name'])
);
}
Before accessing any property of $_FILES['uploaded_file'] you have to check the value $_FILES['uploaded_file']['error']. And yes, it's a good idea to check if such key exists at all - as with anything coming from the user, there is no guarantee that it exists in the request.
Simply check if the variable is not empty
$data = '';
if (!empty($_FILES['uploaded_file']['tmp_name'])) {
$data = $con->real_escape_string(file_get_contents($_FILES['uploaded_file']['tmp_name']));
}
if error doesn't affect your project just ignore it and add this code in top of your php.
<?php ERROR_REPORTING(E_ALL & ~E_NOTICE); ?>
it will ignore and hide the error. :)

how to simple multiple file upload using name="image[ ]"

Im new to php and would like to ask about uploading multiple files using name="image[]". Here's my code :
HTML
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="image[]" multiple="multiple"/>
<input type="submit" name="submit"/>
</form>
PHP
<?php
$table_name ="posts";
$_POST['submit'];
$title = $_POST['title'];
$cat_id = $_POST['cat_id'];
$date = date('Y-m-d', strtotime($_POST['date']));
$description = mysql_real_escape_string($_POST['description']);
$status = $_POST['status'];
$sql = "INSERT INTO $table_name (title,cat_id,date,description,status) VALUES ('$title','$cat_id','$date','$description','$status')";
$result = mysql_query($sql);
$sqlImage = mysql_fetch_array(mysql_query("SELECT id FROM $table_name ORDER BY id DESC "));
$parentId = $sqlImage['id'];
foreach ($_POST['image'] as $images) {
$resImage = mysql_query("INSERT INTO media (parent_id,image) VALUES ('$parentId','$images') ");
}
$_SESSION['add_content'] = true;
echo ' <script>location.href="'.$base_url.''.$table_name.'"</script>';
?>
How can I input my image?
First of all the $_POST['submit'] currently doesn't do anything. Secondly uploading files requires you to use $_FILES instead of $_POST.
This should work to get all of the files uploaded
foreach($_FILES['image'] as $file)
{
//do something with the data
}
PHP manual : $_FILES
Hope this helps!

Categories