Skip to content

Instantly share code, notes, and snippets.

@incubs
Created January 19, 2017 15:45
Show Gist options
  • Save incubs/79cc4dcdeb76626fcbc00324277d8927 to your computer and use it in GitHub Desktop.
Save incubs/79cc4dcdeb76626fcbc00324277d8927 to your computer and use it in GitHub Desktop.
oop data save, update and delete
///classes .....................
<?php
class saveusers
{
protected $name;
protected $message;
protected $password;
protected $id;
public function saveuUserInfo()
{
$name =$this->getName();
$message=$this->getMessage();
$password=$this->getPassword();
$connect= new connect();
$mysql=$connect->getconnection();
$query = "insert into users(name,message,password) values('$name','$message','$password')";
//die($query);
mysqli_query( $mysql,$query);
}
public function updateuser()
{
$name =$this->getName();
$message=$this->getMessage();
$password=$this->getPassword();
$connect= new connect();
$mysql=$connect->getconnection();
$query = "update users set name='$name',message='$message',password='$password' where id=".$this->getId();
// die($query);
mysqli_query( $mysql,$query);
}
public function deletedata()
{
$connect= new connect();
$mysql=$connect->getconnection();
$query="delete from users where id=".$this->getId();
mysqli_query($mysql,$query);
}
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @param mixed $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* @param mixed $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* @return mixed
*/
public function getMessage()
{
return $this->message;
}
/**
* @param mixed $message
*/
public function setMessage($message)
{
$this->message = $message;
}
/**
* @return mixed
*/
public function getPassword()
{
return $this->password;
}
/**
* @param mixed $password
*/
public function setPassword($password)
{
$this->password = $password;
}
}
next class.......................
<?php
/**
* Created by PhpStorm.
* User: Rabin
* Date: 1/17/2017
* Time: 7:36 PM
*/
class showusers extends connect
{
protected $table;
protected $id;
public function getdata()
{
$connect=new self();
$mysql=$connect->getconnection();
$query='select * from '.$this->getTable();
$result= mysqli_query($mysql,$query);
$users=[];
while ($row=mysqli_fetch_assoc($result))
{
$data=new saveusers();
$data->setName($row['name']);
$data->setPassword($row['password']);
$data->setMessage($row['message']);
$data->setId($row['id']);
$users[]=$data;
}
//var_dump($data);
return $users;
}
public function showuserinfo()
{
$connect=new connect();
$mysql=$connect->getconnection();
$query="select * from users where id=".$this->getId();
$result=mysqli_query($mysql,$query);
$row=mysqli_fetch_assoc($result);
$data=new saveusers();
$data->setName($row['name']);
$data->setPassword($row['password']);
$data->setMessage($row['message']);
$data->setId($row['id']);
// echo "<pre>";
// var_dump($data);
// die;
return $data;
}
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @param mixed $id
*/
public function setId($id)
{
$this->id = $id;
}
public function getTable()
{
return $this->table;
}
/**
* @param mixed $table
*/
public function setTable($table)
{
$this->table = $table;
}
}
next classs........................... for connection
<?php
class connect
{
protected $host;
protected $user;
protected $password;
protected $database;
public function getconnection()
{
$this->setHost('localhost');
$this->setUser('root');
$this->setPassword('');
$this->setDatabase('leapfrog');
return mysqli_connect($this->host,$this->user,$this->password,$this->database);
}
/**
* @return mixed
*/
public function getHost()
{
return $this->host;
}
/**
* @param mixed $host
*/
public function setHost($host)
{
$this->host = $host;
}
/**
* @return mixed
*/
public function getUser()
{
return $this->user;
}
/**
* @param mixed $user
*/
public function setUser($user)
{
$this->user = $user;
}
/**
* @return mixed
*/
public function getPassword()
{
return $this->password;
}
/**
* @param mixed $password
*/
public function setPassword($password)
{
$this->password = $password;
}
/**
* @return mixed
*/
public function getDatabase()
{
return $this->database;
}
/**
* @param mixed $database
*/
public function setDatabase($database)
{
$this->database = $database;
}
}
.................for saving and listing data ..........................
<html>
<head>
<title> display </title>
<link rel="stylesheet" href="css/bootstrap.css">
</head>
<body>
<?php
include 'vendor/autoload.php';
..............save data.................
if (isset($_POST['save'])) {
$user = new saveusers();
$user->setName($_POST['name']);
$user->setPassword($_POST['password']);
$user->setMessage($_POST['message']);
$user->saveuUserInfo();
}
..............show data............
$show=new showusers();
$show->setTable('users');
$result=$show->getdata();
//echo "<pre>";
//var_dump($result);
//die;
//echo "<pre>";
//var_dump($result);
?>
<div class="container">
<table class="table-condensed table-bordered table-hover">
<tr>
<td>Name</td>
<td>Password</td>
<td>Message</td>
</tr>
<?php foreach ($result as $data ){ ?>
<tr>
<td><?php echo $data->getname(); ?></td>
<td><?php echo $data->getpassword();?></td>
<td><?php echo $data->getMessage();?></td>
<td>
<div class="form-group" >
<a href="delete.php?id=<?php echo $data->getid();?>" class="btn btn-info">Delete</a>
</div>
</td><td>
<div class="form-group" >
<a href="update.php?id=<?php echo $data->getid();?>" class="btn btn-info"> Update</a>
</div>
</td>
<?php } ?>
</tr>
</table>
</div>
<?php
?>
</body>
</html>
......................for deleting data.......................
<?php
include 'vendor/autoload.php';
$user=new saveusers();
$user->setid($_GET['id']);
$user->deletedata();
header('location:save.php');
...............for updating data ..............................
...............showing data for update........
<?php
include 'vendor/autoload.php';
$show=new showusers();
$show->setId($_GET['id']);
$row=$show->showuserinfo();
//echo "<pre>";
//var_dump($row);
//die;
if (!empty($row)){
?>
<html>
<head>
<title>Form</title>
<link rel="stylesheet" href="css/bootstrap.css">
</head>
<body>
<div class="container">
<div class="row">
<h1>Form</h1>
<form action="saveupdate.php" class="form" method="get">
<div class="form-group">
<label for="name">Name</label>
<input type="text" name="name"class="form-control" value="<?php echo $row->getName(); ?>">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" name="password" class="form-control" value="<?php echo $row->getPassword();?>">
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea name="message" id="" cols="30" rows="10" class="form-control"><?php echo $row->getMessage(); ?></textarea>
</div>
<div class="form-group" >
<input name="id" type="hidden" value="<?php echo $row->getId(); ?>"/>
<button class="btn btn-info" name="update" >Update</button>
</div>
</form>
</div>
</div>
</body>
</html>
<?php } ?>
....................update ...............
<?php
include 'vendor/autoload.php';
$update=new saveusers();
$update->setId($_GET['id']);
$update->setName($_GET['name']);
$update->setMessage($_GET['message']);
$update->setPassword($_GET['password']);
$update->updateuser();
header('location:save.php');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment