Created
May 19, 2015 08:09
-
-
Save basicssm/c8bf56d4d6d0fa0a1a7e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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