UPDATE subjects werkt niet
Ik ben bezig aan een CMS. De subject en pages haalt hij uit de databank, ik kan al subjects toevoegen en verwijderen, maar ik heb een probleempje met het wijzigen ervan. Iemand enig idee hoe dit komt?
CONTENT.PHP
Code (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<table id="structure">
<tr>
<td id="navigation">
<?php echo navigation($sel_subject, $sel_page); ?>
<br />
<a href="new_subject.php">+ Add a new subject</a>
</td>
<td id="page">
<?php if(!is_null($sel_subject)) { ?>
<h2><?php echo $sel_subject['menu_name']; ?></h2>
<?php } elseif(!is_null($sel_page)) { ?>
<h2><?php echo $sel_page['menu_name']; ?></h2>
<div class="page-content">
<?php echo $sel_page['content']; ?>
</div>
<?php } else { ?>
<h2>Select a subject or page to edit</h2>
<?php } ?><br />
</td>
</tr>
</table>
[/CODE]
<?php include("includes/footer.php"); ?>
EDIT_SUBJECT.PHP
<?php require_once("includes/functions.php"); ?>
<?php require_once("includes/connection.php"); ?>
<?php
if(intval($_GET['subj']) == 0) {
redirect_to("content.php");
}
if(isset($_POST['submit'])) {
$errors = array();
$required_fields = array('menu_name', 'position', 'visible');
foreach($required_fields as $fieldname) {
if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname])) {
$errors[] = $fieldname;
}
}
$fields_with_lengths = array('menu_name' => 30);
foreach($fields_with_lengths as $fieldname => $maxlenght) {
if(strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength) {
$errors[] = $fieldname;
}
}
if(empty($errors)) {
$id = mysql_prep($_GET['subj']);
$menu_name = mysql_prep($_POST['menu_name']);
$position = mysql_prep($_POST['position']);
$visible = mysql_prep($_POST['visible']);
$query = "UPDATE subjects SET
menu_name = '{$menu_name}',
position = {$position},
visible = {$visible}
WHERE id = {$id}";
$result = mysql_query($query, $connection);
if(mysql_affected_rows() == 1) {
// Succes
$message = "The subject was successfully updated.";
} else {
// Failed
$message = "The subject update failed.";
$message .= "<br />" . mysql_error();
}
} else {
// Errors occurred
$message = "There were " . count($errors) . " errors in the form";
}
}
?>
<?php find_selected_page(); ?>
<?php include("includes/header.php"); ?>
[CODE]
<table id="structure">
<tr>
<td id="navigation">
<?php echo navigation($sel_subject, $sel_page); ?>
</td>
<td id="page">
<h2>Edit Subject: <?php echo $sel_subject['menu_name']; ?></h2>
<?php
if(!empty($message)) {
echo"<p class=\"message\">" . $message . "</p>";
}
?>
<?php
if(!empty($errors)) {
echo "<p class=\"errors\">";
echo "Please review the following fileds:<br />";
foreach($errors as $error) {
echo " - " . $error . "<br />";
}
echo "</p>";
}
?>
<form action="edit_subject.php?subj=<?php echo
urlencode($sel_subject['id']); ?>" method="post">
<p>Subject name:
<input type="text" name="menu_name" value="<?php echo
$sel_subject['menu_name']; ?>" id="menu_name" />
</p>
<p>Position:
<select name="position">
<?php
$subject_set = get_all_subjects();
$subject_count = mysql_num_rows($subject_set);
for($count=1; $count <= $subject_count+1; $count++) {
echo "<option value=\"{$count}\"";
if($sel_subject['position'] == $count) {
echo " selected";
}
echo ">{$count}</option>";
}
?>
</select>
</p>
<p>Visible:
<input type="radio" name="visible" value="0"<?php
if($sel_subject['visible'] == 0) { echo " checked"; }
?> /> No
<input type="radio" name="visible" value="1"<?php
if($sel_subject['visible'] == 1) { echo " checked"; }
?> /> Yes
</p>
<input type="submit" name="submit" value"Edit Subject" />
<a href="delete_subject.php?subj=<?php echo
urlencode($sel_subject['id']); ?>" onclick="return confirm('Are you sure?');">Delete Subject</a>
</form>
<br />
<a href="content.php">Cancel</a>
</td>
</tr>
</table>
[/CODE]
<?php include("includes/footer.php"); ?>
FUNCTIONS.PHP
<?php
function mysql_prep($value) {
$magic_quotes_active = get_magic_quotes_gpc();
$new_enough_php = function_exists("mysql_reql_escqpe_string");
if($new_enough_php) {
if($magic_quotes_active) { $value = stripslashes($value); }
$value = mysql_real_escape_string($value);
} else {
if(!magic_quotes_active) { $value = addslashes($value); }
}
return $value;
}
function redirect_to($location = NULL) {
if($location != NULL) {
header("Location: {$location}");
exit;
}
}
function confirm_query($result_set) {
if(!$result_set) {
die("Database query failed: " .mysql_error());
}
}
function navigation($sel_subject, $sel_page) {
$output = "<ul class=\"subjects\">";
$subject_set = get_all_subjects();
while($subject = mysql_fetch_array($subject_set)) {
$output .= "<li";
if ($subject["id"] == $sel_subject['id']) { $output .= "
class=\"selected\""; }
$output .= "><a href=\"edit_subject.php?subj=" . urlencode($subject["id"]) .
"\">{$subject["menu_name"]}</a></li>";
$page_set = get_pages_for_subject($subject["id"]);
$output .= "<ul class=\"pages\">";
while($page = mysql_fetch_array($page_set)) {
$output .= "<li";
if ($page["id"] == $sel_page['id']) { $output .= " class=\"selected\""; }
$output .= "><a href=\"content.php?page=" . urlencode($page["id"]) .
"\">{$page["menu_name"]}</a></li>";
}
$output .= "</ul>";
}
$output .= "</ul>";
return $output;
}
?>
<tr>
<td id="navigation">
<?php echo navigation($sel_subject, $sel_page); ?>
<br />
<a href="new_subject.php">+ Add a new subject</a>
</td>
<td id="page">
<?php if(!is_null($sel_subject)) { ?>
<h2><?php echo $sel_subject['menu_name']; ?></h2>
<?php } elseif(!is_null($sel_page)) { ?>
<h2><?php echo $sel_page['menu_name']; ?></h2>
<div class="page-content">
<?php echo $sel_page['content']; ?>
</div>
<?php } else { ?>
<h2>Select a subject or page to edit</h2>
<?php } ?><br />
</td>
</tr>
</table>
[/CODE]
<?php include("includes/footer.php"); ?>
EDIT_SUBJECT.PHP
<?php require_once("includes/functions.php"); ?>
<?php require_once("includes/connection.php"); ?>
<?php
if(intval($_GET['subj']) == 0) {
redirect_to("content.php");
}
if(isset($_POST['submit'])) {
$errors = array();
$required_fields = array('menu_name', 'position', 'visible');
foreach($required_fields as $fieldname) {
if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname])) {
$errors[] = $fieldname;
}
}
$fields_with_lengths = array('menu_name' => 30);
foreach($fields_with_lengths as $fieldname => $maxlenght) {
if(strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength) {
$errors[] = $fieldname;
}
}
if(empty($errors)) {
$id = mysql_prep($_GET['subj']);
$menu_name = mysql_prep($_POST['menu_name']);
$position = mysql_prep($_POST['position']);
$visible = mysql_prep($_POST['visible']);
$query = "UPDATE subjects SET
menu_name = '{$menu_name}',
position = {$position},
visible = {$visible}
WHERE id = {$id}";
$result = mysql_query($query, $connection);
if(mysql_affected_rows() == 1) {
// Succes
$message = "The subject was successfully updated.";
} else {
// Failed
$message = "The subject update failed.";
$message .= "<br />" . mysql_error();
}
} else {
// Errors occurred
$message = "There were " . count($errors) . " errors in the form";
}
}
?>
<?php find_selected_page(); ?>
<?php include("includes/header.php"); ?>
[CODE]
<table id="structure">
<tr>
<td id="navigation">
<?php echo navigation($sel_subject, $sel_page); ?>
</td>
<td id="page">
<h2>Edit Subject: <?php echo $sel_subject['menu_name']; ?></h2>
<?php
if(!empty($message)) {
echo"<p class=\"message\">" . $message . "</p>";
}
?>
<?php
if(!empty($errors)) {
echo "<p class=\"errors\">";
echo "Please review the following fileds:<br />";
foreach($errors as $error) {
echo " - " . $error . "<br />";
}
echo "</p>";
}
?>
<form action="edit_subject.php?subj=<?php echo
urlencode($sel_subject['id']); ?>" method="post">
<p>Subject name:
<input type="text" name="menu_name" value="<?php echo
$sel_subject['menu_name']; ?>" id="menu_name" />
</p>
<p>Position:
<select name="position">
<?php
$subject_set = get_all_subjects();
$subject_count = mysql_num_rows($subject_set);
for($count=1; $count <= $subject_count+1; $count++) {
echo "<option value=\"{$count}\"";
if($sel_subject['position'] == $count) {
echo " selected";
}
echo ">{$count}</option>";
}
?>
</select>
</p>
<p>Visible:
<input type="radio" name="visible" value="0"<?php
if($sel_subject['visible'] == 0) { echo " checked"; }
?> /> No
<input type="radio" name="visible" value="1"<?php
if($sel_subject['visible'] == 1) { echo " checked"; }
?> /> Yes
</p>
<input type="submit" name="submit" value"Edit Subject" />
<a href="delete_subject.php?subj=<?php echo
urlencode($sel_subject['id']); ?>" onclick="return confirm('Are you sure?');">Delete Subject</a>
</form>
<br />
<a href="content.php">Cancel</a>
</td>
</tr>
</table>
[/CODE]
<?php include("includes/footer.php"); ?>
FUNCTIONS.PHP
<?php
function mysql_prep($value) {
$magic_quotes_active = get_magic_quotes_gpc();
$new_enough_php = function_exists("mysql_reql_escqpe_string");
if($new_enough_php) {
if($magic_quotes_active) { $value = stripslashes($value); }
$value = mysql_real_escape_string($value);
} else {
if(!magic_quotes_active) { $value = addslashes($value); }
}
return $value;
}
function redirect_to($location = NULL) {
if($location != NULL) {
header("Location: {$location}");
exit;
}
}
function confirm_query($result_set) {
if(!$result_set) {
die("Database query failed: " .mysql_error());
}
}
function navigation($sel_subject, $sel_page) {
$output = "<ul class=\"subjects\">";
$subject_set = get_all_subjects();
while($subject = mysql_fetch_array($subject_set)) {
$output .= "<li";
if ($subject["id"] == $sel_subject['id']) { $output .= "
class=\"selected\""; }
$output .= "><a href=\"edit_subject.php?subj=" . urlencode($subject["id"]) .
"\">{$subject["menu_name"]}</a></li>";
$page_set = get_pages_for_subject($subject["id"]);
$output .= "<ul class=\"pages\">";
while($page = mysql_fetch_array($page_set)) {
$output .= "<li";
if ($page["id"] == $sel_page['id']) { $output .= " class=\"selected\""; }
$output .= "><a href=\"content.php?page=" . urlencode($page["id"]) .
"\">{$page["menu_name"]}</a></li>";
}
$output .= "</ul>";
}
$output .= "</ul>";
return $output;
}
?>
Gewijzigd op 12/01/2012 12:04:52 door Jos Vermassen
Plaats a.u.b. alleen de relevante code. Controleer je query (wat is daadwerkelijk je query) en wat gaat er dan fout? (krijg je een error, gebeurt er niks etc.)
TJVB tvb op 12/01/2012 12:00:45:
Plaats a.u.b. alleen de relevante code. Controleer je query (wat is daadwerkelijk je query) en wat gaat er dan fout? (krijg je een error, gebeurt er niks etc.)
Als ik op de knop druk om een subject te wijzigen, dan gaat hij naar de 'else'.
if(mysql_affected_rows() == 1) {
// Succes
$message = "The subject was successfully updated.";
} else {
// Failed
$message = "The subject update failed.";
$message .= "<br />" . mysql_error();
}
} else {
// Errors occurred
$message = "There were " . count($errors) . " errors in the form";
}
Dit is mijn query:
$query = "UPDATE subjects SET
menu_name = '{$menu_name}',
position = {$position},
visible = {$visible}
WHERE id = {$id}";
Gewijzigd op 12/01/2012 12:11:54 door Jos Vermassen
Zet dit eens bovenaan je code
En haal je variabelen eens buiten quotes, dus:
en probeer de uitkomst van de echo eens in phpmyadmin zelf.. (mocht hij kloppen in de echo naar jou idee)
Als ik op de knop druk om te wijzigen, gaat hij meteen naar de else van dit:
Code (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
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
if(empty($errors)) {
$id = mysql_prep($_GET['subj']);
$menu_name = mysql_prep($_POST['menu_name']);
$position = mysql_prep($_POST['position']);
$visible = mysql_prep($_POST['visible']);
$query = "UPDATE subjects SET
menu_name = '{$menu_name}',
position = {$position},
visible = {$visible}
WHERE id = {$id}";
$result = mysql_query($query, $connection);
if(mysql_affected_rows() == 1) {
// Succes
$message = "The subject was successfully updated.";
} else {
// Failed
$message = "The subject update failed.";
$message .= "<br />" . mysql_error();
}
} else {
// Errors occurred
$message = "There were " . count($errors) . " errors in the form";
}
}
[/CODE]
$id = mysql_prep($_GET['subj']);
$menu_name = mysql_prep($_POST['menu_name']);
$position = mysql_prep($_POST['position']);
$visible = mysql_prep($_POST['visible']);
$query = "UPDATE subjects SET
menu_name = '{$menu_name}',
position = {$position},
visible = {$visible}
WHERE id = {$id}";
$result = mysql_query($query, $connection);
if(mysql_affected_rows() == 1) {
// Succes
$message = "The subject was successfully updated.";
} else {
// Failed
$message = "The subject update failed.";
$message .= "<br />" . mysql_error();
}
} else {
// Errors occurred
$message = "There were " . count($errors) . " errors in the form";
}
}
[/CODE]
Gewijzigd op 12/01/2012 12:19:20 door Jos Vermassen
Dan moet je dus de rest controleren.
echo eens mysql_affected_rows(), kijk hoeveel er daadwerkelijk zijn aangepast.
There were 1 errors in the form
Please review the following fileds:
- menu_name
Lijkt me toch duidelijk, dan moet je die error oplossen.