I have email contact in PHP
and I wanted to add part where it should check if there is actual order ID written in <input>
in my table, otherwise, it sends email.
EDIT: added prepared statement $stmt->execute([ ':order' => $order ]);
<?php
if (isset($_POST['submit'])) {
$subject = $_POST['subject'];
$message = $_POST['message'];
$order = $_POST['orderId'];
$mailTo = "mail@mail.com";
if ($order != "") {
$db = new PDO('mysql:host=localhost;dbname=dbname;charset=utf8', 'username', 'password');
$order = $_POST['orderId'];
$stmt = $db->query("SELECT * FROM Orders WHERE OrderID= :order ");
$stmt->execute([ ':order' => $order ]);
if (!$row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo 'No such ID';
}
else {
$txt .= "Query Received!\n\nOrder ID: ".$order."\n\nMessage context: \n\n".$message;
mail($mailTo, $subject, $txt);
}
}
else {
$txt .= "Bug report received!\n\n"."Message context: \n\n".$message;
mail($mailTo, $subject, $txt);
}
}
?>
And my HTML:
<center><form class="query-form" method="post">
<input style="width: 300px;" class="orderId" type="text" name="orderId" placeholder="Order ID. Leave blank if reporting a bug">
<br>
<input required style="width: 300px;" type="text" name="subject" placeholder="Subject">
<br>
<textarea required name="message" placeholder="Query text" style="width: 300px;" maxlength = "700"></textarea>
<br>
<input type="submit" name="submit" placeholder="Send Query">
</form></center>
When I fill up orderId
input and on purpose type characters that aren't in my table ("test"), it still sends an email ( while it should echo that there is no such order ID provided in input):
Query Received!
Order ID:
Message context:
Test
But when I leave orderId
empty, PHP works just fine and gives me second message, as wanted.
Can you please tell me why it's just going through that code?