QuizToon
07-03-2006, 10:31 PM
Hi all
Got 2 questions here.
I have managed, after much reading trial and error to get a form to post a picture into a directory and the path and image name etc in the mysql database, all that is working fine.
I have even managed to get the pictures to get a page working which extracts the information onto the page. Except
question 1 - All of the fields I have asked for, display but the picture is just the place holder with the little red x in the top left corner. The picture is sitting in the correct directory. I have a feeling it is something to do with the path but I dont know. the path is in this file:
<html>
<head>
<title>Upload File To MySQL Database</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.box {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
border: 1px solid #000000;
}
-->
</style>
</head>
<body>
<?
// you can change this to any directory you want
// as long as php can write to it
$uploadDir = '/home/sites/grap.co.uk/public_html/upload/pictures/';
if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$comment = $_POST['comment'];
// the files will be saved in filePath
$filePath = $uploadDir . $fileName;
// move the files to the specified directory
// if the upload directory is not writable or
// something else went wrong $result will be false
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}
include 'library/config.php';
include 'library/opendb.php';
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
$query = "INSERT INTO upload2 (name, size, type, path, comment ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$filePath', '$comment')";
mysql_query($query) or die('Error, query failed : ' . mysql_error());
include 'library/closedb.php';
echo "<br>File uploaded<br>";
}
?>
<form action="" method="post" enctype="multipart/form-data" name="uploadform">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td width="246"><p>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
Select Photograph to upload<input name="userfile" type="file" class="box" id="userfile">
</p>
<p>Comment <textarea rows="10" name="comment" cols="30"></textarea>
</p></td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>
</body>
</html>
Question 2. - How do I make it so that all of the entries in the database display one below another instead of just the last picture uploaded. Each picture and its comments have an id which is the unique key.
My display file is
<?php
// This is the config
$dbhost = 'localhost';
$dbuser = 'web23-dbase';
$dbpass = 'daddad';
$dbname = 'web23-dbase';
// This is to open the database
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
//This is the query
$query = "SELECT * FROM upload2";
$result = mysql_query ($query);
while ($row = mysql_fetch_assoc ($result)) {
$text = $row['id'];
$url = $row['path'];
$description = $row['comment'];
}
// This is to close database
mysql_close($conn);
?>
<table width="800" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center"><img src="images/<?php echo"$url" ?>" width="250" height="250" alt=""><br><?php echo"$url" ?></td>
<td align="left" valign="top"><?php echo"$description" ?></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
If yoou have read this far I thank you very much for your time, I hope someone can shed some light on this for me. I am very new to php and am learning albeit slowly.
Thanks again:)
Got 2 questions here.
I have managed, after much reading trial and error to get a form to post a picture into a directory and the path and image name etc in the mysql database, all that is working fine.
I have even managed to get the pictures to get a page working which extracts the information onto the page. Except
question 1 - All of the fields I have asked for, display but the picture is just the place holder with the little red x in the top left corner. The picture is sitting in the correct directory. I have a feeling it is something to do with the path but I dont know. the path is in this file:
<html>
<head>
<title>Upload File To MySQL Database</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.box {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
border: 1px solid #000000;
}
-->
</style>
</head>
<body>
<?
// you can change this to any directory you want
// as long as php can write to it
$uploadDir = '/home/sites/grap.co.uk/public_html/upload/pictures/';
if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$comment = $_POST['comment'];
// the files will be saved in filePath
$filePath = $uploadDir . $fileName;
// move the files to the specified directory
// if the upload directory is not writable or
// something else went wrong $result will be false
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}
include 'library/config.php';
include 'library/opendb.php';
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
$query = "INSERT INTO upload2 (name, size, type, path, comment ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$filePath', '$comment')";
mysql_query($query) or die('Error, query failed : ' . mysql_error());
include 'library/closedb.php';
echo "<br>File uploaded<br>";
}
?>
<form action="" method="post" enctype="multipart/form-data" name="uploadform">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td width="246"><p>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
Select Photograph to upload<input name="userfile" type="file" class="box" id="userfile">
</p>
<p>Comment <textarea rows="10" name="comment" cols="30"></textarea>
</p></td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>
</body>
</html>
Question 2. - How do I make it so that all of the entries in the database display one below another instead of just the last picture uploaded. Each picture and its comments have an id which is the unique key.
My display file is
<?php
// This is the config
$dbhost = 'localhost';
$dbuser = 'web23-dbase';
$dbpass = 'daddad';
$dbname = 'web23-dbase';
// This is to open the database
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
//This is the query
$query = "SELECT * FROM upload2";
$result = mysql_query ($query);
while ($row = mysql_fetch_assoc ($result)) {
$text = $row['id'];
$url = $row['path'];
$description = $row['comment'];
}
// This is to close database
mysql_close($conn);
?>
<table width="800" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center"><img src="images/<?php echo"$url" ?>" width="250" height="250" alt=""><br><?php echo"$url" ?></td>
<td align="left" valign="top"><?php echo"$description" ?></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
If yoou have read this far I thank you very much for your time, I hope someone can shed some light on this for me. I am very new to php and am learning albeit slowly.
Thanks again:)