So I got this code, it is a PHP script that communicates my database with my application. But, somehow the update function isn't working at all and I can't figure out what's wrong - though I'm 99% sure it isn't the application itself.
Here's the whole php scipt
<?php
class DataBase
{
private $server;
private $user;
private $password;
private $database;
function __construct($server, $user, $password, $database)
{
$this->server = $server;
$this->user = $user;
$this->password = $password;
$this->database = $database;
}
private function connect()
{
$connect = mysqli_connect($this->server, $this->user, $this->password) or die('Error, no se ha podido conectar.');
mysqli_select_db($connect, $this->database);
return $connect;
}
private function diconnect($connect)
{
return mysqli_close($connect);
}
private function wheres($wheres)
{
$w = '';
foreach ($wheres as $where => $key)
{
$w .= ' `'.$where.'` = "'.$key.'"';
}
return $w;
}
private function sets($sets)
{
$w = '';
foreach ($sets as $set => $key)
{
$w .= ' `'.$set.'` = "'.$key.'",';
}
return substr($w, 0, -1);
}
public function select($table, $wheres = null)
{
$connect = $this->connect();
if ($wheres == null)
{
$query = mysqli_query($connect, 'SELECT * FROM `'.$table.'`');
} else {
$query = mysqli_query($connect, 'SELECT * FROM `'.$table.'` WHERE '.$this->wheres($wheres));
}
$i = 0;
$ret = array();
while ($row = mysqli_fetch_assoc($query)) {
foreach ($row as $key => $value) {
$ret[$i][$key] = $value;
}
$i++;
}
return ($ret);
}
public function insert($table, $inserts)
{
$connect = $this->connect();
$values = array_values($inserts);
$keys = array_keys($inserts);
return $query = mysqli_query($connect, 'INSERT INTO `'.$table.'` (`'.implode('`,`', $keys).'`) VALUES (\''.implode('\',\'', $values).'\')');
}
public function delete($table, $wheres = null)
{
$connect = $this->connect();
if ($wheres == null)
{
return $query = mysqli_query($connect, 'DELETE FROM `'.$table.'`');
} else {
return $query = mysqli_query($connect, 'DELETE FROM `'.$table.'` WHERE '.$this->wheres($wheres));
}
}
public function update($table, $id, $sets = null)
{
$connect = $this->connect();
return $query = mysqli_query($connect, 'UPDATE `'.$table.'` SET'.$this->sets($sets).' WHERE `id` = "'.$id.'"');
}
}
if (!empty($_POST))
{
if (isset($_POST['method']))
{
if (isset($_POST['table']))
{
$DataBase = new DataBase('127.0.0.1', 'root', 'password', 'coord_tic');
switch ($_POST['method'])
{
case 'insert':
$inserts = $_POST;
unset($inserts['table']);
unset($inserts['method']);
echo json_encode($DataBase->insert($_POST['table'], $inserts));
break;
case 'delete':
$wheres = $_POST;
unset($wheres['table']);
unset($wheres['method']);
echo json_encode($DataBase->delete($_POST['table'], $wheres));
break;
case 'select':
$wheres = $_POST;
unset($wheres['table']);
unset($wheres['method']);
echo json_encode($DataBase->select($_POST['table'], $wheres));
break;
case 'update':
$wheres = $_POST;
unset($wheres['table']);
unset($wheres['method']);
unset($wheres['id']);
echo json_encode($DataBase->update($_POST['table'], $_POST['id'], $wheres));
break;
default:
echo json_encode(false);
break;
}
} else {
echo json_encode(false);
}
} else {
echo json_encode(false);
}
} else {
echo json_encode(false);
}
I really hope you can help me because my work depends on these right now,
big thanks!