I'm trying to make it to where the user can input a name that is already in the database. Once they select "Go", I need it to show all of the results from the contact
table that matches the inputted data by the user. Am I missing something? When I run the HTML, the PHP runs, but comes up with "0 results". I think it might be something in my SELECT statement, but could not find an answer. Code below...
HTML File
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Module 3 | Course Project</title>
</head>
<body>
<h4>Ancestry Data Query</h4>
<form action="queryMyDatabase.php" method="post">
First Name: <input name="dataItem1" type="text" size="30" maxlength="30"><br><br>
Last Name: <input name="dataItem2" type="text" size="30" maxlength="30"><br><br>
<input value="Go" type="submit">
</form>
</body>
</html>
PHP File
<?php
// Make a MySQL Connection
$servername = "localhost";
$username = "root";
$password = "bitnami admin password";
$dbname = "adventureworks";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$item1=$_POST["dataItem1"];
$item2=$_POST["dataItem2"];
$sql="SELECT * FROM `contact` WHERE FirstName = `$item1` AND LastName = `$item2`";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "First Name " . $row["FirstName"] . "<br>" . "Last Name " . $row["LastName"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
The shell code I had was this....
<?php
// Make a MySQL Connection
$servername = "localhost";
$username = "root";
$password = "yourApplicationPassword";
$dbname = "desiredDatabase";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$stmt = $conn->prepare("SELECT * FROM desiredTable WHERE desiredColumn1 = ? AND desiredColumn2 = ?");
$stmt->bind_param('ss', $_POST["dataItem1"] , $_POST["dataItem2"]);
$stmt->execute();
$result = $stmt->get_result();
$row_count= $result->num_rows;
echo "Query Results</br>-----------------------------<br><br>";
if($row_count>0){
while($row = mysqli_fetch_array($result))
{
echo $row['desiredColumn1']." ".$row['desiredColumn2']." ".$row['desiredColumn3']."-".$row['desiredColumn4']."-".$row['desiredColumn5']."</br>";
}
} else {
echo "0 results";
}
echo "<br>-----------------------------<br>";
$stmt->close();
$conn->close();
?>
But I still needed the variables for the "dataItem".
$item1=$_POST["dataItem1"];
$item2=$_POST["dataItem2"];