gpt4 book ai didi

php - 加载存储在数据库中的图像时出错

转载 作者:行者123 更新时间:2023-11-30 22:45:29 26 4
gpt4 key购买 nike

由于isset(),想要存储和检索数据库中的数据,包括图像,能够存储图像但无法显示它错误。

以下是我用过的文件。

  1. HTML 格式

    <html>
    <title>reg</title>
    <style type="text/css">
    body {
    background-color: rgb(200,200,200);
    color: white;
    padding: 20px;
    font-family: Arial, Verdana, sans-serif;}
    h4 {
    background-color: DarkCyan;
    padding: inherit;}
    h3 {
    background-color: #ee3e80;
    padding: inherit;}
    p {
    background-color: white;
    color: rgb(100,100,90);
    padding: inherit;}
    </style>

    <form method="POST" action="login_back.php" enctype="multipart/form-data"></br>
    &nbsp<font color="DarkCyan"> Choose a user name:</font> <input type="text" name="username">
    </br></br>
    &nbsp<font color="DarkCyan"> First name:</font> <input type="text" name="firstname"/>
    </br></br>
    &nbsp<font color="DarkCyan"> Last name:</font><input type="text" name="lastname"/>
    </br></br>
    &nbsp<font color="DarkCyan"> File: <input type="file" name="image"></font>
    </br></br>
    <input type="submit" value="Save and Proceed">
    </form>
    </html>
  2. 用于在数据库中存储和检索数据的 PHP 文件

     <?php
    #echo "<body style='background-color:rgb(200,200,200)'>";
    session_start();
    if( isset($_POST['username']) && isset($_FILES['image']) )
    {
    $_SESSION['username']=$_POST['username'];
    $_SESSION['firstname']=$_POST['firstname'];
    $lastname=$_POST['lastname'];
    $file=$_FILES['image']['tmp_name'];
    $image_size=getimagesize($_FILES['image']['tmp_name']);
    if(!isset($file))
    echo"please select an image";
    else
    {
    $image_name=$_FILES['image']['name']; //grabing image name
    $image_size=getimagesize($_FILES['image']['tmp_name']); //getting image size
    }
    echo "</br>";


    #connection to db
    mysql_connect("localhost","root","")or die(mysql_error());
    mysql_select_db("wordgraphic")or die(mysql_error());



    #checking the available username
    $query = mysql_query("SELECT * FROM userdata WHERE username = '" . $_SESSION['username'] . "'" );
    $ans=mysql_num_rows($query);
    if ($ans > 0)
    {
    echo "Username already in use please try another.";

    }

    else if($image_size==FALSE)
    {
    echo"That's not an image.";

    }

    else
    {
    #Insert data into mysql

    #1.Inserting user name & image into db
    $sql="INSERT INTO userdata(username, firstname, lastname, image)VALUES('" . $_SESSION['username'] . "', '" . $_SESSION['firstname'] . "', '$lastname','$image')";
    $result1=mysql_query($sql);

    if($result1)
    {
    echo "</br>";
    echo "Registration successful";
    echo "</br>";

    //displaying image
    $lastid=mysql_insert_id();//get the id of the last record
    echo "uploaded image is :";
    echo "<img src='get.php?id=".$lastid."'>"; > this command has some mistake

    }#if insertion into db successful
    else
    {
    echo "Problem in database operation";
    }
    }# else block of unique username n img
    }#end of isset
    ?>
  3. 得到。 php,输出数据的附加文件,此文件已在<img src=get.php?id=$lastid>中使用和 $_REQUEST在这个文件中使用。控制流未通过 isset()制造问题。

    $image=mysql_query("SELECT * FROM userdata WHERE id=$mid") or die("Invalid query: " . mysql_error());
    $image=mysql_fetch_assoc($image);
    $image=$image['image'];
    header("Content-type: image/jpeg");
    echo $image;
    }
    else
    {
    echo"error";
    }
    ?>

最佳答案

这是我第一次在论坛上发帖,准确的问题应该是,此代码可能有什么问题:

$lastid=mysql_insert_id();//get the id of the last record
echo "<img src='get.php?id=".$lastid."'>";

get.php 在哪里:

<?php
#connection to db
mysql_connect("localhost","root","")or die(mysql_error());
mysql_select_db("wordgraphic")or die(mysql_error());
if(isset($_REQUEST['id']) ) > this block of code is not runninng
{
$mid=(int)($_REQUEST['id']);

$image=mysql_query("SELECT * FROM userdata WHERE id=$mid") or die("Invalid query: " . mysql_error());
$image=mysql_fetch_assoc($image);
$image=$image['image'];
header("Content-type: image/jpeg");
echo $image;
}
else
{
echo"error";
}
?>

关于php - 加载存储在数据库中的图像时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29732701/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com