-
Notifications
You must be signed in to change notification settings - Fork 0
/
newshoe.php
165 lines (129 loc) · 4.24 KB
/
newshoe.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/*
Runner's Medium
http://www.runnersmedium.com/
newshoe.php
add new shoe
copyright 2009 Mark Baltrusaitis <http://josieprogramme.com>
*/
require('lib/base.php');
$user->signinCheck(signin());
$stop = false;
$prompt = null;
$day = date('j', time());
$month = date('n', time());
$year = date('Y', time());
$brand = $model = $price = null;
$retired = 0;
if (isset($_POST['action']) && $_POST['action'] == 'Save') {
// get post data
if (isset($_POST['brand'])) {
$brand = $_POST['brand'];
}
if (isset($_POST['model'])) {
$model = $_POST['model'];
}
if (isset($_POST['price'])) {
$price = $_POST['price'];
}
if (isset($_POST['day'])) {
$day = $_POST['day'];
}
if (isset($_POST['month'])) {
$month = $_POST['month'];
}
if (isset($_POST['year'])) {
$year = $_POST['year'];
}
if (isset($_POST['status'])) {
$retired = $_POST['status'];
}
// validate
if (strlen($brand) < 1 || strlen($brand) > 32) {
$error = 'brand must be between 1 and 32 characters';
} elseif (strlen($model) > 32) {
$error = 'model must be less than 32 characters';
} elseif (notempty($price) && !is_numeric($price)) {
$error = 'price must be numeric';
} elseif (!is_numeric($day) || !is_numeric($month) || !is_numeric($year) ) {
$error = 'invalid purchased date';
} elseif (!checkdate($month, $day, $year) ) {
$error = 'your purchase date does not exist';
} elseif ($retired != '0' && $retired != '1') {
$error = 'invalid status response';
} else {
// escape
$sqlbrand = mysql_real_escape_string($brand);
$sqlmodel = (notempty($model)) ? '\''.mysql_real_escape_string($model).'\'' : 'NULL';
$sqlprice = (notempty($price)) ? mysql_real_escape_string($price) : 'NULL';
$sqlretired = mysql_real_escape_string($retired);
$sqldate = mysql_real_escape_string("$year-$month-$day");
// insert
$conn->query("INSERT INTO shoes (user, brand, model, price, purchased, retired)
VALUES (".mysql_real_escape_string($user->ID()).", '$sqlbrand', $sqlmodel, $sqlprice, '$sqldate', $sqlretired)");
// get the new shoe id
$result = $conn->query("SELECT id FROM shoes WHERE id = LAST_INSERT_ID() LIMIT 1");
if ($conn->rowCount($result) == 1) {
// get the new course id
$line = $conn->fetchAssoc($result);
// redirect on success
redirect(root().'shoes', '<a href="'.root().'newrun?shoes='.format($line['id']).'">Added your shoes. Click here to run them.</a>');
} else {
$error = 'there was an error creating the shoes';
}
}
}
$title = 'Runner\'s Medium - New Shoe';
require('header.php');
?>
<div id="content">
<h2>New Shoes</h2>
<?php messages($error, $message); ?>
<?php if (!$stop) : ?>
<form action="" method="post" id="newshoe">
<fieldset>
<label for="brand">Brand</label>
<input name="brand" id="brand" type="text" value="<?php echo format($brand); ?>" />
<label for="model">Model</label>
<input name="model" id="model" type="text" value="<?php echo format($model); ?>" />
<label for="price">Price</label>
<input name="price" id="price" type="text" value="<?php
if (is_numeric($price)) {
echo number_format(format($price), 2);
} else {
echo format($price);
}
?>" />
<label for="month">Purchased</label>
<select name="month" id="month">
<?php echo arrayToSelect($user->getMonths(), format($month)); ?>
</select>
<select name="day" id="day">
<?php echo arrayToSelect($user->getDays(), format($day)); ?>
</select>
<select name="year" id="year">
<?php echo arrayToSelect($user->getYears(), format($year)); ?>
</select>
<label for="active">Status</label>
<fieldset class="radio">
<input name="status" id="active" type="radio" value="0" <?php
if (!$retired) {
echo 'checked="checked"';
} ?> /><label for="active">Active</label>
<input name="status" id="retired" type="radio" value="1" <?php
if ($retired) {
echo 'checked="checked"';
} ?> /><label for="retired">Retired</label>
</fieldset>
<input name="action" type="submit" value="Save" class="button" />
</fieldset>
</form>
<?php
elseif (!is_null($prompt)) :
echo $prompt;
endif; // no errors
?>
</div>
<?php
require('footer.php');
?>