-
Notifications
You must be signed in to change notification settings - Fork 79
/
send-message.php
35 lines (25 loc) · 987 Bytes
/
send-message.php
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
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$id = $_POST['user_id'];
$msg = $_POST['msg'];
// Create a new SQLite database connection
$db = new SQLite3('db.sqlite');
// Prepare the INSERT statement
$stmt = $db->prepare('INSERT INTO main.chat_history (user_id, human) VALUES (:user_id, :human)');
// Bind the parameters and execute the statement for each row of data
$row = ['user_id' => $id, 'human' => $msg];
$stmt->bindValue(':user_id', $row['user_id']);
$stmt->bindValue(':human', $row['human']);
$stmt->execute();
//
// Close the database connection
// Set the HTTP response header to indicate that the response is JSON
header('Content-Type: application/json');
// data
$data = [
"id" => $db->lastInsertRowID()
];
// Convert the chat history array to JSON and send it as the HTTP response body
echo json_encode($data);
$db->close();
}