-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsendMessage.php
More file actions
47 lines (34 loc) · 1.35 KB
/
sendMessage.php
File metadata and controls
47 lines (34 loc) · 1.35 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
<?php
//See original: https://www.w3schools.com/php/php_mysql_connect.asp
$servername = "localhost";
$username = "root";
$password = "";
$dbName = "chatLog";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbName", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully <br>";
$sender = $_GET["sender"];
$recipient = $_GET["to"];
$subject = $_GET["subject"];
$body = $_GET["body"];
$getSenderID = "SELECT * FROM users WHERE username = '$sender'";
$getRecipientID = "SELECT * FROM users WHERE username = '$recipient'";
$senderStatement = $conn -> query($getSenderID);
$recipientStatement = $conn -> query($getRecipientID);
$senderRow = $senderStatement -> fetch();
$recipientRow = $recipientStatement -> fetch();
$senderID = $senderRow["userID"];
$recipientID = $recipientRow["userID"];
$send = "INSERT INTO messages(fromUserID, subject, body) values('$senderID', '$subject', '$body')";
$conn -> exec($send);
$messageID = $conn -> lastInsertId();
$updateMessageRecipients = "INSERT INTO messageRecipients(messageID, toUserID) values('$messageID', '$recipientID')";
$conn -> exec($updateMessageRecipients);
echo "Sent message successfully!";
}
catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>