I want to select a row from my users’ table 'tbl_login' in my database with the following php script:
<?php
include 'config.inc.php';
$conn = mysqli_connect($servername, $username, $password, $dbname);
$email = $_POST["email"];
$sql = "SELECT email, firstname, lastname, location
FROM tbl_login
WHERE email = '$email'";
$res = mysqli_query($conn,$sql);
$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result,
array('email'=>$row[0],
'firstname'=>$row[1],
'lastname'=>$row[2],
'location'=>$row[3]
));
}
if ($conn->query($sql) === TRUE) {
echo "Selected row successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
echo json_encode(array("result"=>$result));
mysqli_close($conn);
?>
My problem is that I’m not sure how to write this part:
$email = $_POST['email']; // <--- This.
$sql = "SELECT email, firstname, lastname, location
FROM tbl_login
WHERE email = '$email'"; // <--- And this.
If I’m trying without the $_POST and with just a hardcoded string like 'Luke', it works, so I figured my problem is on the $_POST.
My others scripts are done this way and they all work, if you could enlighten me on why this is not working, I would be extremely grateful, as I've been working on this for a few hours already. Thanks a lot in advance, Charles.