Reputation: 29
I already created a my sql database "travel_guide" with the table "places". it has columns "ID" "name" "details". And I have a html web site. I want to connect the database to the web page and retrieve place details from the place table by searching the place name in the search bar of web site. how can I do this? I'm new to this.
Upvotes: 1
Views: 21806
Reputation: 119
step 1 your going to need to connect to your database
step 2 your going to need something to pass the values from your database into your html to display them.
So since your beginning I would suggest using php to pass values to and from your database.
To connect to your database your going to need to specify 4 things. The servername the username of the user you have created for your database that users password and the actual name of your database I have included an example below
<?php
$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "Brendan7";
$dbName = "cars";
$conn = mysqli_connect( $dbServername , $dbUsername , $dbPassword , $dbName );
Now once you have connected you can make a SQL select statement to bring data into you html like so
<?php
include_once 'header.php';
$user_id = $_SESSION["u_id"];
$sql = "SELECT * FROM `person` WHERE person_id = " .$user_id." ";
// $sql = "SELECT SINGLE `user_id` FROM `users` WHERE user_id = '".$user_id."'";
$conn = mysqli_connect( $dbServername , $dbUsername , $dbPassword , $dbName );
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "user_id: " . $row["person_id"]. " - person_id: " . $row["person_first"]. " " . $row["person_last"]. "<br>";
$person_id = $row["person_id"];
$person_first = $row["person_first"];
}
} else {
echo $sql;
}
$conn->close();
?>
<div class="form-group">
<label class="col-md-12">First Name</label>
<div class="col-md-12">
<input type="text" required="" name="person_first" value="<?php echo $person_first;?>" class="form-control form-control-line"> </div>
</div>
<div class="form-group">
<label class="col-md-12">Last Name</label>
<div class="col-md-12">
<input type="text" required="" name="person_last" value="<?php echo $person_last;?>" class="form-control form-control-line"> </div>
</div>
<div class="form-group">
<label for="example-email" class="col-md-12">Mobile</label>
<div class="col-md-12">
<input type="text" required="" name="person_mobile" value="<?php echo $person_mobile;?>" class="form-control form-control-line"> </div>
</div>
<div class="form-group">
<label for="example-email" class="col-md-12">Address</label>
<div class="col-md-12">
<input type="address" required="" name="person_address" value="<?php echo $person_address;?>" class="form-control form-control-line"> </div>
</div>
<div class="form-group">
<label class="col-md-12">Email</label>
<div class="col-md-12">
<input type="text" required="" name="person_email" value="<?php echo $person_email;?>" class="form-control form-control-line"> </div>
</div>
<div class="form-group">
<label class="col-md-12">SSN</label>
<div class="col-md-12">
<input type="text" required="" name="SSN" value="<?php echo $SSN;?>" class="form-control form-control-line"> </div>
</div>
<?php
include_once 'footer.php';
?>
Upvotes: 3