I have insert and select query and just need help with update query:please help!!
<?php
/**
* Created by PhpStorm.
* User: Mo
* Date: 29/06/2016
* Time: 05:06 PM
*/
class Database
{
private $host;
private $username;
private $password;
private $dbname;
private $link;
function __construct()
{
$this->host = 'localhost';
$this->username = 'root';
$this->password = '123';
$this->dbname = 'school';
}
/**
*
*/
public function connect()
{
$this->link = mysqli_connect($this->host, $this->username, $this->password, $this->dbname);
if (!$this->link) {
die('Cannot connect to database!');
}
}
public function disconnect()
{
mysqli_close($this->link);
}
public function insert($table, $columns, $values)
{
if (count($columns) != count($values))
return false;
$cols = implode(',', $columns);
$vals = implode('","', $values);
$vals = '"' . $vals . '"';
$sql = "insert into $table ($cols) values ($vals)";
$this->connect();
mysqli_query($this->link, $sql);
$this->disconnect();
}
//Select
public function select($table, $columns = array(), $condition = array())
{
$this->connect();
$where = '1 = 1 ';
foreach ($condition as $row){
$str = $row['type'].' '.$row['col']. ' '.$row['cnd']. ' "'.$row['value'].'" ';
$where .= $str;
}
$cols = '';
if(empty($columns)) $cols = '*';
else $cols = implode(',', $columns );
$sql_select = "select $cols from $table where $where" ;
$this->connect();
$result = mysqli_query($this->link, $sql_select);
$rows = array();
while($row = mysqli_fetch_array($result)) {
$rows[] = $row;
}
$this->disconnect();
return $rows;
}
//Delete
public function delete($table, $condition = array())
{
$this->connect();
$where = '1 = 1 ';
foreach ($condition as $row){
$str = $row['type'].' '.$row['col']. ' '.$row['cnd']. ' "'.$row['value'].'" ';
$where .= $str;
}
$sql_del = "delete from $table where $where ";
if(mysqli_query($this->link, $sql_del))
$result = true;
else
$result = false;
$this->disconnect();
return $result;
}
//Update
public function update($table, $data, $where)
{
$cols = array();
foreach($data as $key=>$val) {
$cols[] = "$key = '$val'";
}
$sql_update = "update $table set " . implode(', ', $cols) . " where $where";
return($sql_update);
}
public function query($sql){
$this->connect();
$result = mysqli_query($this->link, $sql);
$this->disconnect();
return $result;
}
}