thedigicraft / Atom.CMS

Atom.CMS
56 stars 52 forks source link

Part 57 - User Data not displaying & Active states #170

Closed dbashby closed 8 years ago

dbashby commented 8 years ago

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':

            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(isset($_GET['id'])){ $opened = data_page($dbc, $_GET['id']); }

    break;`

My users segment of the switch is

`case 'users':

            if(isset($_POST['submitted']) == 1){

                $first = mysqli_real_escape_string($dbc, $_POST['firstname']);
                $last = mysqli_real_escape_string($dbc, $_POST['lastname']);

                //May need to check update query firstname and lastname
                if(isset($_POST['id']) != 'null'){
                    $action = 'updated!';
                    $q = "UPDATE users SET 
                        firstname = '$first', 
                        lastname = '$last', 
                        password = '$_POST[password]', 
                        status = $_POST[status]
                        WHERE id = $_GET[id]";

                } else {
                    $action = 'added!';
                    $q = "INSERT INTO users (firstname, lastname, password, status) VALUES ('$first', '$last', '$_POST[password]', '$_POST[status]')";

                }

                $r = mysqli_query($dbc, $q);

                #Debug Query
                if($r){

                    $message = '<p>User was ' . $action . '</p>';

                } else {

                    $message = '<p>User could not be ' . $action . ' because: ' . mysqli_error($dbc);
                    $message .= '<p>' . $q . '</p>';

                }

            }

            if(isset($_GET['id'])){ $opened = data_user($dbc, $_GET['id']); }

    break;`

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

`

            <div class="form-group">
                <label for="firstname">First Name:</label>
                <input type="text" class="form-control" id="firstname" value="<?php echo $opened['firstname']; ?>" name="firstname" placeholder="First Name">
            </div>

            <div class="form-group">
                <label for="lastname">Last Name:</label>
                <input type="text" class="form-control" id="lastname" value="<?php echo $opened['lastname']; ?>" name="lastname" placeholder="Last Name">
            </div>

            <div class="form-group">
                <label for="status">Status:</label>
                <select class="form-control" id="status" name="status">

                    <option value="0" <?php if(isset($_GET['id'])){ selected('0', $opened['status'], 'selected'); } ?>>Inactive</option>
                    <option value="1" <?php if(isset($_GET['id'])){ selected('1', $opened['status'], 'selected'); } ?>>Active</option>

                </select>
            </div>

            <div class="form-group">
                <label for="password">Password:</label>
                <input type="text" class="form-control" id="password" value="" name="password" placeholder="Password">
            </div>

            <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']; ?>">

        </form>`

I do not know which issue could be causing both the failure of the active state but also the form not loading content as everything up until this video is working as per the tests that Alan does through the tutorials.

I would really appreciate someone pointing out to me where I have made the mistake as I cannot see it despite it probably waving to me. Thanks

dbashby commented 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

creptor commented 8 years ago

you should note that you use an if(isset($_POST['id'])!=null){.... and then in the query you use $_GET[id]...

dbashby commented 8 years ago

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')";

                }`
creptor commented 8 years ago

.-.

ok, then just place the code of the form... that way I can better understand what inputs you're submitting.

dbashby commented 8 years ago

The form code is at http://pastebin.com/wz5yL7ip

creptor commented 8 years ago

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-->" 
dbashby commented 8 years ago

Thanks for that, saved me ripping what remains of my hair.

dbashby commented 8 years ago

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 =

dbashby commented 8 years ago

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

dbashby commented 8 years ago

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?

dbashby commented 8 years ago

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!