0

I'm trying to list a record from the database based on its id..i have one script that lists all records from the database and displays the records in an html table.in the table, there's one row 'Campaigns' that is a link which when clicked displays only the record of one row..the idea is to list only the row that has an email else display a message saying no email is available

Here's the script

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

but i get the error 'Undefined index: email' on running the script..please help

user11
  • 35
  • 1
  • 10

4 Answers4

1

Try to use mysql_fetch_object: example:

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

in your code , for example:

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

after your query:

while($row = mysql_fetch_object($query))
{
   if(empty($row->email))
   {
       echo "email is empty!"
       continue;
   }
}
mortymacs
  • 3,456
  • 3
  • 27
  • 53
0

Am I wrong or do I see it correctly that you have your else { echo 'That record does not have an Email'; } applied to the if-statement saying if ($email != '')?

-- update: you should move the else statement withing the while() and make it an IF statement, so you know which records have and which do not have a valid/empty mailaddress.

If so, it must be because the $_REQUEST is not specified. For multiple reasons, also see Among $_REQUEST, $_GET and $_POST which one is the fastest? to understand why or why not to use $_REQUEST but to use $_GET/$_POST instead.

Community
  • 1
  • 1
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>";
uvais
  • 416
  • 2
  • 6
0

Thankyou for your answers poeple

i've instead opted for another alternative..the obvious and simplest one actually..

i've included the following in my SELECT statement

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

Works like a charm!

user11
  • 35
  • 1
  • 10