Php Code To Search Data In Mysql Database Table And Dispaly Results In Inputs Using MySQLI .
In this Php Tutorial we will Learn How To Find Data In MySQL Database Table By Id And Show The Results In Form Inputs In Php using MySQLI .
I Use In This Tutorial:
- NetBeans IDE .
- XAMPP .
- PhpMyAdmin .
Php Source Code:
<?php
// php code to search data in mysql database and set it in input text
if(isset($_POST['search']))
{
// id to search
$id = $_POST['id'];
// connect to mysql
$connect = mysqli_connect("localhost", "root", "","test_db");
// mysql search query
$query = "SELECT `fname`, `lname`, `age` FROM `users` WHERE `id` = $id LIMIT 1";
$result = mysqli_query($connect, $query);
// if id exist
// show data in inputs
if(mysqli_num_rows($result) > 0)
{
while ($row = mysqli_fetch_array($result))
{
$fname = $row['fname'];
$lname = $row['lname'];
$age = $row['age'];
}
}
// if the id not exist
// show a message and clear inputs
else {
echo "Undifined ID";
$fname = "";
$lname = "";
$age = "";
}
mysqli_free_result($result);
mysqli_close($connect);
}
// in the first time inputs are empty
else{
$fname = "";
$lname = "";
$age = "";
}
?>
<!DOCTYPE html>
<html>
<head>
<title> PHP FIND DATA </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="php_search_in_mysql_database.php" method="post">
Id:<input type="text" name="id"><br><br>
First Name:<input type="text" name="fname" value="<?php echo $fname;?>"><br>
<br>
Last Name:<input type="text" name="lname" value="<?php echo $lname;?>"><br><br>
Age:<input type="text" name="age" value="<?php echo $age;?>"><br><br>
<input type="submit" name="search" value="Find">
</form>
</body>
</html>
///////////////OUTPUT:
2 comments
commentsI got the right output thankyou so much:),
Replysimilarly i made use of searching username to display userinfo but it was not possible
i got:Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\raji\login_form.php on line 20
:(
my 20th line is:
if(mysqli_num_rows($result) > 0)
please do help me solve this problem
or you can mail me : rajuvanivani@gmail.com
Muito bom!
ReplyDeixa eu tirar uma dúvida, como faço, caso eu queria colocar só o nome, pra achar o resto ?
Idade, ID, etc.
No caso se fosse uma tabela que não aceitasse nomes iguais, ai iria só ter 1 nome pra casa?