Help me
Hi im new in programming How to print the error message in a function i want to display "connection success" and "connection failed" i put echo but nothing happens
<?php require 'config.php'; class db_class extends db_connect{ public function __construct(){ $this->connect(); } public function create($firstname, $lastname){ $stmt = $this->conn->prepare("INSERT INTO `member` (`firstname`, `lastname`) VALUES (?, ?)") or die($this->conn->error); $stmt->bind_param("ss", $firstname, $lastname); if($stmt->execute()){ $stmt->close(); $this->conn->close(); return true; } } public function read(){ $stmt = $this->conn->prepare("SELECT * FROM `member` ORDER BY `lastname` ASC") or die($this->conn->error); if($stmt->execute()){ $result = $stmt->get_result(); $stmt->close(); $this->conn->close(); return $result; } } public function member_id($mem_id){ $stmt = $this->conn->prepare("SELECT * FROM `member` WHERE `mem_id` = '$mem_id'") or die($this->conn->error); if($stmt->execute()){ $result = $stmt->get_result(); $fetch = $result->fetch_array(); $stmt->close(); $this->conn->close(); return $fetch; } } public function delete($mem_id){ $stmt = $this->conn->prepare("DELETE FROM `member` WHERE `mem_id` = '$mem_id'") or die($this->conn->error); if($stmt->execute()){ $stmt->close(); $this->conn->close(); return true; } } public function update($firstname, $lastname, $mem_id){ $stmt = $this->conn->prepare("UPDATE `member` SET `firstname` = '$firstname', `lastname` = '$lastname' WHERE `mem_id` = '$mem_id'") or die($this->conn->error); if($stmt->execute()){ $stmt->close(); $this->conn->close(); return true; } } } ?>
<!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" /> <title>OOP PHP CRUD Operation</title> </head> <body> <nav class = "navbar navbar-default"> <div class = "container-fluid"> <a class = "navbar-brand" href = "https://www.sourcecodester.com">Sourcecodester</a> </div> </nav> <div class = "row"> <div class = "col-md-3"> </div> <div class = "col-md-6 well"> <h3 class = "text-primary">OOP PHP CRUD Operation Using MySQLi - Part 2</h3> <hr style = "border-top:1px dotted #000;"/> <form method = "POST" class = "form-inline" action = "create.php"> <div class = "form-group"> <label>Firstname:</label> <input type = "text" id = "firstname" name = "firstname" class = "form-control" required = "required"/> </div> <div class = "form-group"> <label>Lastname:</label> <input type = "text" id = "lastname" name = "lastname" class = "form-control" required = "required"/> </div> <div class = "form-group"> <button name = "save" class = "btn btn-primary"><span class = "glyphicon glyphicon-plus"></span> Add</button> </div> </form> <br /> <table class = "table table-bordered alert-warning table-hover"> <thead> <th>Firstname</th> <th>Lastname</th> <th>Action</th> </thead> <tbody> <?php require 'class.php'; $conn = new db_class(); $read = $conn->read(); while($fetch = $read->fetch_array()){ ?> <tr> <td><?php echo $fetch['firstname']?></td> <td><?php echo $fetch['lastname']?></td> <td><center><a class = "btn btn-warning update_mem_id" data-toggle = "modal" data-target = "#update_modal" name = "<?php echo $fetch['mem_id']?>"><span class = "glyphicon glyphicon-edit"></span> Update</a> | <a class = "btn btn-danger del_mem_id" name = "<?php echo $fetch['mem_id']?>" data-toggle = "modal" data-target="#del_modal"><span class = "glyphicon glyphicon-trash"></span> Delete</a></center></td> </tr> <?php } ?> </tbody> </table> </div> </div> <!-- Modal --> <div class="modal fade" id="del_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-body"> <center><h4 class = "text-danger">Are you sure you want to delete this record?</h4></center> </div> <div class="modal-footer"> <button type = "button" class="btn btn-warning" data-dismiss="modal"><span class = "glyphicon glyphicon-remove"></span> No</button> <button type = "button" class="btn btn-danger del_mem"><span class = "glyphicon glyphicon-trash"></span> Yes</button> </div> </div> </div> </div> <div class="modal fade" id="update_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class = "modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button> <h3 class = "text-success modal-title">Update Member</h3> </div> <form method = "POST" action = "update_mem_query.php"> <div class="modal-body update"> </div> <div class="modal-footer"> <button class="btn btn-warning" name = "update"><span class = "glyphicon glyphicon-edit"></span> Save Changes</button> </div> </form> </div> </div> </div> </body> <script src = "js/jquery-3.1.1.js"></script> <script src = "js/bootstrap.js"></script> <script type = "text/javascript"> $(document).ready(function(){ //Delete $('.del_mem_id').on('click', function(){ $mem_id = $(this).attr('name'); $('.del_mem').on('click', function(){ window.location = "delete_mem.php?mem_id=" + $mem_id; }); }); //Update $('.update_mem_id').on('click', function(){ $mem_id = $(this).attr('name'); $('.update').load('mem_data.php?mem_id=' + $mem_id); }); }); </script> </html>
<?php require_once 'class.php'; $mem_id = $_REQUEST['mem_id']; $conn = new db_class(); $conn->delete($mem_id); ?>
<?php $mem_id = $_REQUEST['mem_id']; require_once 'class.php'; $conn = new db_class(); $fetch = $conn->member_id($mem_id); ?> <div class = "form-group"> <label>Firstname</label> <input type = "text" name = "firstname" value = "<?php echo $fetch['firstname']?>" class = "form-control" /> <input type = "hidden" name = "mem_id" value = "<?php echo $mem_id?>" /> </div> <div class = "form-group"> <label>Lastname</label> <input type = "text" name = "lastname" value = "<?php echo $fetch['lastname']?>" class = "form-control" /> </div>
<?php require_once 'class.php'; $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $mem_id = $_POST['mem_id']; $conn = new db_class(); $conn->update($firstname, $lastname, $mem_id); echo ' <script>alert("Updated Successfully")</script>; <script>window.location = "index.php"</script>; '; } ?>