How to Upload Image PHP and Insert Path in MySQL

How to upload image and save path to database?

After some hours I found a solution. It works. Although I would still be happy to find a second solution (according to the code I first posted here). Here is the second solution :

form page :

<form enctype="multipart/form-data" action="insert_image.php" method="post" name="changer">
<input name="image" accept="image/jpeg" type="file">
<input value="Submit" type="submit">
</form>

insert to database page :

<?php

include 'conf.php';

if ($_FILES["image"]["error"] > 0)
{
echo "<font size = '5'><font color=\"#e31919\">Error: NO CHOSEN FILE <br />";
echo"<p><font size = '5'><font color=\"#e31919\">INSERT TO DATABASE FAILED";
}
else
{
move_uploaded_file($_FILES["image"]["tmp_name"],"images/" . $_FILES["image"]["name"]);
echo"<font size = '5'><font color=\"#0CF44A\">SAVED<br>";

$file="images/".$_FILES["image"]["name"];
$sql="INSERT INTO eikones (auxon, path) VALUES ('','$file')";

if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
echo "<font size = '5'><font color=\"#0CF44A\">SAVED TO DATABASE";

}

mysql_close();

?>

Upload image to server and store image path in mysql database

SQL:

CREATE TABLE `uploads` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`filename` varchar(255) NOT NULL,
`path` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

PHP:

$file_path = "uploads/";

$file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
// replace $host,$username,$password,$dbname with real info
$link=mysqli_connect($host,$username,$password,$dbname);
mysqli_query($link,"INSERT INTO `files` (filename,path) VALUES ('".$_FILES['uploaded_file']['tmp_name']."','".$file_path."')") or trigger_error($link->error."[ $sql]");
mysqli_close($link);

} else{
echo "fail";

Insert image directory into MySQL

Try this :

// The directory that will recieve the uploaded file
$dir = 'uploads/';

//variables for images
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["picture"]["name"]);
$extension = end($temp);

if(isset($_POST['submit'])) {
if ((strlen($title) > 0) && (strlen($description) > 0)) {
$newFilePath = $dir . "/" . $title . "." . $extension;
move_uploaded_file($_FILES['picture']['tmp_name'], $newFilePath);

// Query database and insert data into item table
$itemQry = 'INSERT INTO items
(title, picture, startPrice, category, description,
quantity, location, sellingFormat, duration,
paymentType, postageDetails)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';

$statement = $conn->prepare($itemQry);
$statement->bind_param('sssssssssss', $title, $newFilePath,
$startPrice, $category, $description,
$quantity, $location, $sellingFormat,
$duration, $paymentType, $postageDetails);

$statement->execute();
}
}

Note: Here I have used $newFilePath as a new variable which keeps the path and the new file name and using the same to insert into DB. Hope this helps you :).

Upload image to web server and store path in database

You need a valid enctype when uploading files: enctype="multipart/form-data"

Change it to:

  <form method="POST" action="post.php" enctype="multipart/form-data"> 

you need change variable to this too because $_FILES['image']['name'] is just a name, so put this to run move_uploaded_file() function

 $image = $_FILES['image']['tmp_name'];  

how to insert an image file into database table

2 possibilites:

1 Save to file server(prefered):

Save the image on your file server and save the path where you saved it in the database. This allows you to directly access this file and helps you to better output it to the user than with the second method. I'd prefer this.

Explanation: upload file with php and save path to sql

2 Save to database:

You can save the content of images to a BLOB (Binary large object).

Explanation: Insert Blobs in MySql databases with php

PHP upload image to folder and insert record to database

This is probably returning an error which the code is ignoring:

INSERT INTO users (img_path, img_name, img_type) VALUES ('$path', '$file', '$ext') WHERE id = '$session_user_id'

INSERT statements don't have WHERE clauses. Just omit the clause entirely:

INSERT INTO users (img_path, img_name, img_type) VALUES ('$path', '$file', '$ext')

Additionally, you should check for SQL errors after executing a SQL statement.


Edit: Or... Does this need to be an UPDATE statement instead? It strikes me as strange that a table called users would hold image records. Are you instead updating an existing user record to include new values? That would have a WHERE clause. Something like this:

UPDATE users SET img_page='$path', img_name='$file', img_type='$ext' WHERE id = '$session_user_id'


Related Topics



Leave a reply



Submit