Jump to content

[SOLVED] Naming variable help


twilitegxa

Recommended Posts

I have this form that asks for user's questions and/or comments, and then I have it display on a page. What I want to do is have a link for me to respond to these questions or comments. I have the page set up, but I can't figure out how to pull the e-mail address from the displayed results. I'm assuming I need to name a variable, but how can I pull the e-mail address from the displayed information? Please help! Here is the display page code:

 

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }mysql_select_db("smrpg", $con);

// Get all the data from the "guestbook" table
$result = mysql_query("SELECT * FROM contact ORDER BY commentdate DESC") 
or die(mysql_error());  

$email = $row['email'];

echo "<h1>Sailor Moon RPG Questions/Comments - View</h1>";
echo "<table border='0'>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td width='15%'>"; 
echo "<b>Name:</b></td><td>";
echo $row['name'];
echo "</td></tr><tr><td>";
echo "<b>E-mail:</b></td><td>"; 
echo $row['email'];
        echo "</td></tr><tr><td>"; 
	echo "<b>Question/Comment:</b></td><td>";
echo $row['question'];
        echo "</td></tr><tr><td>"; 
			echo "<b>Date & Time:</b></td><td>";
        echo date("F j, Y @ g:i A T",strtotime($row['commentdate']));
echo "</td></tr>"; 
    echo "<tr><td><a href='mailto:$email'>Respond</a>?</td></tr>";
        echo "<tr><td> </td></tr>";
} 

echo "</table>";
?>

Link to comment
https://forums.phpfreaks.com/topic/125995-solved-naming-variable-help/
Share on other sites

I would do this (if I understand what you're trying to do correctly...):

<?php
$con = mysql_connect("localhost","root","") or die('Could not connect: ' . mysql_error());
mysql_select_db("smrpg", $con);

// Get all the data from the "guestbook" table
$result = mysql_query("SELECT * FROM contact ORDER BY commentdate DESC") 
or die(mysql_error());  

echo "<h1>Sailor Moon RPG Questions/Comments - View</h1>";
echo "<table border='0'>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td width='15%'>"; 
echo "<b>Name:</b></td><td>";
echo $row['name'];
echo "</td></tr><tr><td>";
echo "<b>E-mail:</b></td><td>"; 
echo $row['email'];
        echo "</td></tr><tr><td>"; 
	echo "<b>Question/Comment:</b></td><td>";
echo $row['question'];
        echo "</td></tr><tr><td>"; 
			echo "<b>Date & Time:</b></td><td>";
        echo date("F j, Y @ g:i A T",strtotime($row['commentdate']));
echo "</td></tr>"; 
    echo "<tr><td><a href='mailto:".$row['email']."'>Respond</a>?</td></tr>";
        echo "<tr><td> </td></tr>";
} 

echo "</table>";
?>

 

 

It looked like you were trying to define "$email" before there was even a result array (mysql_fetch_array() was below the variable you were trying to define). Instead of creating a variable... we can just display the data straight from the result set using concatenation.

 

Also, you don't need to use an 'if' statement during your connection string, simply using "or die()" will accomplish the same thing with less code.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.