MySQL Forums
Forum List  »  PHP

Re: php mysql search
Posted by: Marc Cardinal
Date: May 20, 2018 04:21PM

Thanks for the explanation.
I probably should have mentioned that I'm a real novice at this, and I was hoping someone could just fix the code? It worked perfectly before I adjusted it for a Lastname search instead of an "ID" search.
I would really just like to tweak this ID search code and replace it to search for Lastname instead.

Here's the code that was working before I tweaked it:

<?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", "root","marctest");

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


$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'];
$Lastname = $row['Lastname'];
$email = $row['email'];
}
}

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


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

}

// in the first time inputs are empty
else{
$Firstname = "";
$Lastname = "";
$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">

Id:<input type="text" name="id"><br><br>

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

Last Name:<input type="text" name="Lastname" value="<?php echo $Lastname;?>"><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
May 20, 2018 09:43AM
May 20, 2018 10:32AM
Re: php mysql search
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.