PHP图像没有显示
我下载了一个包含一个php购物车和一个mysql数据库的内置表格的网络教程。我设法将整个购物车整合到我自己的网站中。我在我的网站上有一个表单,管理员可以上传发送到数据库的产品,并在网站上显示供用户查看。我遇到的问题是图像。在教程中,图像已经手动输入到mysql数据库中。PHP图像没有显示
我有一个表格如下:
<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>
<? } ?>
对不起,真的很长的帖子,任何suggesstions会有所帮助。
感谢
乍一看,我注意到你没有在你的表单标签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
嗨,感谢您的回复,但是我在哪里放置了这一行代码:$ target_path ='path/to/images /'。basename($ _FILES ['picture'] ['name']); move_uploaded_file($ _ FILES ['picture'] ['tmp_name'],$ target_path); – Jahed
您不能只复制代码,您需要弄清楚产品图像当前的存储位置以及它们的url如何存储在数据库中。但它可以代替'$ picture = $ _REQUEST ['picture'];'。而不是将'$ picture'插入到数据库中,将url插入到图像中。 – bencoder
我很迷失atm。我有一个错误提示:警告:move_uploaded_file(images/13092011152.jpg)[function。移动上传文件]:无法打开流:没有这样的文件或目录在78线/home/jahedhus/public_html/music/admin/productsadded.php 警告:move_uploaded_file()[function.move-uploaded-file ]:无法将/home/jahedhus/public_html/music/admin/productsadded.php上的'/ tmp/phpiiTE22'移动到'78 images/13092011152.jpg'的78行 – Jahed
你想要保存图片(数据)的内容到数据库中,或者你是否只想保存一个名字。你的代码意图与你想要做的冲突有关。 – jprofitt
我想将图像保存到数据库中 – Jahed
任何想法jprofitt? – Jahed