| Server IP : 210.245.233.93 / Your IP : 216.73.216.226 Web Server : Apache/2.2.15 (CentOS) System : Linux webserver2.onesolution.com.hk 2.6.32-754.35.1.el6.x86_64 #1 SMP Sat Nov 7 12:42:14 UTC 2020 x86_64 User : apache ( 48) PHP Version : 5.3.3 Disable Function : exec, shell_exec, system, passthru, popen, proc_open, pcntl_exec MySQL : ON | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /var/www/(Del)therainbows.org.hk/webadmin/ |
Upload File : |
<?php
function execute_sql($sql)
{
$result = mysql_query($sql);
if (!$result)
{
$error = mysql_errno() . ' - ' . mysql_error();
echo $error;
throw new Exception($error);
}
return $result;
}
function get_record($sql, $record_not_found_msg = 'Record not found.')
{
$record = array();
$result = execute_sql($sql);
if ($row = mysql_fetch_assoc($result))
{
$record = $row;
mysql_free_result($result);
}
else
{
echo $record_not_found_msg;
throw new Exception($record_not_found_msg);
}
return $record;
}
function &get_records($sql)
{
$result = execute_sql($sql);
$records = array();
while ($row = mysql_fetch_assoc($result))
$records[] = $row;
mysql_free_result($result);
return $records;
}
function save($id, $table, array $save_values, &$action=NULL, array &$model=array())
{
// get table columns
$sql = "SELECT column_name FROM information_schema.columns WHERE table_schema = (SELECT DATABASE()) AND table_name = '$table'";
$tmp_records = get_records($sql);
$columns = array();
foreach ($tmp_records as $tmp_record)
$columns[] = '`' . $tmp_record['column_name'] . '`';
// build save sql
$values = array();
foreach ($save_values as $key => $value)
{
$variable = $key;
$column = '`' . $variable . '`';
$$variable = $value;
$html_variable = 'html_' . $variable;
$$html_variable = htmlspecialchars($$variable, ENT_QUOTES);
$mysql_variable = 'mysql_' . $variable;
$$mysql_variable = empty($$variable) && strlen($$variable) == 0 ? 'NULL' : ("'" . mysql_real_escape_string($$html_variable) . "'");
$model[$variable] = $$html_variable;
if (in_array($column, $columns))
$values[$column] = $$mysql_variable;
}
if (empty($id))
{
// create
if (in_array('`create_on`', $columns))
$values['`create_on`'] = 'NOW()';
$sql = "INSERT INTO `$table` (" . implode(', ', array_keys($values)) . ") VALUES (" . implode(', ', array_values($values)) . ")";
$action = 'Created';
}
else
{
// update
$segments = array();
foreach ($values as $column => $value)
$segments[] = "$column = $value";
$sql = "UPDATE `$table` SET " . implode(', ', $segments) . " WHERE id = $id";
$action = 'Updated';
}
execute_sql($sql);
if (empty($id))
$id = mysql_insert_id();
return $id;
}