469,645 Members | 1,702 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,645 developers. It's quick & easy.

problem to store and retrive image in php

The below code can't take the images.plz tell that what is the problem in that code
[php]
<?php

// Connect to database

$errmsg = "";
if (! @mysql_connect("localhost","root","sreeni")) {
$errmsg = "Cannot connect to database";
}
@mysql_select_db("my_db1");

// First run ONLY - need to create table by uncommenting this
// Or with silent @ we can let it fail every sunsequent time ;-)

$q = <<<CREATE
create table pix (
pid int primary key not null auto_increment,
name varchar(20),email varchar(30),city varchar(20),state varchar(20),zip int(7),country varchar(20),month varchar(20),date int(3),imgdata longblob)
CREATE;
@mysql_query($q);

// Insert any new image into database
if ($_REQUEST[completed] == 1) {
// Need to add - check for large upload. Otherwise the code
// will just duplicate old file ;-)
// ALSO - note that latest.img must be public write and in a
// live appliaction should be in another (safe!) directory.
move_uploaded_file($_FILES['upload']['tmp_name'],"latest.img");
$instr = fopen("latest.img","rb");
$image = addslashes(fread($instr,filesize("latest.img")));
if (strlen($instr) < 149000) {
mysql_query ("insert into pix13(name,email,city,state,zip,country,month,date ,upload) values ('\"". $_POST[name] . "\",\"".$_POST[email]. "\",\"".$_POST[city]. "\",\"".$_POST[state]. "\",\"".$_POST[zip]. "\",\"".$_POST[country]. "\",\"".$_POST[month]. "\",\"".$_POST[date]. "\",\"". $_POST[whatsit] . "\", \"". $image . "\")");
} else {
$errmsg = "Too large!";
}
}
$gotten = @mysql_query("select * from pix order by pid desc limit 1");
if ($row = @mysql_fetch_assoc($gotten)) {
$name = htmlspecialchars($row[name]);
$email = htmlspecialchars($row[email]);
$city = htmlspecialchars($row[city]);
$state = htmlspecialchars($row[state]);
$zip = htmlspecialchars($row[zip]);
$country = htmlspecialchars($row[country]);
$month = htmlspecialchars($row[month]);
$date = htmlspecialchars($row[date]);
$bytes = $row[upload];
} else {
$errmsg = "There is no image in the database yet";
// Put up a picture of our training centre
$instr = fopen("latest.img","rb");
$bytes = fread($instr,filesize("latest.img"));
}
// If this is the image request, send out the image

if ($_POST[gim] == 1) {
header("Content-type: image/jpeg/jpg/zip");
print $bytes;
exit ();
}

?>[/php]

Before you continue: Please enclose any code within the proper code tags. See the Posting Guidelines on how to do that. - moderator
Mar 3 '08 #1
3 1808
bonski
53
The below code can't take the images.plz tell that what is the problem in that code
what exactly you want to do? upload image file and display it on a webpage?
Mar 3 '08 #2
bonski
53
ok... i checked your script... for me.. i think you sql statement is having some errors or cant successfully insert the data.. because you lack some single quotes in your $_POST[]...

i think this one
[PHP]mysql_query ("insert into pix13(name,email,city,state,zip,country,month,date ,upload) values ('\"". $_POST[name] . "\",\"".$_POST[email]. "\",\"".$_POST[city]. "\",\"".$_POST[state]. "\",\"".$_POST[zip]. "\",\"".$_POST[country]. "\",\"".$_POST[month]. "\",\"".$_POST[date]. "\",\"". $_POST[whatsit] . "\", \"". $image . "\")");[/PHP]


should look like this

[PHP]mysql_query ("INSERT INTO pix13(name, email, city, state, zip, country, month, date, upload) VALUES('".$_POST['name']."', '".$_POST['email']."', '".$_POST['city']."', '".$_POST['state']."', '".$_POST['zip']."', '".$_POST['country']."', '".$_POST['month']."', '".$_POST['date']."', '".$_POST['whatsit']."', '".$image."')");[/PHP]

and for uploading files, you can have a look at this
file uploads
Mar 3 '08 #3
ronverdonk
4,258 Expert 4TB
The below code can't take the images.plz tell that what is the problem in that code
That is not a clear description of your problem. Please describe it in more detail and let's not make a puzzle of it.

Ronald
Mar 3 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Patrick F | last post: by
2 posts views Thread by soma.gunasekaran | last post: by
1 post views Thread by =?Utf-8?B?SHVzYW0=?= | last post: by
2 posts views Thread by Eminosoft | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.