簡體   English   中英

使用php從數據庫上傳和顯示圖片庫

[英]Upload and display image gallery from database using php

我正在處理購物車,我想在其中顯示產品,其各自的圖像存儲在數據庫中。 但是我的 php 代碼沒有按預期工作。 我想顯示存儲在 1 個以上記錄中的圖像。

這些是我的 html 和 php 頁面:

索引.html

<html>
    <body>
        <form action="upload_img.php" method="post" enctype="multipart/form-data">
            <center>Select Image to upload: 
                <br><input type="file" name="image">    
                <input type="submit" value="upload">
            </center>   
        </form>
        <form action="display_img2.php">
            <input type="submit">
        </form>
    </body>
</html>

上傳_img.php

<?php

mysql_connect("localhost", "", "") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());

//file properties
$file = $_FILES['image']['tmp_name'];
if (!isset($file)) {

} else {
    $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
    $image_name = addslashes($_FILES['image']['name']);
    $image_size = getimagesize($_FILES['image']['tmp_name']);

    if ($image_size == FALSE) {
        echo "not an image";
    } else {
        if (!$insert = mysql_query("insert into photos values ( ('','$image','$image_name')")) {
            echo "problem uploading image";
        } else {
            echo "image uploaded successfully!!";
            /* $lastid=mysql_insert_id();
              echo "<img src=display_img.php?id=$lastid>"; */
        }
    }
}
?>

display_img2.php

<?php

$con = mysqli_connect("localhost", "", "", "test");
$sql = "select * from photos";
$result = mysqli_query($con, $sql);

$cnt = mysqli_num_rows($result);
echo $cnt;

while ($cnt) {
    echo "<img src=display_img.php?id=$cnt>";
    $cnt--;
}
?>

display_img.php

<?php

mysql_connect("localhost", "", "") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());

$id = addslashes($_REQUEST['id']);
$image = mysql_query("select * from photos where id=$id");
$image = mysql_fetch_assoc($image);
$image = $image['image'];

header("Content-Type: image/jpeg");

echo $image;
?>
<form action="login.php" method="POST" name="image_upload" enctype="multipart/form-data" >
</form>

您應該在表單操作和名稱中使用雙引號。

登錄表格:

<form action=login.php method="POST" name=image_upload enctype="multipart/form-data" >
    username
    <input type=text name=username><br>
    Select image to upload:<input type="file" name="image" id="fileToUpload">       
    <input type="submit">   
</form>

數據庫連接文件

<?php
    $servername="localhost";
    $username="root";
    // $password="";

    // create connection    
    $conn=mysqli_connect($servername,$username,"");
    mysqli_select_db($conn,"test");
    mysqli_query($conn,$sql);

    $sql = "ALTER TABLE `user_image` CHANGE `username` `username` VARCHAR(20) NOT NULL";
    mysqli_query($conn,$sql);

    $sql = "ALTER TABLE `user_image` CHANGE `userimage` `userimage` VARCHAR(40) NOT NULL";
    mysqli_query($conn,$sql);

    $sql = "ALTER TABLE `user_image` CHANGE `userid` `userid` INT(11) NOT NULL AUTO_INCREMENT";
    mysqli_query($conn,$sql);

    echo "<br>done";    
?>

登錄.php

<?php
    include 'dbconnect.php';
    //simple upload
    $file_name = $_FILES['image']['name'];
    $file_tmp = $_FILES['image']['tmp_name'];
    $image=$_FILES["file"]["name"];

    $image_size=getimagesize($_FILES['image']['tmp_name']);
    //echo $image_size;
    if($image_size==FALSE)
    {
        echo ("<SCRIPT LANGUAGE='JavaScript'>window.alert('fill data');</SCRIPT>");
        //readfile("login.php");`enter code here`
    }
    else
    {
        move_uploaded_file($file_tmp,"upload/".$file_name);
        //upload in mysql database

        $file = $_FILES['image']['name'];
        $image_name = addslashes($_FILES['image']['name']);
        $x=$_POST["username"];

        $sql="INSERT INTO  `test`.`user_image` (username, userimage) VALUES('$x','$image_name')";

        if(!$insert=mysqli_query($conn,$sql))
            echo "problem uploading image";
        else
        {
            $img="upload/".$file_name;
            echo '<img src= "'.$img.'" height=200 width=150>';
        }
    }
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM