MySQL Forums
Forum List  »  PHP

php mysql search
Posted by: Marc Cardinal
Date: May 20, 2018 09:43AM

Looking for hep with this code...originally it was working for an ID search, but I modified it to search for Lastname instead. I'm a little stumped now. Here's the full code:

<?php

// php code to search data in mysql database and set it in input text

if(isset($_POST['search']))
{
// id to search
$Lastname = $_POST['Lastname'];
$ID = $_POST['ID'];

// connect to mysql
$connect = mysqli_connect("localhost", "root", "root","marctest");

// mysql search query
$query = "SELECT ID, Lastname, Firstname, email FROM contacts WHERE 'Lastname' = $Lastname";


$result = mysqli_query($connect, $query);

// if id exist
// show data in inputs
if(mysqli_num_rows($result) > 0)
{
while ($row = mysqli_fetch_array($result))
{
$Firstname = $row['Firstname'];
$ID = $row['ID'];
$email = $row['email'];
}
}

// if the id not exist
// show a messemail and clear inputs
else {
echo "Undefined ID";
$Firstname = "";
$ID = "";
$email = "";
}


mysqli_free_result($result);
mysqli_close($connect);

}

// in the first time inputs are empty
else{
$Firstname = "";
$ID = "";
$email = "";
}


?>

<!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">

Lastname:<input type="text" name="Lastname"><br><br>

First Name:<input type="text" name="Firstname" value="<?php echo $Firstname;?>"><br>
<br>

ID:<input type="text" name="ID" value="<?php echo $ID;?>"><br><br>

email:<input type="text" name="email" value="<?php echo $email;?>"><br><br>

<input type="submit" name="search" value="Find">

</form>

</body>

</html>

Options: ReplyQuote


Subject
Written By
Posted
php mysql search
May 20, 2018 09:43AM
May 20, 2018 10:32AM
May 20, 2018 04:21PM
May 24, 2018 05:31PM
May 24, 2018 06:47PM
May 24, 2018 10:12PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.