Search code examples
phpsqlphpmailervar

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


Here is the code

    <?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 = "[email protected]"; // 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);
    }
?>

The problem is with $gemail its not getting email from database.


Solution

  • You need to use mysql_query or mysqli_query. So below will be your code.

    $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."'");