-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert.php
More file actions
55 lines (50 loc) · 1.38 KB
/
insert.php
File metadata and controls
55 lines (50 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
/*
* @Author: mopvhs
* @Date: 2013-11-21 21:31:32
* @Email: mopvhs@gmail.com
* @Last modified by: mopvhs
* @Last Modified time: 2013-11-25 17:05:13
*/
// 引入 Medoo
header('Content-type: application/json; charset=utf-8');
header("access-control-allow-origin: *");
require 'medoo.php';
$database = new medoo(array(
// required
'database_type' => 'mysql',
'database_name' => 'db_name',
'server' => 'localhost',
'username' => 'db_user_name',
'password' => 'db_password',
// optional
'charset' => 'utf8',
// driver_option for connection, read more from http://www.php.net/manual/en/pdo.setattribute.php
'option' => array(
PDO::ATTR_CASE => PDO::CASE_NATURAL
)
));
if(!isset($_POST['title']) || !isset($_POST['url'])) {
return;
}
if(empty($_POST['title']) || empty($_POST['url'])) {
return;
}
$title = trim($_POST['title']);
$url = trim($_POST['url']);
$title = htmlspecialchars($title, ENT_QUOTES);
$url = htmlspecialchars($url, ENT_QUOTES);
// 插入数据例子
$insert_id = $database->insert('online_tools', array(
'title' => $title,
'url' => $url,
'sort' => 0
));
$result = $insert_id ? true : false;
$msg = $result ? '保存成功!' : $database->error();
$data = array( 'success' => $result, 'msg' => $msg );
$json = json_encode($data);
echo isset($_GET['callback'])
? "{$_GET['callback']}($json)"
: $json;
?>