繁体   English   中英

PHP图像未显示

[英]PHP image not showing up

我下载了一个Web教程,其中包含一个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>

产品添加页面如下:

  <?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>
<? } ?>

对于很长的帖子,很抱歉,任何建议都将有所帮助。

谢谢

乍一看,我注意到您的表单标记中没有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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM