I need to validate that the information typed into a text box matches exactly wh
ID: 3706294 • Letter: I
Question
I need to validate that the information typed into a text box matches exactly whats in the database table that is written. Example being if I type Chicago the information is correctly passed to the proper page, if i type chicagon then I need it to post a message to re enter the information again.
this is the code I have written so far....
<?php
require_once('includes/dbconnection.php');
try{
if($_GET['destination'] == "") {
echo "Please Enter a Destination";
}
else{
$dest = $_GET['destination'];
$sql = "SELECT miles, gallons, date, purpose FROM trips WHERE destination = ?";
$st = $conn->prepare($sql);
$st->execute(array($dest));
echo "<table class='table table-bordered table-striped'>";
echo "<thead><tr><th>Miles</th><th>Gallons</th><th>Date</th><th>Purpose</th></tr></thead>";
echo "<tbody>";
$rows = $st;
foreach ($rows as $row) {
echo '<tr>';
echo "<td>" . $row['miles'] . "</td>";
echo "<td>" . $row['gallons'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['purpose'] . "</td>";
echo '</tr>';
}
echo "</tbody>";
}
}
catch (Exception $ex) {
echo "<p><strong>Please Re-Enter Your Destination!!!</strong></p>";
}
?>
Explanation / Answer
Following is the Php code:
<?php
require_once('includes/dbconnection.php');
try{
if($_GET['destination'] == "") {
echo "Please Enter a Destination";
}
else{
$dest = $_GET['destination'];
$sql = "SELECT miles, gallons, date, purpose FROM trips WHERE destination = ?";
$st = $conn->bind_param("s",$sql);//s = string
$cnt = $st->execute();
if($cnt>0){
echo "<table class='table table-bordered table-striped'>";
echo "<thead><tr><th>Miles</th><th>Gallons</th><th>Date</th><th>Purpose</th></tr></thead>";
echo "<tbody>";
$rows = $st;
foreach ($rows as $row) {
echo '<tr>';
echo "<td>" . $row['miles'] . "</td>";
echo "<td>" . $row['gallons'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['purpose'] . "</td>";
echo '</tr>';
}
echo "</tbody>";
}
else{
echo "<p><strong>Please Re-Enter Your Destination!!!</strong></p>";
}
}
}
catch (Exception $ex) {
echo $ex;
}
?>