Forum Moderators: coopster

Message Too Old, No Replies

Setting Variables from an select statement

Want to set a variable from a query result

         

dottomm

9:31 pm on Apr 8, 2008 (gmt 0)

10+ Year Member



I just experienced a severe php epiphany that I really have no idea what I'm doing.

What I am trying to do, is retrieve a record and set the result as a variable that I can send in an email.

Here is my code:


include 'common/useful_stuff.php'; //<--db connection script
if (!db_connect())
die();

$teampassword=""; // <--this is the variable I'm trying to set
$mailmssg ="Your password is ".$teampassword;

if ($doit == "yes"){

if ( $email == ""){
$errmsg = "You must enter an email address!";
}
else {
// go see if they are in the db
$em = mysql_real_escape_string($email);
$p = mysql_real_escape_string($pw);

$res = mysql_query("select teampassword from teams where teamemail='{$email}'");

$row = mysql_fetch_assoc($res);
if (!$row){
$errmsg = "No account with that email address!";
}
else {
mail("$em", "Lost Password",$mailmssg,"FROM:$em");

}
}

?>
<html>
<head>
<link href="css/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php
include 'common/header.php';

echo "
<div id='content'>
<h1>Retrieve Team Password </h1>
<p><font color='red'><b>{$errmsg}&nbsp;</b></font></p>
<p>Please enter the email account your team registered with.</p>
<p>&nbsp;</p>

<p><form action='{$_SERVER['PHP_SELF']}' method='post'>
<input type='hidden' name='doit' value='yes'>
email<br><input type='text' name='email' value='{$email}'>

<br><input type='submit' value='Retrieve Password'>
</form>
<p><font color='#ccc'><b>{$msg}</b></font></p>
<div id ='content_r'></div>
</div>

";

?>

</body>
</html>

thanks in advance to anyone who can help.

MattAU

1:22 am on Apr 9, 2008 (gmt 0)

10+ Year Member



Looks like you just need to get/use teampassword from the $row array and clean things up a bit... Remember you only want to use mysql_real_escape'd values when using them with a database:

if (isset($_POST['doit']) && $_POST['doit'] == "yes"){

if (!isset($_POST['email']) ¦¦ $_POST['email'] == ""){
$errmsg = "You must enter an email address!";
}
else {
$email = trim($_POST['email']);
// go see if they are in the db
$em = mysql_real_escape_string($email);
$p = mysql_real_escape_string($pw); // not sure where this comes from or what it's for...

$res = mysql_query("select teampassword from teams where teamemail=$em"); // This was $email not $em... Remember to use your escaped strings for queries! They've already got the 'quotes' around them

if (!mysql_num_rows($res)){ // cleans this up a bit, reduces chance of php error.
$errmsg = "No account with that email address!";
}
else {
$row = mysql_fetch_assoc($res);
$mailmssg ="Your password is " . $row['teampassword'];
mail($email, "Lost Password",$mailmssg,"FROM:".$email); // Remember not to use escaped strings for anything but the database.
}
}