Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Fetching Data from database using php

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 246
    Comment on it

    To fetch a data from database by using simple PHP code,  firstly create a connection to the database by using simple query and create a login form using HTML code for entering values through user. on clickinng submit button, This form will be loaded into the server on the same page itself and give the result of User's choice in the tabular form.

    <html>
    <head>
      <title>login form</title>
    </head>
    <body>
    <form style="border:1px solid #000; width:250px; margin:10px auto 0px; padding:10px;" method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
    <h3 style="color:#6EA435;">Online Login From</h3>
    <input type="text" style="border-radius:0px; padding:6px 10px; border:1px solid #000; margin-bottom:2px;" name="id" placeholder="id"><br>
    <input type="text" style="border-radius:0px; padding:6px 10px; border:1px solid #000; margin-bottom:2px;" name="name"placeholder="Name"><br>
    <input type="text" style="border-radius:0px; padding:6px 10px; border:1px solid #000; margin-bottom:2px;" name="f_name"placeholder="Father_name"><br>
    <input type="text" style="border-radius:0px; padding:6px 10px; border:1px solid #000; margin-bottom:2px;" name="email"placeholder="email"><br>
    <input type="text" style="border-radius:0px; padding:6px 10px; border:1px solid #000; margin-bottom:6px;" name="number"placeholder="mobile_number"><br>
    <input type="text" style="border-radius:0px; padding:6px 10px; border:1px solid #000; margin-bottom:6px;" name="address"placeholder="address"><br>
     Gender:
    <input type="radio" name="gender" value="female">Female
    <input type="radio" name="gender" value="male">Male
    <br>
    <br>
    <button type="submit" style="background-color:#6EA435;color:#fff; border:1px solid #000; border-radius:0px; padding:5px 10px; margin-left:70px;">Submitt</button>
    </form>
    
     <?php
       include 'database.php';
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
      if (isset($_POST['id']) && ($_POST['name']) && ($_POST['f_name']) && ($_POST['email']) && ($_POST['number']) && ($_POST['address']) && ($_POST['gender'])) {
      $id = $_POST['id'];
      $name = $_POST['name'];
      $father_name = $_POST['f_name'];
      $email = $_POST['email'];
      $mobile_number = $_POST['number'];
      $address = $_POST['address'];
      $gender=$_POST['gender'];
    
      $sql = "INSERT INTO login_form(id, name, father_name, email, mobile_number, address, gender) VALUES ('$id', '$name', '$father_name','$email',' $mobile_number','$address','$gender')";
        if ($conn->query($sql) === TRUE) {
            echo "New record created successfully";
        } 
        else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
    
      } 
      else
        {
         echo "<script type='text/javascript'> alert('some fields left empty')</script>";
        }
      } 
    $sql= "SELECT * FROM login_form";
    $result=$conn->query($sql);
    if ($result->num_rows >0) {
      echo "<table>";
      echo "<tr><th>ID</th> <th>NAME</th>  <th>FATHER NAME</th> <th>EMAIl</th> <th>MOBILE NO.</th> <th>ADDRESS</th> <th>GENDER</th> </tr>";
      while ($row=$result->fetch_assoc()) {
        echo "<tr> <td>".$row['id']."</td> <td>".$row['name']."</td> <td>".$row['father_name']."</td> <td>".$row['email']."</td> <td>".$row['mobile_number']."</td> <td>".$row['address']."</td> <td>".$row['gender']."</td> </tr>";
      }
      echo "</table>";
    }
    $conn->close();
    ?>
    </body>
    </html>

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: