Reputation: 979
I have a website where the user must already exist in the database to be able to proceed to the next page. I have made a query that checks their first name, last name, and email address against those stored in the DB and I can get it to print the result if they match, but I can't get it to return a true or something I can work with in order to perform an action. I need to send an email to the user then redirect them to another page, could you help out with this too?
Here's my code
<?php
include("dbconnect.php");
$firstname=$_POST["firstname"];
$lastname=$_POST["lastname"];
$email=$_POST["email"];
$connect = $conn;
if (!$connect) {
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
// Prepare the statement
$stid = oci_parse($connect, "SELECT * FROM USERS WHERE firstname = '$firstname' AND lastname = '$lastname' AND email = '$email'");
if (!$stid) {
$e = oci_error($connect);
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
// Perform the logic of the query
$r = oci_execute($stid);
if (!$r) {
$e = oci_error($stid);
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
// Fetch the results of the query
print "<table border='1'>\n";
while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
print "<tr>\n";
foreach ($row as $item) {
print " <td>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : " ") . "</td>\n";
}
print "</tr>\n";
}
print "</table>\n";
oci_free_statement($stid);
oci_close($connect);
?>
If you could please help me to run an action if true (send an email and redirect) and display an error if the user does not exist in the database. Thanks - I'm very new to PHP
Upvotes: 0
Views: 82
Reputation: 1294
You can solve your problem with multiple solutions. I will give you two recommendations. The first one is to use a SQL select that counts the returning rows of an executed query. Try this:
SELECT * FROM YOUR_TABLE WHERE SURNAME LIKE '%surname%' AND NAME LIKE '%name%' AND EMAIL like '%email%'
Execute this and after the execute command insert this:
echo oci_num_rows($stid) . " people with this data.
\n";
If it is 1 you have somebody with this data in your database.
You can also use JavaScript. If you want I can explain that also. EDIT: here you go with pure javascript:
A short example of comparasion in JS
http://jsfiddle.net/marcusrommel/tcm4ak1p/
Upvotes: 1