Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Email not picking up data from mysql table

Status
Not open for further replies.

BadChough

Programmer
Dec 20, 2007
137
GB
Trying to patch a php page together from elements from other pages without really understanding what's going on is probably frowned upon, but you've got to start somewhere!
The code below is supposed to include some pieces of text from msql table and include them in an email. The email is being sent, but I clearly don't know how to get it to include the database elements because those bits are missing in the email. I can confirm that the correct record is being accessed by the page.
Can someone give me the vital clue please? I promise I will study php one day soon!

Code:
<?php require_once('../../Connections/tormented3.php'); ?>
<?php
$colname_rstSix = "1";
if (isset($_GET['organ'])) {
  $colname_rstSix = (get_magic_quotes_gpc()) ? $_GET['organ'] : addslashes($_GET['organ']);
}
mysql_select_db($database_tormented3, $tormented3);
$query_rstSix = sprintf("SELECT * FROM chk_sixmonth WHERE ck_organisation = '%s'", $colname_rstSix);
$rstSix = mysql_query($query_rstSix, $tormented3) or die(mysql_error());
$row_rstSix = mysql_fetch_assoc($rstSix);
$totalRows_rstSix = mysql_num_rows($rstSix);

if(isset($_POST['send'])){ // 'submit' is the name of your submit button
$error = '';

 
 if($error == ''){ // if no error, process the form 
 // place here your mail script 
$start = "There is an update from\n";// Set up the email message header
$tit = $row_rstSix['ck_organisation'];
$first = $row_rstSix['ck_firstname'];
$sur = $row_rstSix['ck_lastname'];

$message .= "$start $tit Contact name: $first $sur\n";

if(mail("feedback@tormented.org.uk", "Update Notification.",
$message, "From ")){
header('location:[URL unfurl="true"]http://www.torbayhelp.org.uk/up_content/org_changechoice.php');//[/URL] redirect to page if email has been successfully sent
exit;
}else{
$error == '<p style="color:red;">An error occured, your email could not be sent. Please try
again</p>';
}
}
}
?>

Many thanks,
Steve
 
can't see anything much wrong. i suspect something is going wrong with your query. try this as a debug measure

Code:
<?php require_once('../../Connections/tormented3.php'); ?>
<?php
$colname_rstSix = "1";

if (isset($_GET['organ'])) {
	$colname_rstSix = (get_magic_quotes_gpc()) ? $_GET['organ'] : addslashes($_GET['organ']);
}

mysql_select_db($database_tormented3, $tormented3);

$query_rstSix = sprintf("SELECT * FROM chk_sixmonth WHERE ck_organisation = '%s'", $colname_rstSix);
$rstSix = mysql_query($query_rstSix, $tormented3) or die(mysql_error());
$row_rstSix = mysql_fetch_assoc($rstSix);
$totalRows_rstSix = mysql_num_rows($rstSix);

if(isset($_POST['send'])){ // 'submit' is the name of your submit button

	$error = '';

 
	if($error == ''){ // if no error, process the form
		// place here your mail script
		$start = "There is an update from\n";// Set up the email message header
		$tit = $row_rstSix['ck_organisation'];
		$first = $row_rstSix['ck_firstname'];
		$sur = $row_rstSix['ck_lastname'];

		$message .= "$start $tit Contact name: $first $sur\n Row output was " . print_r($row_rstSix, true); //added for debug

		if ( mail( "feedback@tormented.org.uk", 
					"Update Notification.",
					$message, "From: feedback@tormented.org.uk \r\n")){

			header('location:[URL unfurl="true"]http://www.torbayhelp.org.uk/up_content/org_changechoice.php');//[/URL] redirect to page if email has been successfully sent
			exit;
		}else{
			$error == '<p style="color:red;">An error occured, your email could not be sent. Please try again</p>';
		}
	}
}
?>
 

Thanks jpadie,

Email now reads
"There is an update from
Contact name:
Row output was"

No other change.
I know the page is in touch with the correct record as it behaves correctly to the instruction:
Code:
<?php echo $row_rstSix['ck_firstname']; ?>
elsewhere on the page.

What next?
 
well. simply by the time you get to the email, the $row variable is empty. you would need to show us the whole page for us to be able to help you debug it.
 
