[go: up one dir, main page]

0% found this document useful (0 votes)
30 views8 pages

PHP CRUD Mysqli

This document outlines a PHP CRUD (create, read, update, delete) application for managing book data in a MySQL database. It includes code snippets for connecting to the database, inserting new book records, displaying all records in a table, editing existing records, and deleting records. The application allows users to add, view, update and remove book data from the database through a simple PHP web form interface.

Uploaded by

notope8438
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views8 pages

PHP CRUD Mysqli

This document outlines a PHP CRUD (create, read, update, delete) application for managing book data in a MySQL database. It includes code snippets for connecting to the database, inserting new book records, displaying all records in a table, editing existing records, and deleting records. The application allows users to add, view, update and remove book data from the database through a simple PHP web form interface.

Uploaded by

notope8438
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

PHP CRUD using MYSQLI

-> Database Connection

<?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>

<td><?php echo $row['page'];?></td>


<td><?php echo $row['price'];?></td>
<td>
<a href="edit.php?editid=<?php echo $row['id'];?>" class="btn btn-
success">Edit</a>
<a href="delete.php?delid=<?php echo $row['id'];?>" onclick="return
confirm('Do you really want to delete?');" class="btn btn-danger">Delete</a>
</td>
</tr>
</tbody>

<?php
$cnt = $cnt + 1;
}
}
?>

</table>
</div>
</body>
</html>
--------------------------------------------------------------------------------------------------------------------------

-> Edit Page


<?php
include_once('config/db.php');
if(isset($_POST['submit'])){
$eid = $_GET['editid'];

$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">

<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" value="<?php echo $row['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" 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>";
}
?>

You might also like