The SQL code for the student_details table:
CREATE TABLE `student_details` ( `id` int(11) NOT NULL, `name` varchar(100) DEFAULT NULL, `email` varchar(45) DEFAULT NULL, `contact_no` varchar(45) DEFAULT NULL, `gender` varchar(45) DEFAULT NULL, `qualification` varchar(45) DEFAULT NULL, `address` varchar(450) DEFAULT NULL, `create_date` timestamp NULL DEFAULT current_timestamp() ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Here we are utilizing PDO Object-Oriented strategy to Open a Connection to MySQL.
This code for the database connection file.
servername=localhost Username=root Password="" Databasename='student'
dbconnection.php
<?php define('DB_HOST','localhost'); define('DB_USER','root'); define('DB_PASS',''); define('DB_NAME','student'); // Establish database connection. try { $dbconnection = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME,DB_USER, DB_PASS,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'")); } catch (PDOException $e) { exit("Error: " . $e->getMessage()); } ?>
fetech.php
<?php include 'dbconnection.php'; ?> <link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <div class="container"> <div class="row centered-form"> <div class="col-xs-8 "> <div class="panel panel-default"> <div class="panel-heading"> <h3 class="panel-title">Data Fetch From Database Using PHP PDO</h3> </div> <div class="panel-body"> <table class="table table-bordered"> <thead> <tr> <th>Sr.No</th> <th>Name</th> <th>Email</th> <th>Contact-No</th> <th>Gender</th> <th>Qualification</th> <th>Address</th> </tr> </thead> <tbody> <?php $sql ="SELECT id,name,email,contact_no,gender,qualification,address from student_details"; $query= $dbconnection -> prepare($sql); $query-> execute(); $results = $query -> fetchAll(PDO::FETCH_OBJ); $cnt=1; if($query -> rowCount() > 0) { foreach($results as $result) { ?> <tr> <td><?php echo($cnt);?></td> <td><?php echo htmlentities($result->name);?></td> <td><?php echo htmlentities($result->email);?></td> <td><?php echo htmlentities($result->contact_no);?></td> <td><?php echo htmlentities($result->gender);?></td> <td><?php echo htmlentities($result->qualification);?></td> <td><?php echo htmlentities($result->address);?></td> <td><td> <a href="edit.php?id=<?php echo htmlentities($result->id);?>"><button class="btn btn-primary btn-xs">Edit Data</button></a></td> </tr> <?php $cnt=$cnt+1; } } ?> </tbody> </table> </div> </div> </div> </div> </div> <style type="text/css"> body { background-color: #fff; } .centered-form { margin-top: 60px; } .centered-form .panel { background: rgba(255, 255, 255, 0.8); box-shadow: rgba(0, 0, 0, 0.3) 20px 20px 20px; } </style>
update.php
<?php include 'dbconnection.php'; // Get the userid $userid=intval($_GET['id']); $sql = "SELECT id,name,email,contact_no,gender,qualification,address from student_details where id=:uid"; //Prepare the query: $query = $dbconnection->prepare($sql); //Bind the parameters $query->bindParam(':uid',$userid,PDO::PARAM_STR); //Execute the query: $query->execute(); //Assign the data which you pulled from the database (in the preceding step) to a variable. $results=$query->fetchAll(PDO::FETCH_OBJ); // For serial number initialization $cnt=1; if($query->rowCount() > 0) { //In case that the query returned at least one record, we can echo the records within a foreach loop: foreach($results as $result) { $Names = $result->name; $Emaiids = $result->email; $contactno = $result->contact_no; $genders = $result->gender; $eductions = $result->qualification; $addresss = $result->address; } } ?> <?php if(isset($_POST['submit'])) { // Get the userid $userid=intval($_GET['id']); // Posted Values $name = $_POST['name']; $email = $_POST['email']; $mobile = $_POST['contactno']; $gender = $_POST['gender']; $eduction = $_POST['qualification']; $address = $_POST['address']; // Query for Updation $sql="update student_details set name=:names,email=:emailid,contact_no=:mobileno,gender=:gendr,qualification=:eductions,address=:addresss where id=:uid"; //Prepare Query for Execution $query = $dbconnection->prepare($sql); // Bind the parameters $query->bindParam(':names',$name,PDO::PARAM_STR); $query->bindParam(':emailid',$email,PDO::PARAM_STR); $query->bindParam(':mobileno',$mobile,PDO::PARAM_STR); $query->bindParam(':gendr',$gender,PDO::PARAM_STR); $query->bindParam(':eductions',$eduction,PDO::PARAM_STR); $query->bindParam(':addresss',$address,PDO::PARAM_STR); $query->bindParam(':uid',$userid,PDO::PARAM_STR); // Query Execution $query->execute(); // Mesage after updation echo "<script>alert('Record Updated successfully');</script>"; // Code for redirection echo "<script>window.location.href='fetch.php'</script>"; } ?> <link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <div class="container"> <div class="row centered-form"> <div class="col-xs-12 col-sm-8 col-md-4 col-sm-offset-2 col-md-offset-4"> <div class="panel panel-default"> <div class="panel-heading"> <h3 class="panel-title">Update data from Database Using PHP</h3> </div> <div class="panel-body"> <form method="POST"> <div class="form-group"> <label for="name">Name</label> <input type="text" name="name" id="name" class="form-control input-sm" placeholder="Name" required="" value="<?php echo $Names;?>"> </div> <div class="form-group"> <label for="email">Email</label> <input type="email" name="email" id="email" class="form-control input-sm" placeholder="Email Address" required="" value="<?php echo $Emaiids; ?>"> </div> <div class="form-group"> <label for="contactno">Contact No</label> <input type="text" name="contactno" id="contactno" class="form-control input-sm" placeholder="Contact No" required="" maxlength="10" value="<?php echo $contactno; ?>"> </div> <div class="form-group"> <label for="name">Gender</label> <input type="radio" name="gender" value="Male" <?php if ($genders == 'Male') echo 'checked="checked"'; ?>" /> Male <input type="radio" name="gender" value="Male" <?php if ($genders == 'Female') echo 'checked="checked"'; ?>" /> Female </div> <div class="form-group"> <label for="qualification">Qualification</label> <select name="qualification" id="qualification" class="form-control input-sm" placeholder="Qualification" required=""> <option value="NA">--select--</option> <option value="10th"<?php if($eductions=='10th')echo 'selected'?>>10th</option> <option value="12th"<?php if($eductions=='12th')echo 'selected'?>>12th</option> <option value="Graduation"<?php if($eductions=='Graduation')echo 'selected'?>>Graduation</option> </select> </div> <div class="form-group"> <label for="name">Address</label> <textarea name="address" id="address" class="form-control input-sm" required=""><?php echo $addresss; ?></textarea> </div> <input type="submit" name="submit" value="Update" class="btn btn-info btn-block"> </form> </div> </div> </div> </div> </div> <style type="text/css"> body{ background-color: #fff; } .centered-form{ margin-top: 60px; } .centered-form .panel{ background: rgba(255, 255, 255, 0.8); box-shadow: rgba(0, 0, 0, 0.3) 20px 20px 20px; } </style>
How To Data Update From Database Using PHP PDO,MYSQL With Source Code
Size: 3kb
Version: 2.1
31 Downloads
Leave a Comment