PDA

View Full Version : Storing images - mysql..


Tjobbe
February 13th, 2006, 20:33
I have a simple form I'm using to store both text and images, the text will be stored to the mysql but the image will uploaded to a folder.

How do I set the field up in mysql / phpmyadmin to handle the image?

I've not done this for a long time so I might not be making any sense! Would appreciate any help, thanks!

Tjobbe
February 13th, 2006, 21:38
I managed to figure it out in the end!


I'm having problem storing anything to the database though. The image upload is working fine but the text storing isn't!

Below are my dbconnect.php, store.php and submit.php files as well as a copy of my database, if anyone can see what I'm doing wrong I'd be eternally grateful!

Tjobbe

dbconnect.php
<?php

$c = mysql_connect ('localhost', 'root'/*, ''*/);

if (!$c) die("Crap.. not again " . mysql_error());

mysql_select_db('articles', $c) or die(" Grrrr " . mysql_error());

?>


store.php
<!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=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<?php

$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
include 'dbconnect.php';



$sqlquery = "INSERT INTO `journal` ( `id`, `title`, `date`, `intro`, `image`, `link`, `linktext`) VALUES('$id', '$title', '$date', '$intro', '". basename( $_FILES['uploadedfile']['name']). "', '$link', '$linktext')";


$results = mysql_query($sqlquery);

mysql_close();

print "<html><body><center>";
print "<p>You have just entered this record<p>";
print "<strong>title :</strong> $title<br>";
print "<strong>date :</strong> $date<br>";
print "<strong>intro :</strong> $intro<br>";
print "<strong>linktext :</strong> $description<br>";
print "<strong>image :</strong> $image<br>";
print "</body></html>";
?>

</body>
</html>


submit.php
<!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=iso-8859-1" />
<title>Add a new article</title>
<style type="text/css">
<!--
body,td,th {
font-family: Arial, Helvetica, sans-serif;
font-size: 90%;
color: #000000;
}
body {
background-color: #FFFFFF;
}
-->
</style></head>

<body>
<form enctype="multipart/form-data" name="form" method="post" action="store.php">
<table width="75%" border="0" align="center" cellpadding="4" cellspacing="0">
<tr>
<td width="135">Title:</td>
<td>
<p>
<input name="title" id="title" value="Article Title" />
</p>
</td>
</tr>
<tr>
<td>Date:</td>
<td>
<p>
<input name="date" id="date" value="Todays Date" />
</p>
</td>
</tr>
<tr>
<td width="135">Introduction:</td>
<td rowspan="2">
<textarea name="intro" cols="35" rows="5" id="intro">Brief Description</textarea>
</td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td>
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />Choose a file to upload:
</td>
<td>
<input name="uploadedfile" type="file" />
</td>
</tr>
<tr>
<td>Links to URL: </td>
<td>
<input name="link" id="link" value="http://" />
</td>
</tr>
<tr>
<td>Anchor Text (link name) </td>
<td>
<input name="linktext" id="linktext" value="e.g: Click Here" />
</td>
</tr>
<tr>
<td colspan="2">
<div align="center">
<input type="submit" name="Submit" value="Submit" />
</div>
</td>
</tr>
</table>
</form>
</body>
</html>


journal.sql
-- phpMyAdmin SQL Dump
-- version 2.7.0-pl1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Feb 13, 2006 at 08:08 PM
-- Server version: 5.0.17
-- PHP Version: 5.1.1
--
-- Database: `articles`
--

-- --------------------------------------------------------

--
-- Table structure for table `journal`
--

CREATE TABLE `journal` (
`id` tinyint(4) NOT NULL auto_increment,
`title` text collate latin1_general_ci NOT NULL,
`date` text collate latin1_general_ci NOT NULL,
`intro` text collate latin1_general_ci NOT NULL,
`link` text collate latin1_general_ci NOT NULL,
`image` varchar(255) collate latin1_general_ci default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1 ;

--
-- Dumping data for table `journal`
--



It must be something really simple but I can't see it anwyhere..

Christopher Lee
February 13th, 2006, 22:42
I'm having problem storing anything to the database though. The image upload is working fine but the text storing isn't!


Only glanced over your code, and I'm not sure if I understand your question correctly, but:

I didn't see you set the variables using $_POST there. Could it be that register globals is off and therefore they aren't set?

Then I reread your post, and am wondering: is the section where you try to insert the basename the only part of the string that isn't working? Have you dumped the string to output to see what it prints?

Christopher Lee
February 13th, 2006, 22:51
Also, in the insert string, wouldn't the value for image be

basename($target_path)

instead of

basename( $_FILES['uploadedfile']['name'])

Although, rereading it again, I'm not sure if they would or wouldn't return the same value, now that I think of it. I'll actually have to sit down and experiment with it.

Christopher Lee
February 24th, 2006, 19:03
Tjobbe, did you ever resolve this to your satisfaction?

Tjobbe
February 24th, 2006, 19:07
Yes I did, I completely forgot to say thank you, how rude of me!

thanks for your help with this, all sorted now!

adhp
March 16th, 2006, 04:51
Hi Tjobbe,

I am trying to do eactly the same thing.
Came across your thread.
Could you help me achieve this upload and write and retrieve of image from a form please?

Ta Bev

Tjobbe
March 16th, 2006, 10:50
Exactly what do you need to do? Will you be storing text as well and need to retrieve the text at the same time?

I should be able to dig up what it was I made and just post it here.