I want to update my image in my database. But when I update it in my form the value in my database goes empty for the image.
Here is the PHP script I am using to update the image and other values. Good to notice is that only my image value goes to empty the rest of the values in my database are updating.
<?php
if(isset($_POST["submit"]) && isset($_GET['id']) && $_GET['id'] == "type") {
include('config.php');
$imgFile = $_FILES['user_image']['name'];
$tmp_dir = $_FILES['user_image']['tmp_name'];
$imgSize = $_FILES['user_image']['size'];
if($imgFile)
{
$upload_dir = 'user_images/'; // upload directory
$imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension
$valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
$userpic = rand(1000,1000000).".".$imgExt;
if(in_array($imgExt, $valid_extensions))
{
if($imgSize < 5000000)
{
unlink($upload_dir.$edit_row['opzoekImage']);
move_uploaded_file($tmp_dir,$upload_dir.$userpic);
}
else
{
$errMSG = "Sorry, your file is too large it should be less then 5MB";
}
}
else
{
$errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
}
}
else
{
// if no image selected the old image remain as it is.
$userpic = $edit_row['opzoekImage']; // old image from database
}
$sql2 = "UPDATE tblOpzoek SET opzoekName='".$_POST["typenaam"]."', opzoekValue='".$_POST["typewaarde"]."', opzoekImage='".$userpic."' WHERE opzoekId=".$_POST["typeid"]."";
if($db->query($sql2) === TRUE) {
header('Location: ' . $_SERVER['HTTP_REFERER']);
} else {
echo "<script type= 'text/javascript'>alert('Error: " . $sql2 . "<br>" . $db->error."');</script>";
}
}
?>
And here is the config.php code:
<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'root');
define('DB_DATABASE', 'offerteBVDO');
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?>
Update:
The SQL query is now outputting this:
UPDATE tblOpzoek SET opzoekName='Greenline Veranda', opzoekValue='greenline_veranda', opzoekImage='12443.jpg' WHERE opzoekId='1'
But in the database the image does not have the value as in the statement before.
When i insert the SQL query in my phpmyadmin it is working, why is it not working in my PHP?