将src img添加到mysql表中

时间:2016-08-12 15:29:37

标签: php html mysql image

我现在正在尝试将图像上传到文件夹(图像)并将文件路径显示到表格列中以引用上传的图像。 (示例)上传的图像:(report.jpg)将图像存储到文件夹(图像)中,现在创建表格中的图像的URL< img src =“https://www.****.com/images/report.jpg”alt =“”/> unfortunatley我想出了只存储文件名在表格中(report.jpg)

<?php

if ($_POST["action"] == "Load") {
    $folder = "images/";
    move_uploaded_file($_FILES["filep"]["tmp_name"], "$folder".$_FILES["filep"]["name"]);

    echo "<p align=center>File ".$_FILES["filep"]["name"]."loaded...";

    $result = mysql_connect("**", "**", "**") or die ("Could not save image name Error: " . mysql_error());
    mysql_select_db("testing") or die("Could not select database");
    mysql_query("INSERT into links (hyper_links) VALUES('".$_FILES['filep']['name']."')");

    if ($result) { 
        echo "Image name saved into database";
    } else {
        //Gives and error if its not
        echo "Sorry, there was a problem uploading your file.";
    }
}
?>
<form action=sample.php method=post enctype="multipart/form-data">
    <table border="0" cellspacing="0" align=center cellpadding="3" bordercolor="#cccccc">
        <tr>
            <td>File:</td>
            <td><input type="file" name="filep" size=45/></td>
        </tr>
        <tr>
            <td colspan=2>
                <p align=center>
                    <input type=submit name=action value="Load"/>
                </p>
            </td>
        </tr>
    </table>
</form>

1 个答案:

答案 0 :(得分:1)

尝试以下代码。

注意: - 在代码中使用您的网站名称更改http://www.yourwebsite.com

<?php

if ($_POST["action"] == "Load") {
    $folder = "images/";
    move_uploaded_file($_FILES["filep"]["tmp_name"], "$folder".$_FILES["filep"]["name"]);

    echo "<p align=center>File ".$_FILES["filep"]["name"]."loaded...";

    $result = mysql_connect("**", "**", "**") or die ("Could not save image name Error: " . mysql_error());
    mysql_select_db("testing") or die("Could not select database");
    mysql_query("INSERT into links (hyper_links) VALUES('https://www.yourwebsite.com/images/".$_FILES['filep']['name']."')");

    if ($result) { 
        echo "Image name saved into database";
    } else {
        //Gives and error if its not
        echo "Sorry, there was a problem uploading your file.";
    }
}
?>

这会在您的表格中添加 http://www.yourwebsite.com/images/report.jpg