Skip to content

Instantly share code, notes, and snippets.

@basicssm
Created May 19, 2015 08:09
Show Gist options
  • Save basicssm/c8bf56d4d6d0fa0a1a7e to your computer and use it in GitHub Desktop.
Save basicssm/c8bf56d4d6d0fa0a1a7e to your computer and use it in GitHub Desktop.
<?php
require_once(__DIR__.'/../lib/settings.php');
require_once(__DIR__.'/../lib/conexion.php');
if(isset($_POST['table'])){
$result = new stdClass();
$table = $_POST['table'];
//This post parameter indicates whether the operation is an insert or update
//If there is the id, the item is already stored, so that your data will be updated and not inserted again
//Queries are generated dynamically
if($_POST[$table.'_iddb'] == ''){
//INSERT
$keys = '';
$values = '';
foreach($_POST as $key => $val){
if(str_replace($table.'_','',$key) != 'iddb' && $key != 'table'){
if(str_replace($table.'_','',$key)=='activo' && $val=='on'){
$val = 1;
}
$keys .= str_replace($table.'_','',$key).', ';
$values .= '"'.$val.'", ';
}
}
$keys = substr($keys,0,-2);
$values = substr($values,0,-2);
$query = 'INSERT INTO '.$table.' ('.$keys.') VALUES ('.$values.');';
}
else{
//UPDATE
$id = $_POST[$table.'_iddb'];
$pares = '';
if(!isset($_POST[$table.'_activo'])){ $pares .= 'activo = 0, '; }
foreach($_POST as $key => $val){
if(str_replace($table.'_','',$key) != 'iddb' && $key != 'table'){
if(str_replace($table.'_','',$key)=='activo' && $val=='on'){
$val = 1;
}
$pares .= str_replace($table.'_','',$key).' = "'.$val.'", ';
}
}
$pares = substr($pares,0,-2);
$query = 'UPDATE '.$table.' SET '.$pares.' WHERE id = '.$id.';';
}
if ($mysqli->query($query)){
$result->ok = 'true';
$result->data = '';
}
else
{
$result->ok = 'false';
$result->data = 'Database error';
}
}
else{
$result->ok = 'false';
$result->data = 'Error';
}
echo json_encode($result);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment