-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat_process.php
More file actions
42 lines (33 loc) · 968 Bytes
/
chat_process.php
File metadata and controls
42 lines (33 loc) · 968 Bytes
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
<?php
header('Content-Type: application/json');
$request = json_decode(file_get_contents('php://input'), true);
if (!isset($request['message'])) {
echo json_encode(['response' => 'Invalid request.']);
exit;
}
$message = trim($request['message']);
// Database connection
$servername = "localhost";
$username = "root";
$password = "Pa$$@word12";
$dbname = "beta_x2";
$port = "3306";
$conn = new mysqli($servername, $username, $password, $dbname, $port);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Prepare and bind
$stmt = $conn->prepare("SELECT response FROM chat_responses WHERE question = ?");
$stmt->bind_param("s", $message);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($response);
if ($stmt->num_rows > 0) {
$stmt->fetch();
} else {
$response = "Sorry, I don't have an answer for that question.";
}
$stmt->close();
$conn->close();
echo json_encode(['response' => $response]);
?>