Why do we need a database when we come to the question if we are going to make an interactive site is very important to have a database. Html / Css sites are static sites.
Now let’s move on to the coding side, which is the most beautiful part of the job.?
First we need to create database and table with mysql. I use Mysql Workbench program for this. But you can also create database and tables from the command line. I created a table in the test database as information.
CREATE TABLE `test`.`bilgiler` ( `id` INT NOT NULL AUTO_INCREMENT, `ad` VARCHAR(25) NULL, `soyad` VARCHAR(25) NULL, PRIMARY KEY (`id`));
Adding Records
Now, how we are adding records to our database look at it. Bootstrap is a really nice blessing, making the appearance of normal html tags really more beautiful. It also adds richness to the appearance with some unique features.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.6/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <form action="php_kaydet.php" method="post" class="form-control"> <input type="text" placeholder="Ad" name="ad" class="form-control"> <input type="text" placeholder="Soyad" name="soyad" class="form-control"> <input type="submit" value="Kaydet"> </form> </div> </div> </body> </html>
The action element within the form tag specifies which page we will send the request to. If you send with the Get method, the information you send will appear in the address bar. Click on the link to write about this topic.
http://fatihdemirag.net/php-get-post-istekleri/
Now let’s go to the code that we will write on the server side, ie php_kaydet.php.
<?php $con=mysqli_connect("localhost","dbKullaniciAdi","dbSifre","test") or die("Hata"); $ad=$_POST['ad']; $soyad=$_POST['soyad']; $sql="insert into bilgiler(ad,soyad) values('$ad','$soyad')"; $res=mysqli_query($con,$sql); if ($res) echo "Bilgiler kaydedildi."; else echo "Bilgiler kaydedilemedi."; mysqli_close($con); ?>
In the first line, we assign our mysql connection into the $ con variable. We will use this variable later when running the query. Here are the important points. One of the requests is the name of the input tag in our html page. name must be $ _POST [‘ad’].
<input type="text" placeholder="Ad" name="ad" class="form-control">
$ad=$_POST['ad'];
Then we run our query in our $ res variable. After running, if the query returned true, our data was added. The last one is closing the database connection.
Listing Records
I used the table when listing the records, but the desired tags can be used. I used it because the table is more organized. I did the list on the same page.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.6/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <form action="php_kaydet.php" method="post" class="form-control"> <input type="text" placeholder="Ad" name="ad" class="form-control"> <input type="text" placeholder="Soyad" name="soyad" class="form-control"> <input type="submit" value="Kaydet"> </form> </div> <div class="container"> <table class="table"> <thead> <tr> <th> Ad </th> <th> Soyad </th> </tr> </thead> <tbody> <tr> <?php $con=mysqli_connect("localhost","dbKullaniciAdi","dbSifre","test") or die("Hata"); $sql="select * from bilgiler"; $res=mysqli_query($con,$sql); while ($row=mysqli_fetch_assoc($res)) { echo "<td>".$row["ad"]."</td>"; echo "<td>".$row["soyad"]."</td>"; } mysqli_close($con); ?> </tr> </tbody> </table> </div> </body> </html>