Showing posts with label Web Technology. Show all posts
Showing posts with label Web Technology. Show all posts

Tuesday 17 October 2023

DATABASE CONNECTIVITY STEP BY STEP GUIDANCE FOR Beginners....

 

DATABASE CONNECTIVITY

To fetch data from a MySQL database and display it in an HTML table format, you'll need to use a server-side scripting language (such as PHP) to communicate with the database and generate the HTML content. 

Step-by-Step guidance is as follows.

1.      Set Up Your Environment:

Download XAMPP - https://www.apachefriends.org/index.html

2.      Create a Database.

To Create Database: https://www.geeksforgeeks.org/how-to-create-a-new-database-in-phpmyadmin/

3.      Server side Scripting Language.

(PHP) Language: We require server side scripting language for following task:

a.       To connect to the database,

b.      To retrieve data from database,

c.       To generate HTML page.

4.      Establish Database Connection.

To establish a MySQL database connection in PHP script you can use mysqli () extension.

<? php

$host = "your_host";  // Replace with your MySQL server hostname

$username = "your_username";  // Replace with your MySQL username

$password = "your_password";  // Replace with your MySQL password

$database = "your_database";  // Replace with your MySQL database name

 

// Create a connection to the MySQL database

$connection = mysqli_connect ($host, $username, $password, $database);

 

// Check the connection

if (!$connection) {

    die("Connection failed: " . mysqli_connect_error());

}

// You now have a valid database connection in the $connection variable.

 

// Perform database operations, queries, etc. here

 

// Close the connection when you're done

mysqli_close ($connection);

?>

 

5.      Query the Database.

Write a query as per the business logic to retrieve the data from database.

$query = "SELECT * FROM your_table_name";

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

6.      Fetch and Format Data.

Now you can retrieve the data from the result set and format it as HTML. To do this we can use the loop to iterate through the data and prepare the table rows and cells.

echo "<table>";

while ($row = mysqli_fetch_assoc($result)) {

             echo "<tr>";

            echo "<td>" . $row['column1'] . "</td>";

            echo "<td>" . $row['column2'] . "</td>";

            // Add more columns as needed

             echo "</tr>";

}

echo "</table>";

7.      Display in HTML.

You have to embed the PHP code that generates the HTML table in your HTML file. Ensure that the HTML file has a proper file extension like .php

            <!DOCTYPE html>

            <html>

            <head>

                <title>MySQL to HTML Table</title>

            </head>

            <body>

                <?php

                    // Your PHP code here

                ?>

            </body>

            </html>

8.      Style the Table by using a style sheet.

        You can use CSS in your code to make it style better.

9.      Check the data which is displayed on a webpage is valid.

        In the end, you have to check whether the data which are fetching from the table are valid or not.

Saturday 22 April 2023

Implement the sample program demonstrating the use of Servlet.

 Example: Create a database table ebookshop (book_id,book_title,book_author,book_price,quantity) using database like oracle/MySQL etc and display (use SQL select query ) the table content using servlet.



Sunday 18 September 2022

Student Management Project using Technologies: Spring MVC, Hibernate, ORM

How to create Spring MVC project: Customer Relationship Management Code

Monday 30 September 2019

Step by Step guide to connect login page with database using HTML, CSS, PHP, MySQL

Problem statement:
 Design HTML pages for Registration/login and connect that pages with database also generate 
 several reports as per the application requirement in your project.
Objective:
 1. create login and registration page.
   1.1 when user enters username and password check either it is valid display the message login succesfull..or if not.
   1.2 if it is not valid store his information in database and make him valid and again perform the
         login if login successfull display the appropriate message login successfull.
Solution:
 A. First install XAMPP server on your machine as per the machine operating system and also
     download sublime Text editor. (Errors may be like this:>api-ms-win-crt-runtime-|1-1-0.dll
     reinstall)
 B. Start Apache and MySQL services.
 C. Go to browser and type localhost/phpmyadmin
 D. Create database eg:login after that Create table eg:users inside this database and insert
      one row value in it like testuser and password testuserpass.
 E. Create one folder inside C:/Program file/XAMPP --> htdoc ---> create your project folder like
      LoginProj
 F. inside this LoginProj folder store three files in it. 1.login.php 2. style.css  3.process.php
 G. Now inside login.php write the code of login form design with form action=process.php and
      method="POST"
 H. After that go to Browser and check either login form design is proper or not so for that type
      localhost/LoginProj/login.php.
 I. When we click on login button it take us to process.php page.
 J. Now go to style.css page and write all code neccessary for login form design.
 K. Now go to process.php page and write code
        //1.to get values passes from form in login.php file.
      <?php
         $username=$_POST['username']; //username must be id="username" value in login.php file
         $password=$_POST['password']; //password must be id="password" value in login.php file
       //2.to prevent mysql injection. Ref:https://www.youtube.com/watch?v=arqv2YVp_3E
         $username=stripcslashes($username);
         $password=stripcslashes($password);
         $username=mysql_real_escape_string($username);       
         $password=mysql_real_escape_string($password);
       //3.connect to the server and select database.
         mysql_connect("localhost","root","");
         mysql_select_db("login");
       //4.query the database for user.
        $result=mysql_query("select * from users where username = '$username' and password =
        '$password'") or die("Failed to query database" .mysql_error());
        $row = mysql_fetch_array($result);
        if($row['username']==$username && $row['password']==$password)
       {
         echo "Login success!!! Welcome".$row['username'];
       }
       else
       {
         echo "Failed to login...";
       }
     ?>
 L. Now go to browser and type localhost/LoginProj/login.php. enter user and password and click on login button appropriate message gets displayed.

/*HERE IS THE CODE FOR ALL PAGES*/ 

 1. login.php
 <html>
<head>
 <title> Login and Registration Form Design</title>
 <link rel="stylesheet" href="style.css">
</head>
<body>
 <div class="login-page">
 <div class="form">
  <form class="register-form">
  <input type="text" placeholder="username" name="useri" id="user"/>
  <input type="text" placeholder="password" name="pasw" id="pass"/>
  <input type="text" placeholder="email id" name="emaill" id="email"/>
  <button>Create</button>
  <p class="message"> Already Registered?<a href="#"> Login</a></p>
  </form>
  <form class="login-form" action="process.php" method="POST">
  <input type="text" placeholder="username" id="username" name="usr"/>
  <input type="password" placeholder="password" id="password" name="passw"/>
  <button>Login</button>
  <p class="message">Not Registered?<a href="#"> Register</a></p>
 </div>
 </div>
 <script src='https://code.jquery.com/jquery-3.4.1.min.js'>
 </script>
 <script>
  $('.message a').click(function(){
  $('form').animate({height:"toggle",opacity:"toggle"},"slow");
  });
 </script>

</body>
</html>


2. style.css 

body{
 background-image:linear-gradient(rgba(0,0,0,0.6),rgba(0,0,0,0.6)),url(Lighthouse.jpg);
 height:100vh;
 background-size:cover;
 background-position:center;
}
.login-page{
 width:360px;
 padding:10% 0 0;
 margin:auto;
}

.form{
 position:relative;
 z-index:1;
 background:rgba(7,40,195,0.8);
 max-width:360px;
 margin:0 auto 100px;
 padding:45px;
 text-align:center;
}

.form input{
 font family:"Roboto",sans-serif;
 outline:1;
 background:#f2f2f2;
 width:100%;
 border:0;
 margin:0 0 15px;
 padding:15px;
 box-sizing:border-box;
 font-size:14px;
}

.form button{
 font family:"Roboto",sans-serif;
 text-transform:uppercase;
 outline:0;
 background:#4cAF50;
 width:100%;
 border:0;
 padding:15px;
 color:#ffffff;
 font-size:14px;
 cursor:pointer;
}

.form button:hover,.form button:active{
 background:#43A047;
}

.form .message{
 margin:15px 0 0;
 color:aliceblue;
 font-size:12px;
}

.form .message a{
 color:#4CAF50;
 text-decoration:none;
}

.form .register-form{
 display:none;
}

3. process.php

<?php
        $con=mysqli_connect("localhost","root","","login");
        // Check connection
           if (mysqli_connect_errno()) {
           echo "Failed to connect to MySQL: " . mysqli_connect_error();
          }
          // escape variables for security
          $uname = mysqli_real_escape_string($con, $_POST['usr']);
          $password = mysqli_real_escape_string($con, $_POST['passw']);

         //query to the database

          $sql="select * from users where username = '$uname' and password = '$password'";
          $result=mysqli_query($con,$sql);

          if (!mysqli_query($con,$sql)) {
              die('Error: ' . mysqli_error($con));
              }
              else
              {

                $row=mysqli_fetch_array($result,MYSQLI_ASSOC);

                if($row['username']==$uname && $row['password']==$password)
                {
                 echo "Login success!!! Welcome ".$row['username'];
                }
                else
                {
                 echo "Failed to login...";
                }

              }

            mysqli_close($con);      
     ?>