Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

insert image into database,php,mysql

Posted on 2009-04-06
12
Medium Priority
?
977 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 1500 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 1500 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Add visuals to your Prezi to keep your audience engaged. Learn how to embed images, edit them, and more in this video micro tutorial. Select "Insert" from the top menu in your Prezi editor: Select "Image": A toolbar will pop in from the right …
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…
Suggested Courses

916 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