I am creating a back end form system in Php and mysql that will allow me to add captions and hyperlinks on images before their uploaded to a web page. The caption, hyperlink, and a reference to the image will be store in a mysql database. The form will only be used by me. How can I add hyperlinks and captions to images using a form so they can be upload to a web page? This is the code:
<!DOCTYPE html>
<html>
<head>
<title>Upload form</title>
</head>
<?php
$add_link = $_GET['add_link'];
$add_caption = $_GET['add_caption'];
if(isset($_POST['upload_img'])) {
$file_name = $_FILES['image'] ['name'];
$file_type = $_FILES['image'] ['type'];
$file_size = $_FILES['image'] ['size'];
$file_tmp_name = $_FILES['image'] ['tmp_name'];
if($file_name) {
move_uploaded_file($file_tmp_name,"img/$file_name");
}
}
?>
<body>
<form action="" method="post" enctype="multipart/form-data">
<label>upload Image</label><br><br>
<input type="file" name="image"><br><br>
<label>Add link / URL to image:</label>
<input type="text" name="add_link" maxlength="30" size="30"><br><br>
<label>Add text caption:</label>
<input type="text" name="add_caption" maxlength="30" size="30"><br><br>
<input type="submit" value="Upload Image" name="upload_img">
</form> <br>
<?php
$folder = "img/";
if(is_dir($folder)) {
if($handle = opendir($folder)) {
while(($file=readdir($handle)) != false) {
if($file ==='.' || $file ==='..') continue;
echo'<a href="$add_link"> <img src="img/".$file."" width="150" height="150" alt=""><br>';
echo $add_caption . "</a>";
}
closedir($handle);
}
}
?>
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if(!mysqli_select_db($conn,'image_display'))
{
echo 'Database not selected';
}
$add_link = $_GET['add_link'];
$add_caption = $_GET['add_caption'];
$sql = "INSERT INTO table1(image1,name,imagelink,caption,video) VALUES ('$add_link', '$add_caption', '$file_name')";
?>
</body>
</html>