This repository has been archived by the owner on Sep 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createAchievements.php
61 lines (51 loc) · 2.35 KB
/
createAchievements.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
<?php include("static/header.php"); ?>
<div class="container">
<?php
require_once('dbConVars.php');
// Connect to the database
$dbc = mysqli_connect($DB_HOST, $DB_USER, $DB_PASSWORD, $DB_NAME);
// if user submits, run this block
if (isset($_POST['submit'])) {
$name = mysqli_real_escape_string($dbc, trim($_POST['name']));
$description = mysqli_real_escape_string($dbc, trim($_POST['description']));
// if post is empty, skip the mysql insert and print error
if (!empty($name) && !empty($description)) {
$query = "SELECT * FROM food_achievements WHERE name = '$name'";
$data = mysqli_query($dbc, $query);
if (mysqli_num_rows($data) == 0) {
$query = "INSERT INTO food_achievements (name, description) VALUES ('$name', '$description')";
// attempt insert
if( mysqli_query($dbc, $query) ){
// Confirm success with the user
echo '<p>Your new achievement has been successfully created.</p>';
echo "<h4><a href='achievements.php' > Return </a></h4>";
} else {
echo '<p>Error, could not insert to database. Error:' . mysqli_error() .' </p>';
}
mysqli_close($dbc);
exit();
}
else {
echo '<p class="error">An achievement with that name already exists. Please use a different name.</p>';
$userName = "";
}
}
// if nothing was submited.
else {
echo '<p class="error">You must enter all information required.</p>';
}
}
mysqli_close($dbc);
?>
<div class="col-lg-4 col-lg-offset-4 form-group">
<form method="post" class="form-signin" name="form" onsubmit="return validate(this);" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<h2 class="form-signup-heading">Add a new achievement</h2>
<label for="name" class="sr-only">Name</label>
<input type="text" id="name" name="name" class="form-control" placeholder="Achievement name" required autofocus>
<label for="description" class="sr-only">Description</label>
<input type="text" id="description" name="description" class="form-control" placeholder="Description" required>
<button class="btn btn-lg btn-primary btn-block" type="submit" name="submit">Create</button>
</form>
</div>
</div>
<?php include("static/footer.php"); ?>