-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcreateTheKeyword.php
More file actions
52 lines (38 loc) · 1.44 KB
/
Copy pathcreateTheKeyword.php
File metadata and controls
52 lines (38 loc) · 1.44 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
48
49
50
51
52
<?php
require 'bin/functions.php';
require 'db_configuration.php';
/**
* check whether the keyword to be created already exists in the database
*/
function keyword_exists_in_db($keyword, $db)
{
$sql = "SELECT keyword FROM keywords WHERE keyword= '$keyword';";
echo $sql;
$result = mysqli_query($db, $sql);
$count = mysqli_num_rows($result);
echo "number of items found ";
echo $count;
$keyword_exists = ($count==1) ? true : false;
return $keyword_exists;
}
if (isset($_POST['keyword'])) {
//escape any special characters in the input string
$keyword = strtolower(mysqli_real_escape_string($db, $_POST['keyword']));
// check whether the keyword already exists in the DB
$keyword_exists = keyword_exists_in_db($keyword, $db);
echo "junk";
echo $keyword_exists;
// if the keyword exists, return to the list with an error message
if ($keyword_exists == true) {
exit(header('location: keywords_list.php?createKeyword=KEYWORD_EXISTS'));
} else {
// Make the first character of keyword capitalized
$keyword = ucfirst($keyword);
$sql = "INSERT INTO keywords (keyword) VALUES ('$keyword')";
mysqli_query($db, $sql);
exit(header('location: keywords_list.php?createKeyword=Success'));
}
// otherwise, pass the status code on why create failed
exit(header('location: keywords_list.php?.php?createKeyword=Error'));
} //end if
?>