Issue
I am currently trying to create a .php page to upload images to store in a table in phpmyadmin. Then to display them.
First things first, adding the image to the table within the database. Hierarchy in phpmyadmin is: localhost -> images -> Greeting_Cards.
Currently I am trying to insert into Greeting Cards table, as I will have multiple tables for multiple categories later and display them respectively.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>upload</title>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file_upload" />
<input type="submit" name="submit" value="upload" />
</form>
<?php
include '/var/db_file.php';
if(isset($_POST['submit']))
{
$conn = mysql_connect("localhost","root", $pass);
mysql_select_db("images");
/* Variable inits */
$imageName = $imageData = $imageType = null;
$imageName = mysql_real_escape_string($_FILES["image"]["name"]);
$imageData = mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"]));
$imageType = mysql_real_escape_string($_FILES["image"]["type"]);
if(substr($imageType,0,5) == "image"){
mysql_query("INSERT INTO 'Greeting_Cards' VALUES('','$imageName','$imageData')");
echo "Image Uploaded!";
}
else{
echo "Has to be an image!";
}
}
?>
</body>
</html>
The uploaded image won't show up in the table. I am logging into the database correctly. The structure under the table "Greeting Cards" are: id(int11), name(varchar30), and image(blob). The only errors/warnings that shows up on the www.mydomainname.net/upload.php are:
Notice: Undefined index: image in /var/www/html/upload.php on line 27
Notice: Undefined index: image in /var/www/html/upload.php on line 28
Warning: file_get_contents(): Filename cannot be empty in /var/www/html/upload.php on line 28
Notice: Undefined index: image in /var/www/html/upload.php on line 29 Has to be an image!
To display the images, I have to get pass this step first. Will report back on this post if any update occurs.
Fix 1: Wrong name: Changed to name="image" to match with the php variable parameters. Fix 2: Back ticks NOT single quotes for table name. Optional: Specify columns.
Thanks in advance your help!
Solution
Fix 1: Wrong name: Changed to name="image" to match with the php variable parameters.
Fix 2: Back ticks NOT single quotes for table name. Optional: Specify columns.
Answered By - LeggoMaEggo
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.