In your comment, you are asking how to load and save data in mysql. So here it is:
To get the file, you must have a script in your html, like this:
<html> <body> <form action="upload_file.php" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file"><br> <input type="submit" name="submit" value="Submit"> </form> </body> </html>
Now, in POST, your PHP file should look like this, but please note that you need to check if the file exists in your POST:
if ($_FILES["file"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br>"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br>"; echo "Type: " . $_FILES["file"]["type"] . "<br>"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>"; echo "Stored in: " . $_FILES["file"]["tmp_name"]; }
Since the "Saved in:" part is just a temporary path, you should go to your "real" image path using move_uploaded_file () . Let's say the real / standard path for your images is:
$image_dir= '/images/';
You just need to move the file using this:
move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $image_dir. $_FILES['uploaded_file']['name']);
And your full path to the image will be
$image = $final_save_dir . $_FILES['uploaded_file']['name'];
There are several ways to save the path to your database:
1st: you only need to save the filename and combine the image path in PHP with $_SERVER['DOCUMENT_ROOT']
and your default image path, for example:
$sql="insert into employee_detail( emp_image, employee_name, employee_address, employee_contact, employee_designation, employee_salary) values( '$image', '$_POST[employee_name]', '$_POST[employee_address]', '$_POST[employee_contact]', '$_POST[employee_designation]','$_POST[employee_salary]')";
2nd: Is saving the full path as follows:
$sql="insert into employee_detail( emp_image, employee_name, employee_address, employee_contact, employee_designation, employee_salary) values( '".$_SERVER['DOCUMENT_ROOT']."\\images\\".$image."', '$_POST[employee_name]', '$_POST[employee_address]', '$_POST[employee_contact]', '$_POST[employee_designation]','$_POST[employee_salary]')";
I recommend this approach, in which you enter a partial path (without the root directory) so that later you do not have problems when deploying it:
$sql="insert into employee_detail( emp_image, employee_name, employee_address, employee_contact, employee_designation, employee_salary) values( 'images\\".$image."', '$_POST[employee_name]', '$_POST[employee_address]', '$_POST[employee_contact]', '$_POST[employee_designation]','$_POST[employee_salary]')";
And make sure that images are successfully loaded into this dir / path image by default .
UPDATE
I also recommend using mysqli_*
or PDO
and using the prepare()
method / function to prevent sql injection.