-
Notifications
You must be signed in to change notification settings - Fork 0
/
contact.php
63 lines (50 loc) · 1.76 KB
/
contact.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
require_once 'inc/headers.php';
require_once 'inc/functions.php';
$name = filter_input(INPUT_POST, "nimi");
$email = filter_input(INPUT_POST, "sposti");
$orderId = filter_input(INPUT_POST, "tilausnro");
$message = filter_input(INPUT_POST, "viesti");
if (!isset($name) || !isset($email) || !isset($message)) {
http_response_code(400);
print json_encode(array("message" => "Attribuutteja puuttuu"));
exit;
}
try {
$db = openDB();
#$responseMessage = checkCategory($db, $categoryName);
$sql = "INSERT INTO `yhteydenotto` (`nimi`, `sposti`, `tilausnro`, `viesti`) VALUES (?,?,?,?)";
$pdoStatement = $db->prepare($sql);
$pdoStatement->bindParam(1, $name);
$pdoStatement->bindParam(2, $email);
$pdoStatement->bindParam(3, $orderId);
$pdoStatement->bindParam(4, $message);
$pdoStatement->execute();
$affectedRows = $pdoStatement->rowCount();
if ($affectedRows <= 0) {
http_response_code(500);
print json_encode(array("message" => "Viestin lähettäminen epäonnistui"));
} else {
http_response_code(200);
print json_encode(array("message" => "Viesti lähetetty"));
}
exit();
} catch (PDOException $error) {
returnError($error);
}
function saveMessage($db, $userInput) {
$sql = "SELECT `nimi` FROM `tuoteryhma` WHERE `nimi` = ?";
$pdoStatement = $db->prepare($sql);
$pdoStatement->bindParam(1, $userInput);
$pdoStatement->execute();
$categoryName = $pdoStatement->fetchColumn();
if(empty($categoryName)) {
$sqlCreate = "INSERT INTO tuoteryhma(nimi) VALUES (?)";
$pdoCreate = $db->prepare($sqlCreate);
$pdoCreate->bindParam(1, $userInput);
$pdoCreate->execute();
$categoryName = $pdoCreate-> fetchColumn();
return $userInput . " kategoria luotu";
}
return $userInput . " kategoria on jo luotu";
}