PHP注意:未定义的索引:

时间:2014-02-12 21:31:41

标签: php mysql

在尝试找到更改md5密码的方法后,我认为最好生成一个新密码并通过电子邮件发送。

我有以下代码,由于某种原因,它给了我这个错误

Notice: Undefined index: email_address on line 7

这是代码

<?php
session_start(); // Start Session
session_register("session");
// This is displayed if all the fields are not filled in
$empty_fields_message = "<p>Please go back and complete all the fields in the form.</p>Click <a class=\"two\" href=\"javascript:history.go(-1)\">here</a> to go back";
// Convert to simple variables 
$email_address = $_POST['email_address'];
if (!isset($_POST['email_address'])) {
?>
<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<label for="email_address">Email:</label>
<input type="text" title="Please enter your email address" name="email_address" size="30"/>
<input type="submit" value="Submit" class="submit-button"/>
</form>

<?php
}
elseif (empty($email_address)) {
echo $empty_fields_message;
}
else {


mysql_connect("localhost", "usern", "passw") or die(mysql_error());
mysql_select_db("mydb") 
or die(mysql_error());



$email_address = mysql_real_escape_string($email_address);
$status = "OK";
$msg="";
//error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR);
if (!stristr($email_address,"@") OR !stristr($email_address,".")) {
$msg="<p>Your email address is not in the correct format.</p>Click <a class=\"two\" href=\"javascript:history.go(-1)\">here</a> to go back"; 
$status= "NOTOK";}

echo "";
if($status=="OK"){ $query="SELECT email,username FROM users WHERE users.email = '$email_address'";
$st=mysql_query($query);
$recs=mysql_num_rows($st);
$row=mysql_fetch_object($st);
$em=$row->email_address;// email is stored to a variable
if ($recs == 0) { 
echo "<p>Sorry your address is not there in our database. Please try again.</p>Click <a class=\"two\" href=\"javascript:history.go(-1)\">here</a> to go back";
exit;
}
function makeRandomPassword() { 
$salt = "abchefghjkmnpqrstuvwxyz0123456789"; 
srand((double)microtime()*1000000); 
$i = 0; 
while ($i <= 7) { 
$num = rand() % 33; 
$tmp = substr($salt, $num, 1); 
$pass = $pass . $tmp; 
$i++; 
} 
return $pass; 
} 
$random_password = makeRandomPassword(); 
$db_password = md5($random_password); 

$sql = mysql_query("UPDATE users SET password='$db_password' 
WHERE email='$email_address'"); 

$subject = "Your New Password"; 
$message = "Hello, you have chosen to reset your password. 

New Password: $random_password 

http://www.yoursite.com/loginn
Once logged in you can change your password 

Thanks! 
Site admin 

This is an automated response, please do not reply!"; 

mail($email_address, $subject, $message, "From: yoursite.com Webmaster<admin@jyoursite.com>\n 
X-Mailer: PHP/" . phpversion()); 
echo "<p>Your new password has been send! Please check your email!";
} 
else {echo "$msg";}
}
?> 

其引用的行是

$email_address = $_POST['email_address'];

我不确定为什么会发生这种情况,我是否已经看到了什么?

2 个答案:

答案 0 :(得分:4)

错误很明显。这里:

$email_address = $_POST['email_address'];

$_POST没有密钥email_address

尝试稍微更改您的代码,首先使用isset

if(isset($_POST['email_address'])) {
    $email_address = $_POST['email_address'];
    ...

请注意,如果设置发布'电子邮件地址',我已将逻辑撤消到

答案 1 :(得分:1)

在检查已设置之前,您正在分配$ _POST ['email_address']的值。 即$ email_address = $ _POST ['email_address'];在第7行。 您需要将此行移至isset($ _POST ['email_address'])if语句内。

如果您未设置$ _POST ['email_address'],则可能需要为变量$ email_address指定不同的值,如果您在代码中的其他位置使用该变量。

$email_address = "";
if (!isset($_POST['email_address'])) {
    $email_address = $_POST['email_address'];
    ...