PHP CRUD Mysqli
PHP CRUD Mysqli
<?php
define('DB_SERVER','localhost');
define('DB_USERNAME','root');
define('DB_PASSWORD','');
define('DB_DATABASE','bookdb');
$dbcon = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
if(!$dbcon){
die ('Connect Failed: '.mysqli_connect_error());
}
?>
--------------------------------------------------------------------------------------------------------------------------
-> Create and View Page
<?php
include_once('config/db.php');
if(isset($_POST['submit'])){
$bname = $_POST['bname'];
$writer = $_POST['writer'];
$page = $_POST['page'];
$price = $_POST['price'];
$sql = "INSERT INTO details(bname,writer,page,price)
VALUE('$bname','$writer','$page','$price')";
$query = mysqli_query($dbcon,$sql);
if($query){
echo "<script>alert('Data Successfully Inserted.');</script>";
echo "<script>window.location='index.php';</script>";
}else{
echo "<script>alert('Something Went Wrong. Try Again!!!');</script>";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Main Page</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<center><h2>Book Form</h2></center><br>
<form class="form-horizontal" method="post">
<div class="form-group">
<label class="control-label col-sm-4" for="book">Book Name:</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="book" placeholder="Enter Book Name"
name="bname" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-4" for="writer">Writer:</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="writer" placeholder="Enter Writer Name"
name="writer" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-4" for="page">Pages:</label>
<div class="col-sm-5">
<input type="number" class="form-control" id="page" placeholder="Enter Pages"
name="page" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-4" for="price">Price (in Rs):</label>
<div class="col-sm-5">
<input type="number" class="form-control" id="price" placeholder="Enter Book Price"
name="price" required>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-4 col-sm-5">
<button type="submit" class="btn btn-primary" name="submit">Submit</button>
</div>
</div>
</form>
</div><br><br>
<!--- VIEW --->
<div class="container">
<center><h2>View Books</h2></center>
<table class="table table-striped">
<thead>
<tr>
<th>Sl No</th>
<th>Book Name</th>
<th>Writer Name</th>
<th>Pages</th>
<th>Price</th>
<th>Actions</th>
</tr>
</thead>
<?php
$cnt = 1;
$sql = "SELECT * FROM details";
$query = mysqli_query($dbcon,$sql);
$row = mysqli_num_rows($query);
if($row > 0){
while($row=mysqli_fetch_array($query)){
?>
<tbody>
<tr>
<td><?php echo $cnt;?></td>
<td><?php echo $row['bname'];?></td>
<td><?php echo $row['writer'];?></td>
<?php
$cnt = $cnt + 1;
}
}
?>
</table>
</div>
</body>
</html>
--------------------------------------------------------------------------------------------------------------------------
$bname = $_POST['bname'];
$writer = $_POST['writer'];
$page = $_POST['page'];
$price = $_POST['price'];
$sql = "UPDATE details SET bname='$bname',writer='$writer',page='$page',price='$price'
WHERE id='$eid'";
$query = mysqli_query($dbcon,$sql);
if($query){
echo "<script>alert('Data Successfully Updated.');</script>";
echo "<script>window.location='view.php';</script>";
}else{
echo "<script>alert('Something Went Wrong. Try Again!!!');</script>";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Main Page</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container" style="margin-top: 150px;">
<center><h2>Book Form</h2></center><br>
<form class="form-horizontal" method="post">
<?php
$eid = $_GET['editid'];
$seq = "SELECT * FROM details WHERE id='$eid'";
$query1 = mysqli_query($dbcon,$seq);
while($row = mysqli_fetch_array($query1)){
?>
<div class="form-group">
<label class="control-label col-sm-4" for="book">Book Name:</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="book" placeholder="Enter Book Name"
name="bname" value="<?php echo $row['bname'];?>" required>
</div>
</div>
<div class="form-group">
<div class="form-group">
<label class="control-label col-sm-4" for="page">Pages:</label>
<div class="col-sm-5">
<input type="number" class="form-control" id="page" placeholder="Enter Pages"
name="page" value="<?php echo $row['page'];?>" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-4" for="price">Price (in Rs):</label>
<div class="col-sm-5">
<input type="number" class="form-control" id="price" placeholder="Enter Book Price"
name="price" value="<?php echo $row['price'];?>" required>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-4 col-sm-5">
<button type="submit" class="btn btn-primary" name="submit">Update</button>
</div>
</div>
<?php
}
?>
</form>
</div>
</body>
</html>
--------------------------------------------------------------------------------------------------------------------------
-> Delete Page
<?php
include_once('config/db.php');
if(isset($_GET['delid'])){
$did = $_GET['delid'];
$sql = "DELETE FROM details WHERE id='$did'";
$query = mysqli_query($dbcon,$sql);
echo "<script>alert('Data Successfully Deleted.');</script>";
echo "<script>window.location='view.php';</script>";
}
?>