如何从 $_POST var 类似于用户名的数据库中检索电子邮件

How to retrieve an email from database where $_POST var is like username

这是代码

    <?php 
if(isset($_POST['uuser'])){
       $gemail = mysql_result("SELECT `email` FROM `user` WHERE `username` LIKE '" . ($_POST['uuser']) . "';");

    $to ="$gemail"; // this is user's Email address 

    $from = "admin@mysite.com"; // this is the sender's Email address
    $first_name = $_POST['uuser'];
    $last_name = $_POST['uuser'];
    $subject = "Form submission";

    $message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];

    $headers = "From:" . $from;
    mail($to,$subject,$message,$headers);
    }
?>

问题是 $gemail 没有从数据库中获取电子邮件。

您需要使用 mysql_query 或 mysqli_query。所以下面将是你的代码。

$con=mysqli_connect("localhost","my_user","my_password","my_db");
$user = mysqli_real_escape_string($con,$_POST['uuser']);
$gemail = mysqli_query($con,"SELECT `email` FROM `user` WHERE `username` LIKE '".$user."'");