connection
<?php
error_reporting(0);
// ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$con= mysqli_connect("localhost","root","","test") or die("falied connection");
// if($con){ echo "connection done"; }
// Check connection
if($con === false){ die("ERROR: Could not connect. " . mysqli_connect_error());}
insert
if(isset($_POST['save'])){
// print_r($_POST);
if($_GET['uid']==''){
$name=$_POST['name'];
// echo $sql="INSERT INTO `simplecrud` ( `crud_name`) VALUES ('mohal')";// echo $sql= "INSERT INTO `simplecrud` (`crud_name`) VALUES ('$name') ";
$sql= "INSERT INTO `simplecrud` (`crud_name`) VALUES('".$_POST["name"]."')";// exit();
if(mysqli_query($con, $sql)){
echo "Records inserted successfully.";}} else{ echo die( mysqli_error($con));} }
fetch data
if((@$_GET['uid'])){
$fetsql="SELECT *FROM `simplecrud` WHERE crud_id='".$_GET['uid']."'";
$runfet=mysqli_query($con,$fetsql) ;
$editData=mysqli_fetch_assoc($runfet);
$editData['crud_id'];
}
update
if(isset($_POST['update'])){
$upsql="UPDATE simplecrud
SET crud_name='".$_POST['name']."' WHERE crud_id= '".$_GET['uid']."' ";
; if(mysqli_query($con,$upsql)){
echo "Record successfully updated"; }
else{ echo "update failed"; } }
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Php crud using mysql </title>
</head>
<body>
<div class="heading">
<h1>simple crud</h1>
</div>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
<label for="">name</label><br>
<input type="text" hidden="crud_id" id="name" value="">
<input type="text" name="name" id="name" value="<?php echo @$editData['crud_name'] ?>">
<?php if($_GET['uid']==''){echo ' <button type="submit" class="form-submit" name="save">Submit</button>';}else
{echo ' <button type="submit" class="form-submit" name="update">update</button>';} ?>
</form> <br><br>
<!-- display data -->
<table border="1" width="50%">
<thead>
<tr>
<th>Id</th>
<th>NAME</th>
<th colspan="2">OPERATION</th>
</tr>
</thead>
<tbody>
<?php
$disql="SELECT *FROM `simplecrud`";
$rundisql=mysqli_query($con,$disql)or die(mysqli_error($con));
if(mysqli_num_rows($rundisql)>0){
// echo 'data found in database';
foreach($rundisql as $key=>$value){
// echo $value['crud_name'].'<br>';
echo '<tr align="center">
<td> '.$value['crud_id'].'</td>
<td> '.$value['crud_name'].'</td>
<td><a href="index.php?uid='.$value['crud_id'].'">Edit</a>
</td><td>
<a href="index.php?del='.$value['crud_id'].'">Delete</a></td>
<tr>';
}
}else{
echo 'data not found in database'; } ?>
</tbody>
</table>
<?php
// Close connection
mysqli_close($con); ?>
</body>
</html>