根据条件在 html 表中列出一行


Listing a row in html table based on a condition

我正在尝试根据其 id 列出数据库中的记录。我有一个脚本列出数据库中的所有记录,并在表格 table.in 以 HTML 显示记录,有一行"广告系列"是一个链接,单击时仅显示一行的记录。这个想法是只列出有电子邮件的行,否则显示一条消息,说没有可用的电子邮件

这是脚本

//database connection details
$id =$_REQUEST['id'];
$email =$_REQUEST['email'];
if($email != "")
{
$query = mysql_query("SELECT * FROM $tbl_name WHERE id='$id'");
echo "<table border = 1 cellpadding = 5> <tr>   <th> Name </th> <th> Address </th> <th> City </th> <th> State </th> <th> Postal Code </th> <th> Voice </th> <th> Email </th> <th> Status </th> </tr>"; 
while($row = mysql_fetch_array($query)) 
{
  echo "<tr>";
  echo "<td> $row[1] </td>";
  echo "<td> $row[2] </td>";
  echo "<td> $row[3] </td>";
  echo "<td> $row[4] </td>";
  echo "<td> $row[5] </td>";
  echo "<td> $row[6] </td>";
  echo "<td> $row[7] </td>";
  echo "<td> $row[8] </td>";
  echo "</tr>";
}
echo "</table>";
}
else
    echo 'That record does not have an Email';

但是我在运行脚本时收到错误"未定义的索引:电子邮件"。請幫助

嘗試

使用mysql_fetch_object:例:

while($row = mysql_fetch_object($query))
{
    echo $row->email;
}

在您的代码中,例如:

if(isset($_REQUEST['email']) && !empty($_REQUEST['email']))
    $email = $_REQUEST['email'];
else
    echo "enter email address";

查询后:

while($row = mysql_fetch_object($query))
{
   if(empty($row->email))
   {
       echo "email is empty!"
       continue;
   }
}

是我错了还是我看错了你有你的 else { echo '该记录没有电子邮件'; } 应用于 if 语句说如果 ($email != '')?

-- 更新:您应该将 else 语句与 while() 一起移动并使其成为 IF 语句,以便您知道哪些记录具有有效/空的邮箱地址,哪些记录没有有效/空的邮箱地址。

如果是这样,那一定是因为未指定 $_REQUEST。出于多种原因,另请参阅在 $_REQUEST、$_GET 和 $_POST 中,哪一个最快?了解为什么或为什么不使用 _REQUEST 美元,而是使用 _GET/_POST 美元。

i think it will be helpfull, try this 
$sqlquery = mysql_query("SELECT * FROM $tbl_name");
echo "<table border = 1 cellpadding = 5> <tr>   <th> Name </th> <th> Address </th> <th>         City </th> <th> State </th> <th> Postal Code </th> <th> Voice </th> <th> Email </th> <th> Status </th> </tr>"; 
foreach($sqlquery as $query) {
    if (!empty($query->email)) {
      echo "<tr>";
      echo "<td>'.$query-><corresponding_column_name>.'</td>";
      echo "<td>'.$query-><corresponding_column_name>.'</td>";
      echo "<td>'.$query-><corresponding_column_name>.'</td>";
      echo "<td>'.$query-><corresponding_column_name>.'</td>";
      .....
      ...
     echo "</tr>";`enter code here`
    } else {
    echo "<tr>'No email is available</tr>";
      }
}
echo "</table>";

谢谢你的回答 人们

相反,我选择了另一种选择..实际上显而易见和最简单的一个..

我在我的 SELECT 语句中包含以下内容

$sql = "SELECT * FROM $tbl_name WHERE email<>''";

像魅力一样工作!