CRUD System in PHP – How to Insert, Update, Delete and Read in PHP using MySQL Database & Bootstrap
designing code:
<title>CRUD Application</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script> </head> <body> <div class="container text-center"> <div class="row"> <div class="col"> </div> <div class="col-lg-4 bg-light rounded"> <div class="bg-light p-1 rounded mb-3 mt-3"> <h1>CRUD Application</h1> </div> <form action="" method="post" > <?php if($result) { if(mysqli_num_rows($result)> 0){ ?> <table class="table table-dark"> <tr> <th>ID</th> <th>EMAIL</th> <th>PASSWORD</th> <th>FNAME</th> <th>LNAME</th> </tr> <?php while($rows=mysqli_fetch_assoc($result)) { ?> <tr> <td><?php echo $rows['id']; ?></td> <td><?php echo $rows['email']; ?></td> <td><?php echo $rows['pass']; ?></td> <td><?php echo $rows['fname']; ?></td> <td><?php echo $rows['lname']; ?></td> </tr> <?php }?> </table> <?php }} else{ ?> <div class="form-floating mb-3"> <input type="text" name="id" class="form-control" id="floatingInput"> <label for="floatingInput">ID</label> </div> <div class="form-floating mb-3"> <input type="email" name="email" class="form-control" id="floatingInput" placeholder="name@example.com"> <label for="floatingInput">Email address</label> </div> <div class="form-floating mb-3"> <input type="password" name="pass" class="form-control" id="floatingPassword" placeholder="Password"> <label for="floatingPassword">Password</label> </div> <div class="form-floating mb-3"> <input type="text" name="fname" class="form-control" id="floatingInput" placeholder="Run"> <label for="floatingInput">First Name</label> </div> <div class="form-floating mb-3"> <input type="text" name="lname" class="form-control" id="floatingPassword" placeholder="Codes"> <label for="floatingPassword">Last Name</label> </div> <?php }?> <div class="d-grid gap-2 d-md-flex"> <a class="btn btn-secondary " href="/crudphp.php" name="home">Home</a> <button class="btn btn-primary" type="submit" name="create" >Create</button> <button class="btn btn-success " type="submit" name="read" >Read</button> <button class="btn btn-warning" type="submit" name="update" >Update</button> <button class="btn btn-danger" type="submit" name="delete" >Delete</button> </div> </form> </div> <div class="col"> </div> </div> </div> </body> </html>
php code:
<?php $conn = mysqli_connect("localhost","root","admin","crudphp"); if($conn){ echo "connection"; }else{ echo "error"; } //insertion code if(isset($_POST["create"])){ $email=$_POST['email']; $password=$_POST['pass']; $fname= $_POST['fname']; $lname= $_POST['lname']; $insertQuery="INSERT INTO crudphp (email,pass,fname,lname) VALUES(?,?,?,?)"; $stmt=mysqli_prepare($conn,$insertQuery); mysqli_stmt_bind_param($stmt,"ssss", $email, $password, $fname, $lname); mysqli_stmt_execute($stmt); if(mysqli_affected_rows($conn)> 0){ echo "<script type='text/javascript'>alert('Created Successfully')</script>"; }else{ echo "error"; } } //READ $result=""; if(isset($_POST["read"])){ $readQuery="SELECT * FROM crudphp"; $result=mysqli_query($conn,$readQuery); } //update if(isset($_POST["update"])){ $id=$_POST['id']; $email=$_POST['email']; $password=$_POST['pass']; $fname= $_POST['fname']; $lname= $_POST['lname']; $updateQuery="UPDATE crudphp SET email=?, pass=?, fname=?, lname=? WHERE id=?"; $stmt=mysqli_prepare($conn,$updateQuery); mysqli_stmt_bind_param($stmt,"ssssi",$email,$password,$fname,$lname,$id); mysqli_stmt_execute($stmt); if(mysqli_affected_rows($conn)> 0){ echo "<script type='text/javascript'>alert('updated Successfully')</script>"; }else{ echo "<script type='text/javascript'>alert('error in updation')</script>"; } } //DELETION if(isset($_POST["delete"])){ $id=$_POST["id"]; $deleteQuery= "DELETE FROM crudphp WHERE id=?"; $stmt=mysqli_prepare($conn,$deleteQuery); mysqli_stmt_bind_param($stmt,"i", $id); mysqli_stmt_execute($stmt); if(mysqli_affected_rows($conn)> 0){ echo "<script type='text/javascript'>alert('deleted Successfully')</script>"; }else{ echo "<script type='text/javascript'>alert('error in deletion')</script>"; } } ?>