PHP图像不显示
我下载了一个网络教程,其中包含一个 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> </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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
乍一看,我注意到您的表单标记中没有 enctype="multipart/form-data" 。
乍一看,我注意到您尝试使用
$_REQUEST['picture']
将图像直接存储在数据库中。它不会那样工作。这实际上取决于购物车如何引用数据库中的图像。它只是一个文件名吗?在这种情况下,您需要使用类似以下内容:并将
$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.
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: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