Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

insert image into database,php,mysql

Posted on 2009-04-06
12
Medium Priority
?
976 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This article discusses how to implement server side field validation and display customized error messages to the client.
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

722 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