Reputation: 436
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
Upvotes: 0
Views: 46
Reputation: 419
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);
?>
Upvotes: 0
Reputation: 594
while ($row = mysqli_fetch_array($query))
{
//access Email using $row['email_address']
// use PHP mail function here
}
Upvotes: 1