Here's the whole page, with your debug elements included, jpadie.
Code:
<?php require_once('../../Connections/tormented3.php'); ?>
<?php
$colname_rstSix = "1";
if (isset($_GET['organ'])) {
  $colname_rstSix = (get_magic_quotes_gpc()) ? $_GET['organ'] : addslashes($_GET['organ']);
}
mysql_select_db($database_tormented3, $tormented3);
$query_rstSix = sprintf("SELECT * FROM chk_sixmonth WHERE ck_organisation = '%s'", $colname_rstSix);
$rstSix = mysql_query($query_rstSix, $tormented3) or die(mysql_error());
$row_rstSix = mysql_fetch_assoc($rstSix);
$totalRows_rstSix = mysql_num_rows($rstSix);

if(isset($_POST['send'])){ // 'submit' is the name of your submit button

    if($error == ''){ // if no error, process the form
        // place here your mail script
        $start = "There is an update from\n";// Set up the email message header
        $tit = $row_rstSix['ck_organisation'];
        $first = $row_rstSix['ck_firstname'];
        $sur = $row_rstSix['ck_lastname'];

        $message .= "$start $tit Contact name: $first $sur\n Row output was " . print_r($row_rstSix, true); //added for debug

        if ( mail( "feedback@tormented.org.uk",
                    "Update Notification.",
                    $message, "From: feedback@tormented.org.uk \r\n")){

            header('location:[URL unfurl="true"]http://www.torbayhelp.org.uk/up_content/org_changechoice.php');//[/URL] redirect to page if email has been successfully sent
            exit;
        }else{
            $error == '<p style="color:red;">An error occured, your email could not be sent. Please try again</p>';
        }
    }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>Feedback  Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="../mentalhealth.css" rel="stylesheet" type="text/css" />
</head>
<body>

<form action="<?php echo $PHP_SELF; ?>" method="post">
<?php echo $error; ?>
			<!-- Call the processing routine -->

  <table cellpadding="1">
    <tr>
      <td colspan="5" class="cellcolour">&nbsp;</td>
    </tr>
    <tr>
      <td colspan="2" valign="top" class="cellcolour"><div align="right"><?php echo $row_rstSix['ck_organisation']; ?></div></td> // 
      <td width="229"><?php echo $row_rstSix['ck_firstname']; ?></td>
    </tr>
    <tr>
      <td colspan="5" valign="top"></td>
    </tr>
    <tr>
      <td colspan="5"><p>Please use the buttons below to inform me if you have made changes to your data or not.</p>      </td>
    </tr>
    <tr>
      <td width="223" align="right" class="cellcolour">        
        <input name="send" type="submit" value="I made changes " />
	  </td>
      <td colspan="3" valign="top"><span class="cellcolour"></span></td>
    </tr>
  </table>
</form>
</body>
</html>
<?php
mysql_free_result($rstSix);
?>

Thanks again for your help.
 
so what happens if the $_GET['organ'] is not set? your code currently does not handle that situation.

and as the form is intended to be self-processing i would expect that the you never get that variable and thus the query always returns nil rows as that is how many rows there are in the db with organisation as null.

if you want to add a query variable that php can identify as $_GET you will need to change your form action as follows
Code:
<form action="<?php echo $PHP_SELF; ?>[red]?organ=something[/red]" method="post">


 
Thanks for the feedback about the coding.
I've managed to get the thing to work with
Code:
<form action="<?php echo $PHP_SELF; ?>?organ=<?php echo  $row_rstSix['ck_organisation']?>" method="post">
Thanks again,
Steve
 
i would have used $colname_rstsix, personally.

but i still have to ask - what happens if a user arrives at this page and no organ variable has been set? so far as I can see the page will just fail gracelessly.
 
Fine. I have substituted $colname_rstsix and am happy to receive the same email from your more-correct code.
For the last hour I have tried to be creative with solutions for the "What if no organ variable?" problem you set me, but to no avail. Basically I am out of my depth with the code I'm employing and need to get the PHP tutorial out and work my way through it, but I have learnt a lot in the past with trial and error, and advice from good people like yourself.
I guess the solution is along the line of
Code:
if('organ' == ''){echo $error;}
but clearly I'm not getting it right.
Your guidance would be appreciated.
 
ok. what I would do is presume a default organisation code.

but on a closer examination of your code, in particular this bit:
Code:
$colname_rstSix = "1";
if (isset($_GET['organ'])) {
  $colname_rstSix = (get_magic_quotes_gpc()) ? $_GET['organ'] : addslashes($_GET['organ']);
}
I see that you are already creating a default value for $colname_rstSix. so I withdraw my question but would advise instead replacing isset with !empty
Code:
if (!empty($_GET['organ'])) ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top