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

时间:2013-11-05 09:56:13

标签: php mysql sql

我正在尝试根据数据列表从数据库中列出一条记录。我有一个脚本列出数据库中的所有记录,并在html表格中显示记录。在表格中,有一行'Campaigns'这是一个链接,当点击时只显示一行的记录..想法是只列出有电子邮件的行,否则显示一条消息说没有可用的电子邮件

这是脚本

//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';

但是我在运行脚本时遇到错误'未定义索引:电子邮件'..请帮助

4 个答案:

答案 0 :(得分:1)

尝试使用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;
   }
}

答案 1 :(得分:0)

我错了,或者我是否正确地看到你有你的其他信息{echo'该记录没有电子邮件';应用于if语句,说if($ email!='')?

- 更新:你应该使用while()移动else语句并使其成为IF语句,这样你就知道哪些记录有哪些,哪些没有有效/空的mailaddress。

如果是这样,那一定是因为没有指定$ _REQUEST。出于多种原因,另请参阅Among $_REQUEST, $_GET and $_POST which one is the fastest?以了解为何或为何不使用$ _REQUEST而是使用$ _GET / $ _ POST。

答案 2 :(得分:0)

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>";

答案 3 :(得分:0)

谢谢你的回答poeple

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

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

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

像魅力一样!

相关问题