<?php $conn = new mysqli('localhost', 'root', '', 'db_csv'); if(!$conn){ } ?>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/> <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/> </head> <body> <nav class="navbar navbar-default"> <div class="container-fluid"> <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a> </div> </nav> <div class="col-md-3"></div> <div class="col-md-6 well"> <h3 class="text-primary">PHP - Generate CSV File Using MySQLi</h3> <hr style="border-top:1px dotted #ccc;"/> <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add Employee</button> <a href="export.php" class="btn btn-success pull-right"><span class="glyphicon glyphicon-export"></span> Export as CSV</a> <br /><br /> <table class="table table-bordered"> <thead class="alert-success"> <tr> <th>Firstname</th> <th>Lastname</th> <th>Address</th> <th>Age</th> <th>Job</th> </tr> </thead> <tbody style="background-color:#fff;"> <?php require 'conn.php'; $query = $conn->query("SELECT * FROM `employee` ORDER BY `lastname` ASC"); while($fetch = $query->fetch_array()){ ?> <tr> <td><?php echo $fetch['firstname']?></td> <td><?php echo $fetch['lastname']?></td> <td><?php echo $fetch['address']?></td> <td><?php echo $fetch['age']?></td> <td><?php echo $fetch['job']?></td> </tr> <?php } ?> </tbody> </table> </div> <div class="modal fade" id="form_modal" aria-hidden="true"> <div class="modal-dialog" role="document"> <form method="POST" action="save_query.php"> <div class="modal-content"> <div class="modal-body"> <div class="col-md-2"></div> <div class="col-md-8"> <div class="form-group"> <label>Firstname</label> <input class="form-control" type="text" name="firstname"/> </div> <div class="form-group"> <label>Lastname</label> <input class="form-control" type="text" name="lastname"/> </div> <div class="form-group"> <label>Address</label> <input class="form-control" type="text" name="address"/> </div> <div class="form-group"> <label>Age</label> <input class="form-control" type="number" name="age"/> </div> <div class="form-group"> <label>Job</label> <input class="form-control" type="text" name="job"/> </div> </div> </div> <div style="clear:both;"></div> <div class="modal-footer"> <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button> <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button> </div> </div> </form> </div> </div> </body> <script src="js/jquery-3.2.1.min.js"></script> <script src="js/bootstrap.js"></script> </html>
<?php require_once 'conn.php'; $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $address = $_POST['address']; $age = $_POST['age']; $job = $_POST['job']; $conn->query("INSERT INTO `employee` VALUES('', '$firstname', '$lastname', '$address', '$age', '$job')") or die(mysqli_errno()); echo "<script>alert('Data Inserted!')</script>"; echo "<script>window.location = 'index.php'</script>"; }else{ echo "<script>alert('Please complete the require field!')</script>"; echo "<script>window.location = 'index.php'</script>"; } } ?>
<?php require_once 'conn.php'; $query = $conn->query("SELECT * FROM `employee` ORDER BY `lastname` ASC"); while($fetch = $query->fetch_assoc()){ } ?>