gpt4 book ai didi

php - 通过电子邮件向用户发送忘记的用户名

转载 作者:行者123 更新时间:2023-11-29 13:17:24 25 4
gpt4 key购买 nike

我有一些 php 代码,我希望用户输入他们的电子邮件地址,对照数据库进行检查,然后将与该电子邮件地址关联的用户名发送给用户。基本上是“忘记用户名”功能。

到目前为止,我有以下代码,它将发送电子邮件,但在电子邮件内显示您的用户名是:就是这样。由于某种原因,$username 变量没有被传递到电子邮件。我对 PHP 还很陌生,因此我们将不胜感激。

<?php

if(isset($_POST['submit'])){

$email = $_POST['email'];

if($email==''){
echo "<script>alert('Please enter email address!')</script>";
exit();
}

$sql=mysql_query("select user_name from users where user_email='$email'");
if(mysql_num_rows($sql)>=1)
{
//echo "<script>alert('Username $user_name already exist in our database, please try another one!')</script>";
$username = $row[0];


$subject = 'Forgotten Username';
$message = 'Your username is: '.$username.'';
$header = 'Header';

if($_POST){
mail($email, $subject, $message, $header);
$feedback = 'Thanks for contacting us! We will be in contact soon!';
}
}
else {
echo "<script>alert('Email doesn't exist')</script>";
}
}
?>

HTML

<form method='post' action='forgotusername.php'>
<table width='400' border='5' align='center'>

<tr>
<td align='center'>Enter your email address</td>
<td><input type="text" name="email" style="width: 200px;" /></td>
</tr>

</table>
<input type='submit' name='submit' value='Post' />
</form>

最佳答案

要回答您的问题,您需要先获取该行,然后才能使用 mysql_fetch_row() 访问它。 :

<?php
$row = mysql_fetch_row($sql);
$username = $row[0];

但更重要的是要注意,这是一种非常古老的访问 mysql 数据库的方法。

参见http://www.php.net/manual/en/function.mysql-fetch-array.php对于缺少的功能,它还会为您提供更新、更安全的数据库访问方法的建议。

还有一个方法mysql_real_escape_string()您可以使用它来转义电子邮件变量,但有一些改进的方法可用。

关于php - 通过电子邮件向用户发送忘记的用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21294881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com