duanluanhui8348 2016-09-16 17:58
浏览 32
已采纳

如何创建一个后端系统,用于在上传图像之前为图像添加超链接和标题

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>
  • 写回答

1条回答 默认 最新

  • douejuan9162 2016-09-16 18:27
    关注

    Here is your code with the problems fixed:

    <!DOCTYPE html>
    
    <html>
    <head>
        <title>Upload form</title>
    </head>
    <?php
    // THESE TWO VARIABLES SHOULD NOT BE HERE     ◄■■■■■■■■■■
    // BECAUSE WE DON'T KNOW IF THE EXIST. THEY   ◄■■■■■■■■■■
    // BELONG TO NEXT IF.                         ◄■■■■■■■■■■
    // $add_link = $_GET['add_link'];
    // $add_caption = $_GET['add_caption'];
    
    if(isset($_POST['upload_img'])) {
        // THE FORM IS "POST", NOT "GET".          ◄■■■■■■■■■■
        $add_link = $_POST['add_link'];            ◄■■■■■■■■■■ POST, NOT GET
        $add_caption = $_POST['add_caption'];      ◄■■■■■■■■■■ POST, NOT GET
        $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");
        }
     // THE DATABASE SECTION MUST BE HERE, INSIDE ◄■■■■■■■■■■■■■■■■■■
     // THE IF, NOT AT THE BOTTOM.                ◄■■■■■■■■■■■■■■■■■■
        $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';
        }
    
        // THESE VARIABLES WERE ALREADY DECLARED.   ◄■■■■■■■■■■■
        // $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>
    
        <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);
            }
        }
        ?>
    </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题
  • ¥15 有没有人能解决下这个问题吗,本人不会编程
  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测