Reputation: 21
Need Help: I am using a simple PHP code to upload photos into remote database. But.. Everytime, two copies of one photo is saved int the DB. Can anyone tell me whats I am doing wrong? PHP Code:
<?PHP
$uploadDir = 'image_folder/';
$uploadDir = 'image_folder/';
if(isset($_POST['Submit'])) //info saving in the variables
{
$fileName = $_FILES['Photo']['name'];
$tmpName = $_FILES['Photo']['tmp_name'];
$fileSize = $_FILES['Photo']['size'];
$fileType = $_FILES['Photo']['type'];
$filePath = $uploadDir . $fileName;
$result = move_uploaded_file($tmpName, $filePath); //moving the photo in the destination
if (!$result) {
echo "Error uploading file";
exit;
}
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
echo "".$filePath."";
$query = "INSERT INTO picture (image) VALUES ('$filePath')";
if (mysql_query($query))
{echo "Inserted";}
mysql_query($query) or die('Error loading file!');
}?>
Upvotes: 2
Views: 188
Reputation: 263
You are using mysql_query
two times. Try this:
if (mysql_query($query)) {
echo "Inserted";
} else {
die('Error loading file!');
}
Upvotes: 1
Reputation: 3310
You are executing mysql_query twice. Modify your if to:
if (mysql_query($query)) {
echo "Inserted";
} else {
die('Error loading file!')
}
And, remove the following line:
mysql_query($query) or die('Error loading file!');
Note: Make sure that you read the warning box in https://www.php.net/mysql_query. mysql_* functions are deprecated.
Upvotes: 0
Reputation: 2347
You are doing mysql_query($query)
two times, first in IF{}
and right after it. :D
ps. mysql_*
functions are depricated, use PDO
or mysqli
Upvotes: 1
Reputation: 1190
if (mysql_query($query))
{echo "Inserted";}
mysql_query($query) or die('Error loading file!');
you are calling mysql_query($query)
twice
Upvotes: 4