gpt4 book ai didi

php - HTML格式的mysql数据库?

转载 作者:行者123 更新时间:2023-11-29 05:58:48 25 4
gpt4 key购买 nike

<分区>

我正在尝试让用户可以输入数据库中已有的名称。一旦他们选择“开始”,我需要它显示 contact 表中与用户输入的数据相匹配的所有结果。我错过了什么吗?当我运行 HTML 时,PHP 运行,但出现“0 个结果”。我认为这可能是我的 SELECT 语句中的内容,但找不到答案。下面的代码...

HTML 文件

<!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文件

<?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();
?>

table structure

我的 shell 代码是这样的....

<?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();
?>

但我仍然需要“dataItem”的变量。

$item1=$_POST["dataItem1"];
$item2=$_POST["dataItem2"];

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com