每当我运行此语句时,所显示的链接都只指向commerces.php,即使id=4.


Whenever i run this statement,the displayed link only goes to the testimonials.php and even if the id !=4

此块用于获取数据库表行以显示其内容,如果表包含任何数据,则产品计数将计数

<?php
    $sql = mysql_query("SELECT * FROM products1 ORDER BY date_added DESC LIMIT 10");
    $productCount = mysql_num_rows($sql); // count the output amount
    if ($productCount > 0) {
    while($row = mysql_fetch_array($sql)){
     echo  '<table width="90%" border="0" cellspacing="0" cellpadding="6">';
     echo '<tr>';
     echo '<td width="17%" valign="top"><img style="border:#666 1px solid;" src="../inventory_images/'.$row['id'].'.png" alt="'.$row['name'].'" width="90" height="110" border="1"/></td>';
     echo '<td width="83%" valign="top">'.$row['name'].'<br/>P'.$row['price'].'<br />';

这是链接,如果正在播放的产品的id为4,则应转到commerces.php,如果它包含id为5,则应转至register.php,如果它含有id为6,则应移至.php,依此类推

     if ($id=4){
     echo '<a href="testimonials.php?">View Product Details</a></td>';}
     elseif ($id=5){
     echo '<a href="register.php?">View Product Details</a></td>';}
     elseif ($id=6){
     echo '<a href="a.php?">View Product Details</a></td>';}
     elseif ($id=7){
     echo '<a href="b.php?">View Product Details</a></td>';}
     elseif ($id=8){
     echo '<a href="c.php?">View Product Details</a></td>';}
     elseif ($id=9){
     echo '<a href="d.php?">View Product Details</a></td>';}
     elseif ($id=10){
     echo '<a href="e.php?">View Product Details</a></td>';}
     elseif ($id=11){
     echo '<a href="f.php?">View Product Details</a></td>';}
     elseif ($id=12){
     echo '<a href="g.php?">View Product Details</a></td>';}
     elseif ($id=13){
     echo '<a href="h.php?">View Product Details</a></td>';}
     elseif ($id=14){
     echo '<a href="i.php?">View Product Details</a></td>';}
     echo '</tr>';
     echo '</table>';
      }

如果它有0个数据,产品计数应该说"我们还没有在商店里列出产品"

   }else
      {
        echo "We have no product listed in our store yet";
      }
      mysql_close(); 
 ?>

这很痛苦。像这样的重复条件,差别很小,最好用一个查找表来完成:

$map = array(
   4 => 'testimonials',
   5 => 'register',
   6 => 'a'
   etc..
)
if (isset($map[$id])) {
    echo '<a href="' . $map$[$id] . '.php?">View Product Details</a></td>';
}

正如所有评论中所提到的,你正在if()测试中进行分配:

if ($id = 4) {
        ^---

在PHP中,赋值的结果是被赋值的值,因此$id被赋值为4,然后继续求值,就好像语句是一样

if (4) {

由于4是一个非假值,因此它将始终计算为true,而else的其余原因将被忽略。