Solved

insert image into database,php,mysql

Posted on 2009-04-06
12
970 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.
Learn the basics of inputting and editing your text components in Prezi. We will cover how to set styles, position, and group your text components. In your Prezi editor, click anywhere on the canvas to add text: A flashing cursor informs you that yo…

910 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now