简体   繁体   English

使用php从数据库上传和显示图片库

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

I'm working on a shopping cart, where I want to display products with their respective images stored in the db.我正在处理购物车,我想在其中显示产品,其各自的图像存储在数据库中。 But my php code is not working as expected.但是我的 php 代码没有按预期工作。 I want to display images stored in more than 1 records.我想显示存储在 1 个以上记录中的图像。

These are my html and php pages:这些是我的 html 和 php 页面:

index.html索引.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>

upload_img.php上传_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 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 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>

You should have a double quotes in form action and name.您应该在表单操作和名称中使用双引号。

login 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>

dbconnect.php数据库连接文件

<?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";    
?>

login.php登录.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