PHP图像不显示

发布于 2024-12-16 01:32:36 字数 3249 浏览 3 评论 0原文

我下载了一个网络教程,其中包含一个 php 购物车和一个 mysql 数据库,其中已经内置了表格。我设法将整个购物车集成到我自己的网站中。我的网站中有一个表单,管理员可以上传产品,这些产品会发送到数据库,并显示在网站上供用户查看。我遇到的问题是图像。在本教程中,图像已手动输入到 mysql 数据库中。

我的表单如下:

    <form action="productsadded.php" method="post">

<table border="0">
  <tr>
    <td><label for="name">Product Name:</label>
</td>
    <td><span id="sprytextfield1">
      <input type="text" name="name" id="name" />
      <span class="textfieldRequiredMsg">Enter Products Name</span></span></td>
  </tr>
  <tr>
    <td><label for="description">Description: </label>
</td>
    <td><span id="sprytextarea1">
      <textarea name="description" id="description" cols="45" rows="5"></textarea>
      <span class="textareaRequiredMsg">Enter Products Description</span></span></td>
  </tr>
  <tr>
    <td>    <label for="price">Price: </label>
</td>
    <td><span id="sprytextfield2">
    <input type="text" name="price" id="price" />
    <span class="textfieldRequiredMsg">Enter Price</span><span class="textfieldInvalidFormatMsg">Numbers Only</span></span></td>
  </tr>
  <tr>
    <td><label for="price">Image: </label></td>
    <td><input name="picture" id="picture" accept="image/jpeg" type="file" /></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input name="" type="submit" value="Add Product" /></td>
  </tr>
</table>

productsadded 页面如下:

  <?php


$name = $_REQUEST['name'];
$description = $_REQUEST['description'];
$price = $_REQUEST['price'];
$picture = $_REQUEST['picture'];

$con = mysql_connect("localhost","*****","*****");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db('jahedhus_91', $con);

$sql="INSERT INTO products (name, description, price, picture) VALUES ('$name', '$description', '$price', '$picture')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }

mysql_close($con)
?>

我遇到的问题是,一旦将产品添加到数据库中,该产品的图像就不会显示在网站上。名称、详细信息和除图片外,均显示产品价格。

显示产品信息的sql语句如下:

<table border="0" cellpadding="2px" width="600px">
    <?
        $result=mysql_query("select * from products");
        while($row=mysql_fetch_array($result)){

    ?>
<tr>
    <td><img src=<?=$row['picture']?> /></td>
    <td>    <b><a href="products.php?product_id=<?=$row['serial']?>"><?=$row['name']?></a></b><br />
            <?=$row['description']?><br />
            Price:<big style="color:green">
                £<?=$row['price']?></big><br /><br />
            <input type="button" value="Add to Cart" onclick="addtocart(<?=$row['serial']?>)" />
        </td>
    </tr>
<tr><td colspan="2"><hr size="1" /></td>
<? } ?>

很抱歉这篇文章很长,任何建议都会有所帮助。

谢谢

i downloaded a tutorial of the web which contained a php shopping cart and an mysql database with the tables already built in. I managed to intergrate that whole shopping cart into my own website. I have a form in my website whereby the admin can upload products which get sent to the database and also show up on the website for the users to view. the problem i am having is with the images. in the tutorial, the images have been manually entered in the mysql database.

I have a form as follows:

    <form action="productsadded.php" method="post">

<table border="0">
  <tr>
    <td><label for="name">Product Name:</label>
</td>
    <td><span id="sprytextfield1">
      <input type="text" name="name" id="name" />
      <span class="textfieldRequiredMsg">Enter Products Name</span></span></td>
  </tr>
  <tr>
    <td><label for="description">Description: </label>
</td>
    <td><span id="sprytextarea1">
      <textarea name="description" id="description" cols="45" rows="5"></textarea>
      <span class="textareaRequiredMsg">Enter Products Description</span></span></td>
  </tr>
  <tr>
    <td>    <label for="price">Price: </label>
