Hi there,
I was hoping for a little help please?? Basically i have created 2 pages(addimage.php, image.php), addImage.php is the page that will allow the user to insert a record to the database done through a basic form:
<?php
if ($_POST['Submit']) {
if ($_POST['MAX_FILE_SIZE'] >= $_FILES['file']['size']) {
//print_r($_FILES);
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("woodside");
$photo = addslashes(fread(fopen($_FILES['file']['tmp_name'], "r"),
$_FILES['file']['size']));
$query = sprintf("INSERT INTO image(Image, FileType) VALUES
('%s', '%s')", $photo, $_FILES['file']['type']);
if (mysql_query($query)) {
$messages[] = "Your files is successfully store in database";
} else {
$messages[]= mysql_error();
}
} else {
$messages[]="The file is bigger than the allowed size please resize";
}
}
?>
<html>
<head>
<title>Add Image</title>
</head>
<body>
<?php
if (isset($messages)) {
foreach ($messages as $message) {
print $message ."<br>";
}
}
?>
<form action="" method="post" enctype="multipart/form-data" name="form1">
<input type="file" name="file">
<input type="hidden" name="MAX_FILE_SIZE" value="96000">
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
The other page is Image.php, this is the code to display the image from the database:
<?php ini_set('error_reporting',E_ALL); ?>
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("woodside");
$result = mysql_query("SELECT ImageId from image");
while ($row = mysql_fetch_array($result)) {
$ids[]=$row['ImageId'];
}
?>
<html>
<head>
<title>Image Loader</title>
</head>
<body>
select image:<br>
<table width="80%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="10%">id</td>
<td width="90%">Image</td>
</tr>
<tr>
<td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<? foreach ($ids as $id) { ?>
<tr>
<td><a href="?id=<?= $id; ?>"><?= $id; ?></a></td>
</tr>
<? } ?>
</table></td>
<td><? if (isset($_GET['id'])) { ?>
<img src="image.php?id=<?= $_GET['id']; ?>"><? } ?></td>
</tr>
</table>
</body>
</html>
Personal Message (Online)
Image insertion and Retrieval
« on: Today at 07:58:38 AM »
Reply with quoteQuote
Hi there,
I was hoping for a little help please?? Basically i have created 2 pages, addImage.php is the page that will allow the user to insert a record to the database dont through a basic form:
Code:
<?php
if ($POST['Submit']) {
if ($POST['MAX_FILE_SIZE'] >= $FILES['file']['size']) {
//print_r($FILES);
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("woodside");
$photo = addslashes(fread(fopen($FILES['file']['tmp_name'], "r"),
$FILES['file']['size']));
$query = sprintf("INSERT INTO image(Image, FileType) VALUES
('%s', '%s')", $photo, $_FILES['file']['type']);
if (mysql_query($query)) {
$messages[] = "Your files is successfully store in database";
} else {
$messages[]= mysql_error();
}
} else {
$messages[]="The file is bigger than the allowed size please resize";
}
}
?>
<html>
<head>
<title>Add Image</title>
</head>
<body>
<?php
if (isset($messages)) {
foreach ($messages as $message) {
print $message ."<br>";
}
}
?>
<form action="" method="post" enctype="multipart/form-data" name="form1">
<input type="file" name="file">
<input type="hidden" name="MAX_FILE_SIZE" value="96000">
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
The other page is Image.php, this is suposed to display the image from the database:
Code:
<?php require_once('Connections/woodside.php'); ?>
<?php ini_set('error_reporting',E_ALL); ?>
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("woodside");
$result = mysql_query("SELECT ImageId from image");
while ($row = mysql_fetch_array($result)) {
$ids[]=$row['ImageId'];
}
?>
<html>
<head>
<title>Image Loader</title>
</head>
<body>
select image:<br>
<table width="80%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="10%">id</td>
<td width="90%">Image</td>
</tr>
<tr>
<td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<? foreach ($ids as $id) { ?>
<tr>
<td><a href="?id=<?= $id; ?>"><?= $id; ?></a></td>
</tr>
<? } ?>
</table></td>
<td><? if (isset($GET['id'])) { ?>
<img src="image.php?id=<?= $GET['id']; ?>"><? } ?></td>
</tr>
</table>
</body>
</html>
Inserting an image works fine, it just when trying to view the images i have a problem. No images or any dyamic data is displayed, leaving only the titles of the records "id" and "image" there!???
When i test out the page theres a shape of an image on the page but it has a cross through it???
If anybody can help me please, that would be great, ive been stuck on this for ages now.
Thank You