duanmu6752 2014-05-10 20:27 采纳率: 0%
浏览 99

如何将图片的目录/ url放入数据库?

I know this question has been asked many times before, but i still can't understand it even if i do many times. I need someone to explain things to me but i have no help here. I hope you guys can help me with my coding.

I try to do an upload page, it's working just fine but i just don't understand how people can grab the image location directory/url and save it into a database. I can show the image but the image is being shown from the folder. I mean i think the directory of the image after it being uploaded into a uploads/ folder isn't being save into the database.

I have a database name images and the table images. And the columns are id, name, and ext.

Here is my coding for index.php:

<html>
<head>
</head>

<body>

<!--Upload form-->
<form action="" method="POST" enctype="multipart/form-data">

<input type="file" name="file"/><br/>
<input type="submit" name="submit" value="Upload">

</form>

<?php
//include the connect.php file

include ('connect.php');

if (isset($_POST['submit'])) 
     {


    //location, where the image will be saved
    $loc= "uploads/";

    if ($_FILES["file"]["type"] == "image/png" 
         || $_FILES["file"]["type"] == "image/jpeg" 
         || $_FILES["file"]["type"] == "image/jpg" 
         || $_FILES["file"]["type"] == "image/gif") 
        {
            //script

            $file=explode(".", $_FILES["file"]["name"]);


            mysql_query("INSERT INTO images VALUES ('','".$file[0]."', '".$file[1]."')");
            $id = mysql_insert_id();

            $newname = "$id.$file[1]";

            $path = "$loc$newname.ShowUploadKatSini.php";

            move_uploaded_file($_FILES["file"]["tmp_name"], $path);
            echo"Your image has been uploaded successfully. Visit this link to see ur image <a href='$path'>Click HERE</a>.";


        } 
        else 
        {
            echo"Invalid file!";

        }



     }

?>

</body>
</html>

And here is for connect.php:

$connect = mysql_connect("localhost","root");

//select database
mysql_select_db("images",$connect);

?>

My real question is how to grab the picture's directory/url into a database and make the pictures that i upload shown in certain page, let say the page name dress.html

  • 写回答

1条回答 默认 最新

  • douyueju2912 2014-05-10 20:50
    关注

    You INSERT query is not correct

    This

    mysql_query("INSERT INTO images VALUES ('','".$file[0]."', '".$file[1]."')");
    

    Should be

    mysql_query("INSERT INTO images (`name`, `ext`) VALUES ('".$file[0]."', '".$file[1]."')");
    
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)