简体   繁体   中英

Select Information from MySql table then store this into a variable

I have a MySql table, and I want to take a users email from it where a user id is given to pin point the information. I want to then get this email, store it in a variable and then use it to email someone inside the script.

Heres my code so far but it does not display:

<

?php

$appid = $_POST["appid"];
$option1 = $_POST['radio'];

$servername = "localhost";
$username = "mcxjb";
$password = "password";
$dbname = "members1";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "UPDATE users SET user_level='$option1' WHERE userid=$appid";

if (mysqli_query($conn, $sql)) {
    echo "Success!";
} else {
    echo "Error " . mysqli_error($conn);
}


$emailsql = "SELECT email_address FROM users WHERE userid=$appid";

$query = mysql_query($emailsql);


mysqli_close($conn);



?>

The script allows the user to approve or deny a person application. It then updates that users user level to approved or denied. I then want to email the user by extracting their email by using their userid that was given and take it from the SQL table !

Thanks, Mark

while ($row = mysqli_fetch_array($query))
        {    

          //access Email using $row['email_address']
            // use PHP mail function here

        }

For your update query you need to change this :

if(mysqli_query($conn, $sql))..

Whit this :

if(mysqli_query($sql))...

And to access the result of your select query you need to do it like this :

while ($row = mysqli_fetch_array($query))
        {    

          //access Email using $row['email_address']
            // use PHP mail function here

        }

So the code will be :

<?php

$appid = $_POST["appid"];
$option1 = $_POST['radio'];

$servername = "localhost";
$username = "mcxjb";
$password = "password";
$dbname = "members1";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "UPDATE users SET user_level='$option1' WHERE userid=$appid";

if (mysqli_query($conn, $sql)) {
    echo "Success!";
} else {
    echo "Error " . mysqli_error($conn);
}


$emailsql = "SELECT email_address FROM users WHERE userid=$appid";

$query = mysqli_query($conn, $emailsql);

while ($row = mysqli_fetch_array($query))
            {    

              //access Email using $row['email_address']
                // use PHP mail function here

            }

mysqli_close($conn);



?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM