-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostComment.php
37 lines (26 loc) · 1.08 KB
/
postComment.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
36
37
<?php
/**
*
* @var $db
*
*/
session_start();
include "config.php";
if (!loggedIn()) return;
$comment = $db->real_escape_string($_POST["comment"]);
$comment = htmlspecialchars($comment);
$stmt = $db->stmt_init();
$sql = "INSERT INTO CMP204comments (content,userId,target) VALUES (?," . $_SESSION["id"] . ",?)";
if (!$stmt = $db->prepare($sql)) errorOutWithMessage("PostComment could not prepare SQL statement for insertion", true);
$stmt->bind_param("ss", $comment, $_SESSION["commentTarget"]);
$affectedRows = $stmt->execute();
if ($affectedRows < 1) errorOutWithMessage("No rows affected", true);
else if ($affectedRows > 1) errorOutWithMessage("More than one row affected", true);
$stmt = $db->stmt_init();
$sql = "SELECT id,date FROM CMP204comments WHERE id = " . $db->insert_id;
if (!$stmt = $db->prepare($sql)) errorOutWithMessage("PostComment could not prepare SQL statement for data get");
$stmt->execute();
$result = $stmt->get_result();
$result = $result->fetch_assoc();
echo json_encode(array("success", $_SESSION["username"], $result["id"], $result["date"],$comment));
return;