<?php
/*
A Handler script for AJAX Lookup
Database
Arie Nugraha 2007
*/
// start the session
session_start();
require_once '../sysconfig.inc.php';
// session checking
require './default/session_check.inc.php';
$table_name = $dbs->escape_string(trim($_POST['tableName']));
$table_fields = trim($_POST['tableFields']);
if (isset($_POST['keywords']) AND !empty($_POST['keywords'])) {
$keywords = $dbs->escape_string(urldecode(trim($_POST['keywords'])));
} else {
$keywords = '';
}
// explode table fields data
$fields = str_replace(':', ', ', $table_fields);
// set where criteria
$criteria = '';
foreach (explode(':', $table_fields) as $field) {
$criteria .= " $field LIKE '%$keywords%' OR";
}
// remove the last OR
$criteria = substr_replace($criteria, '', -2);
$sql_string = "SELECT $fields ";
// append table name
$sql_string .= " FROM $table_name ";
if ($criteria) {
$sql_string .= " WHERE $criteria LIMIT 10";
}
// send query to database
$query = $dbs->query($sql_string);
$error = $dbs->error;
if ($error) {
die('<option value="0">SQL ERROR : '.$error.'</option>');
}
if ($query->num_rows > 0) {
while ($row = $query->fetch_row()) {
echo '<option value="'.$row[0].'">'.$row[1].'</option>'."\n";
}
echo '<option value="0">NONE</option>'."\n";
} else {
// output the SQL string
// echo '<option value="0">'.$sql_string.'</option>';
echo '<option value="0">NO DATA FOUND</option>';
}
?>