Uploading Images into database with line of text.
Hello,
I have another PHP problem. Because I'm very busy these days, I don't have too much time to update my website.
What I have is a page with two frames, left and right. The left frame contains links to images, which load in the right frame. The right frame also loads with a comment on the picture. So I went looking and I read about people storing images in the database, and thought if I modified it up, this could be a real time saver. So what Im trying to do, is upload a picture and a line of text into my database. So heres what I have:
db.sql
CREATE TABLE image (
image_id int(10) unsigned NOT NULL auto_increment,
image_type varchar(50) NOT NULL default '',
image longblob NOT NULL,
image_size bigint(20) NOT NULL default '0',
image_name varchar(255) NOT NULL default '',
image_date datetime NOT NULL default '0000-00-00 00:00:00',
UNIQUE KEY image_id (image_id)
);
------
index1.php
if (in_array (strtolower ($file_type), $image_types)) {
$sql = "INSERT INTO image (image_type, image, image_size, image_name, image_date, comment) ";
$sql.= "VALUES (";
$sql.= "'{$file_type}', '{$userfile}', '{$file_size}', '{$file_name}', NOW())";
@mysql_query ($sql, $conn);
Header("Location:".$_SERVER["PHP_SELF"]);
exit();
}
}
if ($_GET) {
$iid = $_GET["iid"];
$act = $_GET["act"];
switch ($act) {
case rem:
$sql = "DELETE FROM image WHERE image_id=$iid";
@mysql_query ($sql, $conn);
Header("Location:./index1.php");
exit();
break;
default:
print "<img src=\"image.php?iid=$iid\">";
break;
}
}
?>
<html>
<head>
<title>Image Database</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
Select Image File: <input type="file" name="userfile" size="40"><input type="text" value=""><input type="submit" value="submit">
</form>
<?php
$sql = "SELECT * FROM image ORDER BY image_date DESC";
$result = mysql_query ($sql, $conn);
if (mysql_num_rows($result)>0) {
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$i++;
$str .= $i.". ";
$str .= "<a href=\"index1.php?iid=".$row["image_id"]."\">".$row["image_name"]."</a> ";
$str .= "[".$row["image_date"]."] ";
$str .= "[".$row["image_size"]."] ";
$str .= "[".$row["comment"]."] ";
$str .= "[<a href=\"index1.php?act=rem&iid=".$row["image_id"]."\">Remove</a>]<br>";
}
print $str;
}
?>
</body>
</html>
-----
image1.php
<?php
$conn = mysql_connect("localhost", "database", "password") OR DIE (mysql_error());
@mysql_select_db ("illcon_images", $conn) OR DIE (mysql_error());
$sql = "SELECT * FROM image WHERE image_id=".$_GET["iid"];
$result = mysql_query ($sql, $conn);
if (mysql_num_rows ($result)>0) {
$row = @mysql_fetch_array ($result);
$image_type = $row["image_type"];
$image = $row["image"];
$comment = $row["comment"];
Header ("Content-type: $image_type");
print $image;
}
?>
Once I fill all the information in, and hit submit, the page loads, but no text or image is displayed. Thanks in advanced.
Cheers.
|