Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have created a page in html where i have placed a textbox and the user will supply the value in it which will then retrieve the data corresponding to the value from my mysql database. I am unable to retrieve the data.

HTML
<!DOCTYPE HTML>
<html>
<head>
<title>Fetching saved records</title>
</head>

<body>
<div class="container">
	<div class="displaydetails">
	<form method="POST" action="RetrieveandDisplay.php">
		Enter the name to search data:<br/>
		<input type="text" name="FetchDetails">
		<input type="submit" name="Find" value="Find Data"/>
	</form>
	</div>
</div>
</body>
</html>



PHP
<?php
	include('connectionstring.php');
	
	if(!$connection)
	{
		die('Could not connect to database : '.mysql_error());
	}
	
	$inputname=$_POST['Find'];
	
	$myquery="SELECT * FROM addentry WHERE FirstName='$inputname'";
	
	$fetched=mysql_query($myquery);
	
	while($rowvalue=mysql_fetch_array($fetched))
	{
		$firstname=$rowvalue['FirstName'];
		$middlename=$rowvalue['MiddleName'];
		$lastname=$rowvalue['LastName'];
		$phone1=$rowvalue['Phone1'];
		$phone2=$rowvalue['Phone2'];
		$email=$rowvalue['Email'];
		$address=$rowvalue['Address'];
	}
	mysql_close($connection);
?>

<html>
<body>
<div class="container">
<div class="fetched">
<form>
	<label>First Name</label>
	<input name="FirstName" type="text" value='<?php echo $firstname; ?>'/>
	<label>Middle Name</label>
	<input name="MiddleName" type="text" value='<?php echo $middlename; ?>'/>
	<label>Last Name</label>
	<input name="LastName" type="text" value='<?php echo $lastname; ?>'/>
	<label>Phone1</label>
	<input name="Phone1" type="text" value='<?php echo $phone1; ?>'/>
	<label>Phone2</label>
	<input name="Phone2" type="text" value='<?php echo $phone2; ?>'/>
	<label>Email</label>
	<input name="email" type="text" value='<?php echo $email; ?>'/>
	<label>Address</label>
	<textarea id="Address" name="address1" value='<?php echo $address; ?>'></textarea>
	
</form>
</div>
</div>
</body>
</html>
Posted
Updated 19-Oct-20 17:45pm
v2
Comments
[no name] 3-Mar-14 13:36pm    
Please try it youself. If you face any problem then give details about this. Thanks
DevParashar 4-Mar-14 11:59am    
I have created a page in html where i have placed a textbox and the user will supply the value in it which will then retrieve the data corresponding to the value from my mysql database. I am unable to retrieve the data.

<!DOCTYPE HTML>
<html>
<head>
<title>Retrieve Contact</title>
</head>
<body>
<div class="container">
<div class="dsp">
<form method="POST" action="retrievedetails.php">
Enter the first name of any contact to fetch: <br/>
<input type="text" name="FetchName"/>
<input type="submit" name="submit" value="Fetch Details"/>
</form>

</div>
</div>
</body>
</html>


Here is my PHP code.

Krunal Rohit 3-Mar-14 13:36pm    
Any efforts so far ?

-KR
DevParashar 4-Mar-14 12:00pm    
<!DOCTYPE HTML>
<html>
<head>
<title>Retrieve Contact</title>
</head>
<body>
<div class="container">
<div class="dsp">
<form method="POST" action="retrievedetails.php">
Enter the first name of any contact to fetch: <br/>
<input type="text" name="FetchName"/>
<input type="submit" name="submit" value="Fetch Details"/>
</form>

</div>
</div>
</body>
</html>


PHP code

Krunal Rohit 4-Mar-14 12:42pm    
where's PHP ?

-KR

1 solution

Working solution
<?php
include("database.php");
$db=$conn;
// fetch query
function fetch_data(){
 global $db;
  $query="SELECT * FROM users WHERE username='vii'"; // change this
  $exec=mysqli_query($db, $query);
  if(mysqli_num_rows($exec)>0){
    $row= mysqli_fetch_all($exec, MYSQLI_ASSOC);
    return $row;  
        
  }else{
    return $row=[];
  }
}
$fetchData= fetch_data();
show_data($fetchData);


foreach($fetchData as $data){ 
       
    $firstname=$data['udid'];} // change this 'udid' to your table field

?>
<!DOCTYPE HTML>
<html>
<head>
<title>Retrieve Contact</title>
</head>
<body>
<input type=text value= <?php echo "sdfsafs".$firstname; ?>
</body>
</html>
 
Share this answer
 
Comments
Dave Kreskowiak 20-Oct-20 0:36am    
IO doubt the OP is still waiting for an answer 4 years later and you haven't used parameters in your SQL query. You have, however, left your query vulnerable to SQL Injection attacks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900