</td>
    <td><span id="sprytextfield2">
    <input type="text" name="price" id="price" />
    <span class="textfieldRequiredMsg">Enter Price</span><span class="textfieldInvalidFormatMsg">Numbers Only</span></span></td>
  </tr>
  <tr>
    <td><label for="price">Image: </label></td>
    <td><input name="picture" id="picture" accept="image/jpeg" type="file" /></td>
  </tr>
  <tr>
    <td> </td>
    <td><input name="" type="submit" value="Add Product" /></td>
  </tr>
</table>

the productsadded page is as follows:

  <?php


$name = $_REQUEST['name'];
$description = $_REQUEST['description'];
$price = $_REQUEST['price'];
$picture = $_REQUEST['picture'];

$con = mysql_connect("localhost","*****","*****");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db('jahedhus_91', $con);

$sql="INSERT INTO products (name, description, price, picture) VALUES ('$name', '$description', '$price', '$picture')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }

mysql_close($con)
?>

the problem i am having is that once a product is added to the database, the image of the product is not shown on the website. the name, details & price of the products are being shown except the image.

the sql statement for product info to be shown are as follows:

<table border="0" cellpadding="2px" width="600px">
    <?
        $result=mysql_query("select * from products");
        while($row=mysql_fetch_array($result)){

    ?>
<tr>
    <td><img src=<?=$row['picture']?> /></td>
    <td>    <b><a href="products.php?product_id=<?=$row['serial']?>"><?=$row['name']?></a></b><br />
            <?=$row['description']?><br />
            Price:<big style="color:green">
                £<?=$row['price']?></big><br /><br />
            <input type="button" value="Add to Cart" onclick="addtocart(<?=$row['serial']?>)" />
        </td>
    </tr>
<tr><td colspan="2"><hr size="1" /></td>
<? } ?>

sorry for the really long post, any suggesstions will be helpful.

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

-黛色若梦 2024-12-23 01:32:36

乍一看,我注意到您的表单标记中没有 enctype="multipart/form-data" 。

<form action="productsadded.php" method="post" enctype="multipart/form-data">

乍一看,我注意到您尝试使用 $_REQUEST['picture'] 将图像直接存储在数据库中。它不会那样工作。这实际上取决于购物车如何引用数据库中的图像。它只是一个文件名吗?在这种情况下,您需要使用类似以下内容:

$target_path = 'path/to/images/'.basename( $_FILES['picture']['name']); 
move_uploaded_file($_FILES['picture']['tmp_name'], $target_path);

并将 $target_path 作为图片保存在数据库中。这会将上传的文件移动到产品图像目录中。

我建议您从这个购物车中单独学习一些关于 php 文件上传的教程,并首先掌握它的窍门。

编辑:刚刚注意到您想要将图像存储在数据库中的评论。这更复杂,但这看起来是一个很好的教程: http://www.phpriot .com/articles/storing-images-in-mysql

At first glance, I notice you don't have enctype="multipart/form-data" in your form tag.

<form action="productsadded.php" method="post" enctype="multipart/form-data">

On second glance, I notice that you're attempting to store the image directly in the database with $_REQUEST['picture']. It won't work like that. It really depends on how the shopping cart is referencing the images in the database. Is it simply a filename? in which case, you'll need to use something like:

$target_path = 'path/to/images/'.basename( $_FILES['picture']['name']); 
move_uploaded_file($_FILES['picture']['tmp_name'], $target_path);

and save the $target_path in the database as the picture. This will move the uploaded file into the product images directory.

I'd recommend you follow some tutorials on php file upload separately from this shopping cart and get the hang of it first.

Edit: Just noticed the comment that you want to store the image in the database. This is more complicated but this looks like a good tutorial: http://www.phpriot.com/articles/storing-images-in-mysql

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文