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
/
linkIngredientsAction.php
executable file
·80 lines (63 loc) · 2.06 KB
/
linkIngredientsAction.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
session_start();
include('static/header.php');
include 'dbConVars.php';
if(!isset($_SESSION['valid_user'])){
echo "<h2>Please login first before continuing</h2>";
}else{
$failure = false;
$recipeID = $_POST['recipe_id'];
$ingredients = $_POST['ingredients_selected'];
echo "<div class='container'>";
$servername = $DB_HOST;
$username = $DB_USER;
$password = $DB_PASSWORD;
$dbname = $DB_NAME;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//array to store existing ingredient ids
$existingID = array();
//check all ingredient ids for the the recipe that exists in table
$sql = 'SELECT i_id FROM food_recipe_ingredients WHERE r_id='. $recipeID;
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
$existingID[] = $row['i_id'];
}
}
//for all old ingredients, check against newly submitted list
foreach($existingID as $eID){
if(!in_array($eID, $ingredients)){ //if not in new list, then remove from table
$sql = 'DELETE FROM food_recipe_ingredients WHERE r_id='. $recipeID .' AND i_id='. $eID;
$removeResult = $conn->query($sql);
}
}
//take all new ingredients and overwrite old ingredients for recipe
foreach($ingredients as $in){
if(in_array($in, $existingID)){
continue; //skip insertion of this ingredient
}
$sql = 'INSERT INTO food_recipe_ingredients (r_id, i_id) VALUES ('. $recipeID .', '. $in .')';
$res = $conn->query($sql);
if(!$res){
$failure = true;
}
}
if($failure){
echo "Insert Failed!";
}else{
echo "Successfully updated! Redirecting back to recipes page";
}
sleep(3);
?>
</div>
<script type="text/javascript">
window.location = "recipes.php";
</script>
<?php include("static/footer.php");
}
?>