Solved

insert image into database,php,mysql

Posted on 2009-04-06
12
971 Views
Last Modified: 2013-12-12
I need to insert an image into a database and be able to select them all later.. the insert works for the most part. but i get an issue where it will duplicat the last image when i insert the next one some of the time.

I need it to clear itself or flush itself so that a new image is uploaded each time.

Below is the code snippet that i'm working with and it works. it inserts the image into the database. however like i stated above. it will duplicate the previous image sometimes. i need help to make sure the new image is always uploaded.
if(isset($_POST['upload']))
{
        // 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.
        $title = $_REQUEST['title'];
        move_uploaded_file($_FILES['imagefile']['tmp_name'],"latest.img");
        $instr = fopen("latest.img","rb");
        $image = addslashes(fread($instr,filesize("latest.img")));
        if (strlen($instr) < 149000) {
        	
	$table="tournament_images";
	$cols="image,image_name";
	$vals="'$image','$title'";
	putData($table,$cols,$vals);
 
        } else {
                $errmsg = "Too large!";
        }
        $_FILES="";
        ShowData();
}

Open in new window

0
Comment
Question by:TrinitySJ
  • 7
  • 5
12 Comments
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24079114
What is in the putData() function?
0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 500 total points
ID: 24079130
Line 11 should probably be:

 if (strlen($image) < 149000) {
0
 
LVL 1

Author Comment

by:TrinitySJ
ID: 24082643
why $image instead of $instr

what is the difference.?

thanks in advance
0
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24082869
$image contains the image data, $instr contains the file handle. It is the size of the image data you want to check.
0
 
LVL 1

Author Comment

by:TrinitySJ
ID: 24083114
I tried that but it did not help.. i still get the same image uploaded he next time as the last image uploaded...
i need to make sure that it uploads the current image each time.

thanks...
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24083225
What is in the putData() function? That is where the storing is performed, without seeing that code it is impossible to say what is wrong.
0
 
LVL 1

Author Comment

by:TrinitySJ
ID: 24083345
sorry.. the putData is below...
/******************************************/
/* Start Function to do SQL INSERT *********/
function putData($table,$cols,$values)
{
	global $databasename;
	global $dbconn;
 
	$query="INSERT INTO $table($cols)VALUES($values)";
 
	// verify DB connection still exists...
	if(!mysql_select_db($databasename,$dbconn))
	{
		showerror();
		echo "OOPS-no DB!";
	}
	// create the LOCK TABLES statement
	$lock="LOCK TABLES $table WRITE";
 
	// lock the table....
	if(!(mysql_query($lock,$dbconn)))
	{
		echo $lock . "<br>";
		showerror();
	}
 
	if(!($results=mysql_query($query,$dbconn)))
	{
		echo $query . "<br>";
		showerror();
	}
 
	// create UNLOCK TABLE statement
	$unlock="UNLOCK TABLES";
 
	// unlock the table
	if(!(mysql_query($unlock,$dbconn)))
	{
		echo $unlock . "<br>";
		showerror();
	}
	return true;
 
}

Open in new window

0
 
LVL 39

Assisted Solution

by:Roger Baklund
Roger Baklund earned 500 total points
ID: 24083415
This function seems to insert a new record every time. Can you verify that? Is the problem that the wrong file is inserted? Is it possible that this only happen when two users insert a file "simultaneously"?

Try changing this:

move_uploaded_file($_FILES['imagefile']['tmp_name'],"latest.img");
$instr = fopen("latest.img","rb");
$image = addslashes(fread($instr,filesize("latest.img")));

...into this:

$filename = 'latest'.date('Ymd_His').'.img';
move_uploaded_file($_FILES['imagefile']['tmp_name'],$filename);
$instr = fopen($filename,"rb");
$image = addslashes(fread($instr,filesize($filename)));

This way a new file is created for each upload. To avoid these files from collecting, you can delete them after you have loaded them into the db:

fclose($instr);  # close the file
unlink($filename); # remove the file
0
 
LVL 1

Author Comment

by:TrinitySJ
ID: 24083519
I will try that and see if that works...
0
 
LVL 1

Author Comment

by:TrinitySJ
ID: 24083735
This function seems to insert a new record every time. Can you verify that?
--Verified.. this is the case.. each time putData() is called it inserts a record into the database.

Is the problem that the wrong file is inserted?
--It is correct that the wrong file is inserted. it is a duplication of the last file inserted. i need it to be that current selected file that gets inserted.

Is it possible that this only happen when two users insert a file "simultaneously"?
--this is not what is happening. at the moment i am the only one inserting images into the DB
0
 
LVL 1

Author Comment

by:TrinitySJ
ID: 24089472
the above did not work.. will work on it more.
0
 
LVL 1

Author Comment

by:TrinitySJ
ID: 24232400
Found the soulution that i needed... fixed it.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In addition to being a great web-based presentation tool, Prezi also makes it easy to save your presentation as a PDF to share with others as well. Learn how in this tutorial. Select the share icon from the top menu in your Prezi editor: Select "D…
Learn how to use the remote presentation tool in Prezi to allow you to harness the power of this cloud based presentation platform. You can show your presentation with a remote audience using this free tool.

822 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question