Crud Operation Code

Crud Operation Code
Crud Operation Code
full-width
Send Bootsrep Button Database
<input type="submit"  class="btn btn-primary btn-lg btn-block mt-4" value="Click me" style="width: 100%;"/>

CRUD is an acronym for Create, Read, Update, and Delete. CRUD operations are basic data manipulation for database. ... In this tutorial we'll create a simple PHP application to perform all these operations on a MySQL database table at one place.

Data Read
<?php
    $conn = mysqli_connect("localhost","root","","crud") or die("Connection Faild");
    $sql = "SELECT * FROM crud_operation";
    $result =mysqli_query($conn,$sql) or die("Query Unsucessful.");
    if(mysqli_num_rows($result)>0){
    ?>
<table id="customers">
  <tr>
    <th id="id">ID</th>
    <th>Name</th>
    <th>Father Name</th>
    <th>Phone Number</th>
    <th>Email Address</th>
    <th>Current Address</th>
    <th>Permanant Address</th>
    <td><a href="#?id=<?php echo $row['id'];?>">Delete</a>>/td>
    <td><a href="#?id=<?php echo $row['id'];?>">Edit</a>>/td>
  </tr>
  <?php
  while($row = mysqli_fetch_assoc($result)){
  ?>
  <tr>
    <td><?php echo $row['id'];?></td>
    <td><?php echo $row['Name'];?></td>
    <td><?php echo $row['father_name'];?></td>
    <td><?php echo $row['phone_number'];?></td>
    <td><?php echo $row['email_address'];?></td>
    <td><?php echo $row['address_no1'];?></td>
    <td><?php echo $row['address_no2'];?></td>
    <td>Delete</td>
    <td>Edit</td>
  </tr>
  <?php
  }
  ?>
</table>
<?php
}else{
    echo "record not found";
}?>

Send Data to Database

Send Data in Crud Operation is First to Create a Form and use the action="send.php" and action="POST" is used in the form section to send data from, Form to Server mean Database. When the form is fill the data goes to next file name is used in the action Section and then Query are the Pass or Data Send to Database.

MySQL Connection Using CMD:
<?php
  $stud_name = $_POST['sname'];
  $father_name = $_POST['fname'];
  $phone_number = $_POST['sphone'];
  $select_email = $_POST['selectemail'];
  $current_address = $_POST['caddress'];
  $Permanant_address = $_POST['paddress'];
  $conn = mysqli_connect("localhost","root","","crud") or die("Connection Faild");
  $sql = "INSERT INTO crud_operation(Name,father_name,phone_number,email_address,address_no1,address_no2) values('$stud_name','$father_name','$phone_number','$select_email','$current_address','$Permanant_address')";
  $result =mysqli_query($conn,$sql) or die("Query Unsucessful.") or die("query not work");
  header("Location:http://localhost/crud/show.php");
  mysqli_close($conn);
  ?>
Delete Data in Database
<?php
include 'config.php';
$stu = $_GET['id'];
$sql = "Delete From crud_operation where id = {$stu}";
$result = mysqli_query($conn,$sql) or die("Query Not Work");

//Return Back to the Form
header("Location:http://localhost/crud/show.php");
?>

Update Data in Database
<body style="background-color:#ecf0f1;margin-bottom: 10px;">
    <h1 id="registration-text">Registration Form</h1>

    <?php
    $conn = mysqli_connect("localhost","root","","crud") or die("Connection Faild");
    $stu_id = $_GET['id'];
    $sql = "SELECT * FROM crud_operation WHERE id = {$stu_id}";
    $result = mysqli_query($conn,$sql) or die("Query Unsucessful.");
    if(mysqli_num_rows($result)>0){

        while($row = mysqli_fetch_assoc($result)){
    ?>
    <form action="updatedata.php" method="post">

            <div class="form-style">
            <label for="studentname">Name</label><br/>
            <input type="hidden" name="ssid" value="<?php echo $row['id'];?>"/>
            <input type="text" name="sname" placeholder="Enter Name" value="<?php echo $row['Name'];?>" required/>
        </div>

        <div class="form-style">
            <label for="fathername">Father Name:</label><br/>
            <input type="text" name="fname" placeholder="Enter Father Name" value="<?php echo $row['father_name'];?>"/>
        </div>

        <div class="form-style">
            <label for="phoneno">Phone Number:</label><br/>
            <input type="number" name="sphone" placeholder="Enter Number" value="<?php echo $row['phone_number'];?>"/>
        </div>

        <div class="form-group">
            <label for="email">Email Address:</label><br/>
        <?php
           $sql1 = "SELECT * FROM email";
           $result1 =mysqli_query($conn,$sql1) or die("Query Unsucessful.");
           if(mysqli_num_rows($result1)>0){
               echo '<select name="sclass">';
               while($row1= mysqli_fetch_assoc($result1)){

                if($row['sclass']==$row1['email_address']){
                    $select = "selected";
                }else{
                    $select = " ";
                }
               echo "<option {$select} >{$row1['semail']}</option>";
               }
            echo "</select>";
            }
            ?>
        </div>

        <div class="form-style">
            <label for="Address1">Address No 1:</label><br/>
            <input type="text" name="caddress" placeholder="Enter Current" value="<?php echo $row['address_no1'];?>">
        </div>

        <div class="form-style">
            <label for="Address2">Address No 2:</label><br/>
            <input type="text" name="paddress" placeholder="Enter Permanant" value="<?php echo $row['address_no2'];?>">
        </div>

        <input type="submit" value="Update" class="submit-button">
    </form>
    <?php
}}
    ?>
</body>
Update Data in Database
<?php
$stu_id = $_POST['ssid'];
$stud_name = $_POST['sname'];
$father_name = $_POST['fname'];
$phone_number = $_POST['sphone'];
$select_email = $_POST['sclass'];
$current_address = $_POST['caddress'];
$Permanant_address = $_POST['paddress'];

$conn = mysqli_connect("localhost","root","","crud") or die("Connection Faild");

$sql = "UPDATE crud_operation SET Name='{$stud_name}',father_name='{$father_name}',phone_number='{$phone_number}',email_address='{$select_email}',address_no1='{$current_address}',address_no2='{$Permanant_address}' WHERE id = '{$stu_id}'";
$result = mysqli_query($conn,$sql) or die("Query Unsucessful.");
header("Location:http://localhost/crud/show.php");
mysqli_close($conn);
?>

Post a Comment

Post a Comment (0)

Previous Post Next Post