尝试从两个表中显示数据(图像和文本)时出错

时间:2010-11-22 17:28:17

标签: php mysql

我正在尝试显示来自我的数据库的图像,但我在浏览器上看到此消息“item_Details.php?itemID = 1”或者根本没有。

我到处看,看看我做错了什么,但找不到任何东西。

以下是表格:

'CREATE TABLE `images` (
`imagesID` int(5) NOT NULL AUTO_INCREMENT,
`itemID` int(5) NOT NULL,
`categoryID` int(5) NOT NULL,
`name` varchar(30) NOT NULL,
`size` int(11) NOT NULL,
`type` varchar(30) NOT NULL,
`pix` blob NOT NULL,
PRIMARY KEY (`imagesID`),
KEY `categoryID` (`categoryID`),
KEY `images_ibfk_1` (`itemID`),
CONSTRAINT `images_ibfk_1` FOREIGN KEY (`itemID`) REFERENCES `items` (`itemID`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `images_ibfk_2` FOREIGN KEY (`categoryID`) REFERENCES `categories` 
(`categoryID`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1'

'CREATE TABLE `items` (
`itemID` int(5) NOT NULL AUTO_INCREMENT,
`categoryID` int(5) NOT NULL,
`itemName` char(25) NOT NULL,
`item_Description` varchar(255) DEFAULT NULL,
`price` char(10) DEFAULT NULL,
`contactName` varchar(50) DEFAULT NULL,
`phone` char(15) DEFAULT NULL,
`email` varchar(50) DEFAULT NULL,
`submitDate` date NOT NULL,
`expireDate` date NOT NULL,
`website` char(25) DEFAULT NULL,
PRIMARY KEY (`itemID`) USING BTREE,
KEY `submitDate` (`submitDate`),
KEY `categoryID` (`categoryID`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1'

以下是我用于在新窗口中显示特定项目详细信息的代码:

classified.php

//Table 3 Electrical Items
$query3 = "SELECT * FROM {$table} WHERE categoryID='3'";
$result = mysql_query($query3);
//let's get the number of rows in our result so we can use it in a for loop
$numofrows = mysql_num_rows($result);
if($numofrows == 0){
echo "<p class=spect>Electrical Items:</p><p>No entries yet</p> \n";
}
else{
echo"<table id=mytable cellspacing=0 summary=Items from almacen database>
<caption>Electricals: </caption>";
echo "<TR bgcolor=\"lightblue\"><TH>Item Name</TH><TH>Price</TH><TH>Posted On</TH></TR>\n"; 
for($i = 0; $i < $numofrows; $i++) {
$row = mysql_fetch_array($result); //get a row from our result set
$id=$row['itemID'];
if($i % 2) { //this means if there is a remainder
echo "<TR bgcolor=\"#F5FAFA\">\n";
} else { //if there isn't a remainder we will do the else
echo "<TR bgcolor=\"#FFFFFF\">\n";
} 
echo "<TD><a href='item_Details.php?itemID=<?php=$id; ?>' target = '_blank'>{$row['itemName']}</a></TD>
<TD>&pound;".number_format ($row['price'],2)."</TD><TD>".$row['submitDate']."</TD>\n";
echo "</TR>\n";
} } 
echo "</table><br/>";

item_details.php

$query1 = "SELECT * FROM images, items WHERE (images.itemID='$id' && items.itemID='$id')";
$result = mysql_query($query1);

while ($row = mysql_fetch_array ($result))
{
header("Content-type: ".$row['type']."");
//header("Content-Disposition: attachment; filename=".$row['name']."");
echo"<table>";
echo"<tr>";
echo"<td colspan='2'>
  <h2 style='font-size: large;'> ".$row['itemName']."</h2></td>\n";
echo "</tr>";
echo "<tr>";
echo"<td><img border='0' align='right' src='{$row['pix']}' alt='item Image' style='margin-left: 5px;'></td>\n";
echo"<tr>";
echo"<td colspan='2'>
   <div>     
         <b>Price: </b>&pound;" .number_format ($row['price'],2). "<br />
         <b>Posted On: </b>" .$row['submitDate']. "<br />\n
         </div><br>
<b>Description: </b><p>" .$row['item_Description']. "<p><br />
 <div ;='' style=clear: both;>
  <h4 style='font-size: medium;'>Contact details:</h4>
<b>Name: </b>" .$row['contactName']. "<br />
<b>E-mail: </b>".$row['email']." <br /><br />  
</div></td>\n";
echo"</tr>\n";
}
echo "</table><br/>";

如果我写了classified.php <a href=item_Details.php?itemID=$id>,浏览器会在Item_details.php页面上显示“item_Details.php?itemID = 1”。

现在如果写<a href='item_Details.php? itemID=<?php=$id;?>浏览器在Item_details.php页面上根本没有显示任何内容。

无论哪种方式,我都看不到照片。

感谢您的帮助。 埃尔南

1 个答案:

答案 0 :(得分:0)

这是无效的:<?php=$id;?>

您可以使用<?=$id;?><?php echo $id; ?>

我更喜欢后者,因为它更明确。

<强>除了
我不确定您是否正在处理遗留应用程序,或者这是否是您正在编写的新代码。如果是新代码,那么你应该花一两天时间(或者更长时间,如果你能负担得起的话),研究一些更好的编码实践。我将从逻辑分离开始。试试这个: http://www.paragoncorporation.com/ArticleDetail.aspx?ArticleID=21

<强>更新
实际上,请阅读此内容,最好:https://stackoverflow.com/questions/62617/whats-the-best-way-to-separate-php-code-and-html