Here i have Write Oops Config file in Core PHP
<?PHP
session_start();
class Database
{
private $folder_name;
private $host_name;
private $user_name;
private $password;
private $db_name;
private $con;
private $result;
function __construct()
{
if($_SERVER['HTTP_HOST'] == 'localhost'){ //(if we will use it in local server xampp)
$this->folder_name = '/test/';
$this->host_name = "127.0.0.1";
$this->user_name = "root";
$this->password = "";
$this->db_name = "test";
}else{
$this->folder_name = "/test"; //(if we will use it in Hosting server)
$this->host_name = "127.0.0.1";
$this->user_name = "sharique";
$this->password = "sharique";
$this->db_name ="test";
}
$this->con = mysql_connect($this->host_name, $this->user_name, $this->password)
or die("Couldn't connect to the database".mysql_error());
mysql_select_db($this->db_name,$this->con);
}
public function execute($query){
$this->result = mysql_query($query, $this->con);
}
public function getResult(){
return mysql_fetch_assoc($this->result);
}
public function getResults(){ $return = array(); while ($row = mysql_fetch_assoc($this->result)) { $return[]=$row; } return $return; } public function rowCount(){ return mysql_num_rows($this->result); } public function LastId() { return mysql_insert_id(); } public function affectedRows(){ return mysql_affected_rows(); }
}
if($_SERVER["HTTP_HOST"]=="192.168.1.194:8081"){
$folder_name='/Testing/';
} else {
$folder_name='/';
}
$root = 'http//'.$_SERVER['HTTP_HOST'].$folder_name;
$doc_root =$_SERVER['DOCUMENT_ROOT'].$folder_name;
$css_root = $root. 'css/';
$script_root=$root.'js/';
$lib_root = $root.'lib/';
$include_root = $root.'include/';
$images_root = $root.'images/';
date_default_timezone_set('Asia/Calcutta');
$date=date('Y-m-d H:i:s');
$modified_date=date('d-M-Y, D');
$site_title="Testing";
define("WEB_ROOT",$root);
define("DOC_ROOT",$doc_root);
define("CURR_DATE",$date);
define("MODIFIED_DATE",$modified_date);
define("CSS_ROOT",$css_root);
define("LIB_ROOT",$lib_root);
define("INCLUDE_ROOT",$include_root);
define("SCRIPT_ROOT",$script_root);
define("IMAGES_ROOT",$images_root);
define("SITE_TITLE",$site_title);
?>
PHP Config page using mysqli
CONFIG.PHP
<?php
$servername = 'localhost'; $username = 'root'; $password = '';
$dbname = 'databasename';
$connection=mysqli_connect($servername, $username, $password, "$dbname");
if(!$connection) { die('Could not Connect MySql Server:' .mysql_error()); }
/*$mysqli = new mysqli("localhost","root","","databasename"); if ($mysqli->connect_errno) { printf("Connect failed: %s\n", $mysqli->connect_error); exit(); } */
?>
INDEX.PHP
<h2 class="title">Registration Info</h2>
<form method="POST" action="insert.php" name="insertform"> <div class="input-group">
<input class="input--style-1" type="text" placeholder="NAME" name="name" id="name">
</div>
<div class="input-group">
<h1>Show Checkboxes</h1> <br>
<input type="checkbox" name="vcl[]" value="Bike"> <label for="vcl"> I have a bike</label><br> <input type="checkbox" name="vcl[]" value="Car"> <label for="vcl"> I have a car</label><br> <input type="checkbox" name="vcl[]" value="Boat"> <label for="vcl"> I have a boat</label><br><br> </div>
<div class="row row-space">
<div class="col-2">
<div class="input-group">
<input class="input--style-1 js-datepicker" type="text" placeholder="BIRTHDATE" id="birthday" name="birthday"> <i class="zmdi zmdi-calendar-note input-icon js-btn-calendar"></i> </div>
</div>
<div class="col-2">
<div class="input-group">
<div class="rs-select2 js-select-simple select--no-search"> <select name="gender">
<option disabled="disabled" selected="selected">GENDER</option> <option>S</option> <option>M</option> <option>F</option> <option>O</option>
</select>
<div class="select-dropdown"></div> </div>
</div>
</div>
<div class="col-2"> <tr> <!-- checked radio button -->
<td align="left">User Gender : </td>
<td>
<input type="radio" name="user_gender" value="Male"> Male
<input type="radio" name="user_gender" value="Female"> Female <input type="radio" name="user_gender" value="Other"> Other </td>
</tr>
</div>
<div class="col-2">
<div class="input-group">
<tr>
<td>Hobbies</td>
<td> <input type="checkbox" name="hobbies[]" value="cricket"/>Cricket <input type="checkbox" name="hobbies[]" value="football"/>football <input type="checkbox" name="hobbies[]" value="tenis"/>Tenis <input type="checkbox" name="hobbies[]" value="vollyball"/>Vollyball </td>
</tr> </div> </div> </div>
<div class="input-group">
<div class="rs-select2 js-select-simple select--no-search"> <select name="class"> <option disabled="disabled" selected="selected">CLASS</option> <option>Class 1</option>
<option>Class 2</option> <option>Class 3</option>
</select> <div class="select-dropdown"></div> </div> </div> <div class="row row-space"> <div class="col-2"> <div class="input-group"> <input class="input--style-1" type="text" placeholder="REGISTRATION CODE" id="res_code" name="res_code"> </div> </div> </div> <div class="p-t-20"> <button name="send" id="send" class="btn btn--radius btn--green" type="submit">Submit</button> </div> </form> <br> <?php echo "<table border=1>"; echo "<tr>"; echo "<td>ID</td>"; echo "<td>NAME</td>"; echo "<td>DOB</td>"; echo "<td>Gen</td>"; echo "<td>Cls</td>"; echo "<td>RG No</td>"; echo "<td>Hb</td>"; echo "<td>CHK</td>"; echo "<td>Usr Gen</td>"; echo "<td colspan=2>Act</td>"; echo "</tr>"; echo "</thead>"; if ($regis = $connection-> query("SELECT * FROM registration ORDER BY id ASC")){ $i =1; foreach ($regis as $registration) { ?> <tbody> <tr> <th scope="row"><?php echo $i++; ?></th> <td><?php echo $registration['name']; ?></td> <td><?php echo $registration['birthday']; ?></td> <td><?php echo $registration['gender']; ?></td> <td><?php echo $registration['class']; ?></td> <td><?php echo $registration['res_code']; ?></td> <td><?php echo $registration['hobbies']; ?></td> <td><?php echo $registration['vcl']; ?></td> <td><?php echo $registration['user_gender']; ?></td> <td> <a href="edit.php?id=<?php echo $registration['id']; ?>&edit=Y"><i class="fa fa-pencil-square-o" aria-hidden="true"></i></a> </td>   <td><a href="delete.php?id=<?php echo $registration['id']; ?>&del=y" onClick="return confirm('Do you really want to delete this ?');"><i class="fa fa-trash-o" aria-hidden="true"></i></a> </td> </tr> <?php } $regis->close(); } $connection->close(); ?> </tbody> </table> </div> </div> </div> </div> <!-- Jquery JS--> <script src="vendor/jquery/jquery.min.js"></script> <!-- Vendor JS--> <script src="vendor/select2/select2.min.js"></script> <script src="vendor/datepicker/moment.min.js"></script> <script src="vendor/datepicker/daterangepicker.js"></script> <!-- Main JS--> <script src="js/global.js"></script> <script type="text/javascript"> /*$("#send").click(function(e){ var name = $("#name"); var birthday = $("#birthday"); var gender = $("gender"); //var class = $("class"); var res_code = $("res_code"); if(!name.val()){ $("#name").css("border", "1px solid red"); $("#name").focus(); e.preventDefault(); } if(!birthday.val()){ $("#birthday").css("border", "1px solid red"); $("#birthday").focus(); e.preventDefault(); } if(!gender.val()){ $("#gender").css("border", "1px solid red"); $("#gender").focus(); e.preventDefault(); } if(!res_code.val()){ $("#res_code").css("border", "1px solid red"); $("#res_code").focus(); e.preventDefault(); } */ //}); </script> </body><!-- This templates was made by Colorlib (https://colorlib.com) -->
INSERT.PHP
include_once 'config.php';
$name = $_POST['name'];
$birthday = $_POST['birthday'];
$gender = $_POST['gender'];
$class = $_POST['class'];
$res_code = $_POST['res_code'];
$a = $_POST['hobbies'];
$hobbies = implode(",", $a);
$c = $_POST['vcl'];
$vcl = implode(",", $c);
$user_gender = $_POST['user_gender'];
query ("INSERT INTO registration (name,birthday,gender,class,res_code,hobbies,vcl,user_gender) VALUES ('$name','$birthday','$gender','$class','$res_code','$hobbies','$vcl','$user_gender') "); if($insert) { $msg="Inserted"; echo "alert('$msg');
"; header('Location:index.php'); } } else { $errormsg="Error !!! Coba Lagi !!"; echo "alert('$errormsg');"; header('Location:index.php'); } $connection->close(); ?>
EDIT.PHP
include_once 'config.php';
if(isset($_GET['id']))
{
$id=$_GET['id'];
if(isset($_POST['update']))
{
$name=$_POST['name'];
$birthday=$_POST['birthday'];
$gender=$_POST['gender'];
$class=$_POST['class'];
$res_code=$_POST['res_code'];
$b = $_POST['hobbies']; $hobbies=implode(",", $b); $d = $_POST['vcl']; $vcl=implode(",", $d); $user_gender = $_POST['user_gender'];
query ("UPDATE registration SET name='$name',vcl='$vcl', birthday='$birthday', gender='$gender', class='$class', res_code='$res_code', hobbies='$hobbies',user_gender='$user_gender' WHERE id='$id'"); //echo $update_query;die; if($update_query) { echo " alert('Rows are updated successfully');
"; echo "window.location='index.php?msg=Sent sucessfully.';
"; $msg="Secessfully Updated"; //echo ''; //header('Location:index.php'); } } } ?> <?php if(isset($_GET['id'])) { $id = $_GET['id']; $test_query = $connection->query ("SELECT * from registration WHERE id='$id'"); //print_r($test_query);die; while($data_fetch = $test_query->fetch_array(MYSQLI_BOTH)) { //print_r($data_fetch);die; $name = $data_fetch['name']; //print_r($name);die; $birthday = $data_fetch['birthday']; $gender = $data_fetch['gender']; $class = $data_fetch['class']; $res_code = $data_fetch['res_code']; $a = $data_fetch['hobbies']; $hobbies = explode(",",$a); $c = $data_fetch['vcl']; $vcl = explode(",",$c); $user_gender = $data_fetch['user_gender']; //print_r($user_gender);die; ?> <h2>Updating Data</h2> <form action="" method="post" name="insertform"> <table> <tr> <td>Name</td> <td>:</td> <td><input type="text" name="name" required placeholder="name" value="<?php echo $name;?>"></td> </tr> <tr> <td>DOB</td> <td>:</td> <td><input type="text" name="birthday" required placeholder="birthday" value="<?php echo $birthday;?>"></td> </tr> <tr> <td>Gender</td> <td>:</td> <td> <?php $test_gender = array("S","M","F","O" ); ?> <select name="gender" required> <?php foreach ($test_gender as $gender_val ) { $select = ""; if($gender_val == $data_fetch["gender"]){ $select = "selected"; } } ?> <option value="O" <?php if ($data_fetch['gender'] == 'O') echo 'selected="selected"'; ?>>O </option> <option value="M" <?php if ($data_fetch['gender'] == 'M') echo 'selected="selected"'; ?>>M </option> <option value="F" <?php if ($data_fetch['gender'] == 'F') echo 'selected="selected"'; ?>>F </option> </select> </td> </tr> <tr> <td>Hobbies</td> <td> <input type="checkbox" name="hobbies[]" value='cricket' / <?php if (in_array("cricket", $hobbies)){ echo "checked"; }else { echo "";} ?>>Cricket <input type="checkbox" name="hobbies[]" value='football' / <?php if (in_array("football", $hobbies)){ echo "checked"; }else { echo "";} ?>>football <input type="checkbox" name="hobbies[]" value='tenis' / <?php if (in_array("tenis", $hobbies)){ echo "checked"; } else { echo ""; } ?>>Tenis <input type="checkbox" name="hobbies[]" value='vollyball' / <?php if (in_array("vollyball", $hobbies)){ echo "checked"; } else { echo ""; } ?>>Vollyball </td> </tr> <tr> <td>Vechile</td> <td> <input type="checkbox" name="vcl[]" value='bike' / <?php if (in_array("bike", $vcl)){ echo "checked"; } ?>>Bike <input type="checkbox" name="vcl[]" value='car' / <?php if (in_array("car", $vcl)){ echo "checked"; } ?>>Car <input type="checkbox" name="vcl[]" value='boat' / <?php if (in_array("boat", $vcl)){ echo "checked"; } ?>>Boat </td> </tr> <!-- checked radio button --> <tr> <td align="left"> Usr Gender : </td> <td> <?php $radio1 = ""; $radio2 = ""; $radio3 = ""; if($data_fetch["user_gender"] == "Male"){ $radio1 = "checked"; }elseif($data_fetch["user_gender"] == "Female"){ $radio2 = "checked"; }elseif ($data_fetch["user_gender"] == "Other") { $radio3 = "checked"; } ?> <input type="radio" name="user_gender" value="Male" <?php echo $radio1; ?>/> Male <input type="radio" name="user_gender" value="Female" <?php echo $radio2; ?>/> Female <input type="radio" name="user_gender" value="Other" <?php echo $radio3; ?>/> Other </td> </tr> <tr> <td>class</td> <td>:</td> <td><input type="text" name="class" required placeholder="" value="<?php echo $class;?>"></td> </tr> <tr> <td>class</td> <td>:</td> <td><input type="text" name="res_code" required placeholder="" value="<?php echo $res_code;?>"></td> </tr> </table> <input type="submit" name="update" value="Update"/> <input type="button" name="cancel" value="cancel" onClick="window.location.href='index.php';" /> </p> </form> <?php } } ?>