我试着在我的页面上创建一个文本输入,以便能够接收我的数据库上的用户名并返回他自己的另一个信息,我该怎么办呢?例如输入姓名并返回他的电话号码。我根本不知道如何开始。感谢
答案 0 :(得分:0)
<?php
if(isset($_POST['username']){
try{
$dbh = new PDO("mysql:dbname=Amor;host=127.0.0.1",
"Javier", "your_password");
$sql = "SELECT name, phone, address FROM people_table
WHERE username=:username";
$res = $dbh->prepare($sql);
$res->bindParam('username', $_POST['username'], PDO::PARAM_STRING);
$res->exec();
$row = $res->fetchAll(PDO::FETCH_ASSOC);
print_r($row);
//Sorry mate I'm feeling lazy. You can take over from here
}
?>
**//html code here**
<form action="" method="GET>
<input type="text" name="username">
<input type="submit" value="Search">
</form>
答案 1 :(得分:0)
这是工作代码。但首先要学习基础知识。
<强>的index.html 强>
<!DOCTYPE html>
<html>
<head>
<title>Index page</title>
</head>
<body>
<form method="POST" action="retrieve.php">
Enter your name : <input type="text" name='username'><br><br>
<input type="submit" name="submit">
</form>
</body>
</html>
<强> retrieve.php 强>
<?php
//Database connection
$connection = mysqli_connect("localhost","username","password","database_name");
if (isset($_POST['submit'])) {
$uname = $_POST['username'];
// sql query and user is the table name
$query = "SELECT * FROM `user` WHERE user_name='$uname'";
// query to execute
$result = mysqli_query($connection,$query);
if (!$result) {
echo "fails";
}
while($row = mysqli_fetch_array($result)) {
$telephone_no = $row['phoneno'];
$country_name = $row['country'];
echo "Your results for name - " . $uname;
echo "<br><br>";
echo "Telelephone is : " . $telephone_no;
echo "<br>";
echo "Country name is : " . $country_name;
}
}
?>
数据库结构:(用户表)
要测试代码,请在PHPMyadmin
插入选项卡中插入人员详细信息。(保留id,因为它设置为自动增量)。