-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_product.php
98 lines (79 loc) · 2.43 KB
/
create_product.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
// include database and object files
include_once 'config/database.php';
include_once 'objects/product.php';
include_once 'objects/category.php';
// get database connection
$database = new Database();
$db = $database->getConnection();
// pass connection to objects
$product = new Product($db);
$category = new Category($db);
$page_title = "Create Product";
include_once "layout_header.php";
echo "<div class='right-button-margin'>";
echo "<a href='index.php' class='btn btn-default pull-right'>Read Products</a>";
echo "</div>";
?>
<?php
// if the form was submitted - PHP OOP CRUD Tutorial
if($_POST){
// set product property values
$product->name = $_POST['name'];
$product->price = $_POST['price'];
$product->description = $_POST['description'];
$product->category_id = $_POST['category_id'];
// create the product
if($product->create()){
echo "<div class='alert alert-success'>Product was created.</div>";
}
// if unable to create the product, tell the user
else{
echo "<div class='alert alert-danger'>Unable to create product.</div>";
}
}
?>
<!-- HTML form for creating a product -->
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
<table class='table table-hover table-responsive table-bordered'>
<tr>
<td>Name</td>
<td><input type='text' name='name' class='form-control' /></td>
</tr>
<tr>
<td>Price</td>
<td><input type='text' name='price' class='form-control' /></td>
</tr>
<tr>
<td>Description</td>
<td><textarea name='description' class='form-control'></textarea></td>
</tr>
<tr>
<td>Category</td>
<td>
<?php
// read the product categories from the database
$stmt = $category->read();
// put them in a select drop-down
echo "<select class='form-control' name='category_id'>";
echo "<option>Select category...</option>";
while ($row_category = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row_category);
echo "<option value='{$id}'>{$name}</option>";
}
echo "</select>";
?>
</td>
</tr>
<tr>
<td></td>
<td>
<button type="submit" class="btn btn-primary">Create</button>
</td>
</tr>
</table>
</form>
<?php
// footer
include_once "layout_footer.php";
?>