Hệ thống xếp lịch học tín chỉ cho sinh viên CNTT trên PHP & MySQL
112.920 lượt xem;
- ajax_check_unique.php
- project /
1 <?php
2 /**
3 * Checks if the value of a specific unique field already exists
4 * @param t table name
5 * @param f field name
6 * @param id the value of the PK of the record if it already exists
7 * @param value the value to be checked for being a duplicate
8 * @returns json result: { "result": "ok" } or { "result": "error" }
9 */
10 $currDir=dirname(__FILE__);
11 include("$currDir/defaultLang.php");
12 include("$currDir/language.php");
13 include("$currDir/lib.php");
14
15 /* maintenance mode */
16 handle_maintenance();
17
18 /* return json */
19 header('Content-type: application/json');
20
21 $error_return = '{ "result": "error" }';
22 $ok_return = '{ "result": "ok" }';
23
24 /* capture inputs */
25 $table = new Request('t');
26 $field = new Request('f');
27 $id = new Request('id');
28 $value = new Request('value');
29
30 /* prevent conventional error output via a shutdown handler */
31 function cancel_all_buffers(){
32 while(ob_get_level()) ob_end_clean();
33 echo $GLOBALS['result'];
34 }
35 $GLOBALS['result'] = $error_return; // error message to return if any error occurs
36 ob_start();
37 register_shutdown_function('cancel_all_buffers');
38
39 /* user has access to table? */
40 $table_perms = getTablePermissions($table->raw);
41 if(!$table_perms[0]) exit();
42
43 /* PK field name */
44 $pk = getPKFieldName($table->raw);
45 if(!$pk) exit();
46 $spk = makeSafe($pk, false);
47
48 /* check if value exists in records other than the current record */
49 $where = "`{$field->sql}`='{$value->sql}'";
50 if($id->raw){ // existing record to be excluded from search
51 $where .= " and `{$spk}`!='{$id->sql}'";
52 }
53 $chk_query = "select count(1) from `{$table->sql}` where {$where}";
54 $exists = sqlValue($chk_query);
55
56 /* return error if exists or a query error took place */
57 if($exists !== 0 && $exists !== '0') exit();
58
59 /* return ok if unique */
60 $GLOBALS['result'] = $ok_return;
2 /**
3 * Checks if the value of a specific unique field already exists
4 * @param t table name
5 * @param f field name
6 * @param id the value of the PK of the record if it already exists
7 * @param value the value to be checked for being a duplicate
8 * @returns json result: { "result": "ok" } or { "result": "error" }
9 */
10 $currDir=dirname(__FILE__);
11 include("$currDir/defaultLang.php");
12 include("$currDir/language.php");
13 include("$currDir/lib.php");
14
15 /* maintenance mode */
16 handle_maintenance();
17
18 /* return json */
19 header('Content-type: application/json');
20
21 $error_return = '{ "result": "error" }';
22 $ok_return = '{ "result": "ok" }';
23
24 /* capture inputs */
25 $table = new Request('t');
26 $field = new Request('f');
27 $id = new Request('id');
28 $value = new Request('value');
29
30 /* prevent conventional error output via a shutdown handler */
31 function cancel_all_buffers(){
32 while(ob_get_level()) ob_end_clean();
33 echo $GLOBALS['result'];
34 }
35 $GLOBALS['result'] = $error_return; // error message to return if any error occurs
36 ob_start();
37 register_shutdown_function('cancel_all_buffers');
38
39 /* user has access to table? */
40 $table_perms = getTablePermissions($table->raw);
41 if(!$table_perms[0]) exit();
42
43 /* PK field name */
44 $pk = getPKFieldName($table->raw);
45 if(!$pk) exit();
46 $spk = makeSafe($pk, false);
47
48 /* check if value exists in records other than the current record */
49 $where = "`{$field->sql}`='{$value->sql}'";
50 if($id->raw){ // existing record to be excluded from search
51 $where .= " and `{$spk}`!='{$id->sql}'";
52 }
53 $chk_query = "select count(1) from `{$table->sql}` where {$where}";
54 $exists = sqlValue($chk_query);
55
56 /* return error if exists or a query error took place */
57 if($exists !== 0 && $exists !== '0') exit();
58
59 /* return ok if unique */
60 $GLOBALS['result'] = $ok_return;