Webmaster Forums 101 - Free Help and Discussions
 
 
 

MEMBER REWARDS - Earn free advertising by participating in these forums! Click here for details.

Welcome to Webmaster Forums 101!

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

Reply
 
Thread Tools Search this Thread Display Modes
Old 16th November 2003, 03:34 PM   #1

Username: mr t
Rank: WF101 Newbie
Join Date: Apr 2003
Posts: 20
Points: 0.00
Default 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.
mr t is offline   Reply With Quote
Old 17th November 2003, 07:42 AM   #2
Mr. Tech's Avatar

Username: Mr. Tech
Rank: Problem Solver
Join Date: Dec 2004
Location: Gold Coast, Australia
Posts: 2,166
Points: 1,025.61
Send a message via MSN to Mr. Tech
Default Re: Uploading Images into database with line of text.

OK, you will need to upload the file to the server first otherwise where is the image going to link to?

Below is the code you will need for index1.php:

Code:
// Where the images well be placed. // Set this folders permissions writable. $uploaddir = "images"; if(is_uploaded_file($_FILES['userfile']['tmp_name']) && $continue != "no") { $path_parts = pathinfo($_FILES['userfile']['name']); $file_type = $path_parts['extension']; $file_size = $_FILES['userfile']['size'] $file_name = $_FILES['userfile']['name']; move_uploaded_file($_FILES['userfile']['tmp_name'],$uploaddir'/'.$_FILES['userfile']['name']); } $userfile = $_FILES['userfile']['name']; 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(); } }

Hope that helps!
__________________
Please read the Forum Rules

Get rewarded for posting
- free advertising!

.::. [ Webmaster Resources ] .::. [ Web Design ] .::. [ Developer Tutorials ] .::.
Mr. Tech is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Ride4Pain metguitarist Website Design Reviews 14 29th April 2003 11:41 AM

Login
User Name
Password


Featured Members


Featured Links


Useful Resources
Webmaster Resources
Developers Tools
Developer Tutorials


Partners

All times are GMT +10. The time now is 07:51 AM.


Powered by vBulletin Version 3.5.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 2.3.2
 
 
WEBMASTER FORUMS 101
ARCHIVE
CONTACT US
TOP
   
© Webmaster Forums 101 2005. All Rights Reserved.
Design by: vBCore