Closed dbashby closed 8 years ago
Link to code - http://pastebin.com/q2AzvBb6
Issue - This is my code from index.php from video 44
If I do not have null in the if(isset($_POST['id']) != ''){
I lose the ability to edit pages but I can add a new page
If I have null in the above then I can edit pages but I cannot add a new page
The error message is
Page could not be added because: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 7
UPDATE posts SET user = 4, slug = 'erterterter', title = 'trerterter', label = 'reterjhhlhjlhgj', header = 'hjglhgjlghj', body = '
ghjlhgjl
' WHERE id =
Any help appreciated!
Thanks
you should note that you use an if(isset($_POST['id'])!=null){....
and then in the query you use $_GET[id]
...
Thanks, I tried the following but that does not work either. I am assuming that you mean to use $_POST[id] at the end of the update query?
After trying that I have lost both update and insert functionality on the form.
`if(isset($_POST['id']) != 'null'){ $action = 'updated!'; $q = "UPDATE posts SET user = $_POST[user], slug = '$_POST[slug]', title = '$title', label = '$label', header = '$header', body = '$body' WHERE id = $_POST[id]";
} else {
$action = 'added!';
$q = "INSERT INTO posts (user, slug, title, label, header, body) VALUES ($_POST[user], '$_POST[slug]', '$title', '$label', '$header', '$body')";
}`
.-.
ok, then just place the code of the form... that way I can better understand what inputs you're submitting.
The form code is at http://pastebin.com/wz5yL7ip
I found it... in the last input... the name is wrong. It should be named id
the code (line 64-66):
<button type="submit" class="btn btn-success">Save</button>
<input type="hidden" name="submitted" value="1">
<input type="hidden" id="id" name="submitted" value="<?php echo $opened['id']; ?>">
should be:
<button type="submit" class="btn btn-success">Save</button>
<input type="hidden" name="submitted" value="1">
<input type="hidden" id="id" name="id" value="<?php echo $opened['id']; ?>"><!-- THE NAME WAS WRONG-->"
Thanks for that, saved me ripping what remains of my hair.
I have corrected the line as you advised but I am still getting an error on UPDATE.
Page could not be updated because: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
UPDATE posts SET user = 4, slug = 'Test-slug', title = 'Test Title', label = 'Test Lable', header = 'Test Header', body = '
testing the body
' WHERE id =
In the php code block for videos 43, 44 and ending with 45 where we create the queries.php page and past in the following block
`<?php
if(isset($_POST['submitted']) == 1){
$title = mysqli_real_escape_string($dbc, $_POST['title']);
$label = mysqli_real_escape_string($dbc, $_POST['label']);
$header = mysqli_real_escape_string($dbc, $_POST['header']);
$body = mysqli_real_escape_string($dbc, $_POST['body']);
if(isset($_POST['id']) != 'null'){
$action = 'updated';
$q = "UPDATE posts SET user = $_POST[user], slug = '$_POST[slug]', title = '$title', label = '$label', header = '$header', body = '$body' WHERE id = $_GET[id]";
} else {
$action = 'added';
$q = "INSERT INTO posts (user, slug, title, label, header, body) VALUES ($_POST[user], '$_POST[slug]', '$title', '$label', '$header', '$body')";
}
$r = mysqli_query($dbc, $q);
#Debug Query
if($r){
$message = '<p>Page was ' . $action . '</p>';
} else {
$message = '<p>Page could not be ' . $action . ' because: ' . mysqli_error($dbc);
$message .= '<p>' . $q . '</p>';
}
}
?>`
Without null
`<?php
if(isset($_POST['submitted']) == 1){
$title = mysqli_real_escape_string($dbc, $_POST['title']);
$label = mysqli_real_escape_string($dbc, $_POST['label']);
$header = mysqli_real_escape_string($dbc, $_POST['header']);
$body = mysqli_real_escape_string($dbc, $_POST['body']);
if(isset($_POST['id']) != 'null'){
$action = 'updated';
$q = "UPDATE posts SET user = $_POST[user], slug = '$_POST[slug]', title = '$title', label = '$label', header = '$header', body = '$body' WHERE id = $_GET[id]";
} else {
$action = 'added';
$q = "INSERT INTO posts (user, slug, title, label, header, body) VALUES ($_POST[user], '$_POST[slug]', '$title', '$label', '$header', '$body')";
}
$r = mysqli_query($dbc, $q);
#Debug Query
if($r){
$message = '<p>Page was ' . $action . '</p>';
} else {
$message = '<p>Page could not be ' . $action . ' because: ' . mysqli_error($dbc);
$message .= '<p>' . $q . '</p>';
}
}
?>`
If I have the single quotes empty in the initial if(isset()) I can edit a page however I cannot create 1 as I get the sql error saying UPDATE has an error in it. It should be using the INSERT query, yes?
If I put the null in the quotes I cannot edit a page, it creates a new one and if I select create a new page and complete the form I get a new page as expected.
Any ideas as to where the issue could be?
if(isset($_POST['id']) != ''){ $action = 'updated'; $q = "UPDATE posts SET user = $_POST[user], slug = '$_POST[slug]', title = '$title', label = '$label', header = '$header', body = '$body' WHERE id = $_GET[id]"; } else { $action = 'added'; $q = "INSERT INTO posts (user, slug, title, label, header, body) VALUES ($_POST[user], '$_POST[slug]', '$title', '$label', '$header', '$body')"; }
Thanks
The message returned upon the error is showing that it is not seeing the $_GET[id] but as this error is only triggered if I have 'null' instead of '' there cannot be an error in my sqli, right?
A mix of Creptor and McDuck74 fixed the issue, see https://github.com/thedigicraft/Atom.CMS/issues/63#issuecomment-190477657 for McDuck74 solution.
Thanks to both of you!
I have reached 57 but again the same issue has come up as the last time I came to this part of the project.
At the start of 57 Alan cuts the following from setup.php
if(isset($_GET['id'])){ $opened = data_page($dbc, $_GET['id']); }
and pastes it at the bottom of the pages and users switch statement. Once I follow those directions I lose the active state on both my pages.php but also users.php
At 7:50 in the video Alan clearly shows he has the active state running
My pages segment of the switch statement is
`case 'pages':
My users segment of the switch is
`case 'users':
I thought it might have been an issue with the form as the data is not loading into the form once selecting a user but it seems fine
`