thedigicraft / Atom.CMS

Atom.CMS
56 stars 50 forks source link

Video 44, new pages still get added instead of edited after watching whole video #22

Closed MrGobblez closed 10 years ago

MrGobblez commented 10 years ago

Hi, I'm having an issue where my pages still get added as new to the database instead of getting edited. I've watched the video several times and I cannot figure out what's wrong. All I know is that if(isset($_POST['id']) != 'null') never runs. If I use if(isset($_POST['id']) != '') as shown in the video I get an error.

Here's the whole index.php code:

<?php

# Start session
session_start();
if(!isset($_SESSION['username']))
{
    header('Location: login.php');
}

?>

<?php include('config/setup.php'); ?>

<!DOCTYPE html>
<html>
<head>

    <title><?php echo $page_array ['title'] . ' | ' . $site_title; ?></title>
    <meta name="viewport" content="width=device-width, initia-scale=1.0" />

    <!-- CSS include -->
    <?php include('config/css.php'); ?>

    <!-- Javascript include -->
    <?php include('config/js.php'); ?>

</head>
<body>

    <div id="wrap">

        <?php include (DIRECTORY_TEMPLATE . '/navigation.php'); ?>

        <h1>Admin Dashboard</h1>

        <div class="row">

            <div class="col-md-2">

                    <?php
                    if(isset($_POST['submitted']) ==1)
                    {
                        $title = mysqli_real_escape_string($dbc, $_POST['title']);
                        $label = mysqli_real_escape_string($dbc, $_POST['label']);
                        $description = mysqli_real_escape_string($dbc, $_POST['description']);
                        $header = mysqli_real_escape_string($dbc, $_POST['header']);
                        $body = mysqli_real_escape_string($dbc, $_POST['body']);

                        if(isset($_POST['id']) != 'null')
                        {
                            $query = "UPDATE pages SET user = $_POST[user], slug = '$_POST[slug]', title = '$title', 
                                                               label = '$label', description = '$description', 
                                                               header = '$header', body = '$body' WHERE id = $_GET[id]";
                        }

                        else
                        {
                            $query = "INSERT INTO pages(user, slug, title, label, description, header, body) VALUES 
                                 ('$_POST[user]', '$_POST[slug]', '$title', '$label', '$description', '$header', '$body')";

                        }
                        $qResult = mysqli_query($dbc, $query);

                        if($qResult)
                        {
                            $message = '<p>Page was added to database.</p>';
                        }
                        else 
                        {
                            $message = '<p>Page could not be added to database because: ' . mysqli_error($dbc) . '</p>';
                            $message .=  '<p>' . $query . '</p>';
                        }
                    } ?>

                <div class="list-group">

                <a class="list-group-item" href="index.php">
                    <h4 class="list-group-item-heading"><i class="fa fa-plus"></i> New page</h4>
                </a>

                    <?php

                        $query = "SELECT * FROM pages ORDER BY id";
                        $qResult = mysqli_query($dbc, $query);

                        while($page_list = mysqli_fetch_assoc($qResult))
                        { 
                            $clean_text_label = strip_tags($page_list['label']);
                            $clean_text_description = strip_tags($page_list['description']);
                            ?>
                            <a class="list-group-item" href="index.php?id=<?php echo $page_list['id'];?>">
                                <h4 class="list-group-item-heading"><?php echo $clean_text_label; ?></h4>
                                <p class="list-group-item-text"><?php echo $clean_text_description; ?></p>
                            </a>
                        <?php } ?>
                </div>
            </div>

            <div class="col-md-9">

                <?php
                if(isset($_GET['id']))
                {
                    $query = "SELECT * FROM pages WHERE id = $_GET[id]";
                    $qResult = mysqli_query($dbc, $query);

                    $opened = mysqli_fetch_assoc($qResult);
                }
                ?>

                <form action="index.php?<?php echo $opened; ?>" role="form" method="post">

                    <?php
                    if(isset($message))
                    {
                        echo $message;
                    } 
                    ?>

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

                    <div class="form-group">
                        <label for="title">User:</label>
                        <select class ="form-control" name="user" id="user">

                            <option value="0">No user</option>
                            <?php
                                $query = "SELECT id FROM users ORDER BY first";
                                $qResult = mysqli_query($dbc, $query);

                                while($user_list = mysqli_fetch_assoc($qResult))
                                { 
                                    $user_data = user_data($dbc, $user_list['id']);
                                ?>

                                    <option value="<?php echo $user_data['id']; ?>"
                                        <?php 
                                        if(isset($_GET['id']))
                                        {
                                            if($user_data['id'] == $opened['user']) 
                                            {
                                                echo "selected"; 
                                            }
                                        } 
                                        else
                                        {
                                            if($user_data['id'] == $user['id']) 
                                            {
                                                echo "selected"; 
                                            }
                                        }

                                        ?>><?php echo $user_data['fullname']?></option>

                                <?php }
                            ?>

                        </select>
                    </div>

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

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

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

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

                    <div class="form-group">
                        <label for="body">Body:</label>
                        <textarea class ="form-control editor" name="body" id="body" rows="8" placeholder="Page Body"><?php echo $opened['body'];?></textarea>
                    </div>

                    <button type="submit" class="btn btn-default">Save</button>
                    <input type="hidden" name="submitted" value="1">
                    <input type="hidden" name="id" value="<?php echo $opened['id'];?>">

                </form>

            </div>
        </div>

    </div> <!-- END wrap -->

    <?php include (DIRECTORY_TEMPLATE . '/footer.php'); ?>

    <?php if($debug == 1) { include('widgets/debug.php');} ?>

</body>
</html>
thedigicraft commented 10 years ago

If you use NULL, you have to do it without the quotes. NULL not 'null'.

There are some differences to note:

$x = null $x has no value and is equal to absolutely nothing at all $x = 0 $x has a value and is equal to zero $x = '' $x has a value and is equal to an empty string

MrGobblez commented 10 years ago

Thanks for super fast answer! Sadly, trying NULL, null, 0 or '' all give me Page could not be added to database 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 3

UPDATE pages SET user blablabla.

I can also see that it says index.php?id=1 in the url when trying to edit.

thedigicraft commented 10 years ago

Haha, I actually would like to see the full query. Even the blablabla part :-)

MrGobblez commented 10 years ago

Okey, here's the whole if statement with query: if(isset($_POST['id']) != '') { $query = "UPDATE pages SET user = $_POST[user], slug = '$_POST[slug]', title = '$title', label = '$label', description = '$description', header = '$header', body = '$body' WHERE id = $_GET[id]"; }

And here's the whole error: Page could not be added to database 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 pages SET user = 1, slug = 'home', title = 'Home Page', label = 'Home', description = '', header = 'header', body = '

body

' WHERE id =

thedigicraft commented 10 years ago

Weird. So when the page reloads (and this error appears) you still see that id=1 in the URL?

MrGobblez commented 10 years ago

Hmm, nope! index.php?id=1 becomes index.php?Array.

Edit: According to debug window POST has and id value of 1

Dragotic commented 10 years ago

Hello, i suggest using:

if (isset($_POST['id']) && $_POST['id'] != '')

instead of:

if(isset($_POST['id']) != '')

It worked for me for other people as well!

MrGobblez commented 10 years ago

Didn't work for me, still syntaxt error. :(

Error: Page could not be added to database 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 pages SET user = 1, slug = 'home', title = 'Home Page', label = 'Home', description = '', header = 'header', body = '' WHERE id =

thedigicraft commented 10 years ago

oh! see in your <form> tag. You have id=$opened that is an array (which is why it says array in the url). You need to give it a key (or index).. id=$opened['id']

thedigicraft commented 10 years ago

Without the key/index (terms are kinda interchangeable), PHP has no idea what you are trying to access and that is why it just spits out the word 'array' instead of a value.

MrGobblez commented 10 years ago

Changed the form, still get error: Page could not be added to database 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 pages SET user = 1, slug = 'home', title = 'Home Page', label = 'Home', description = 'desc', header = 'header', body = '

body

' WHERE id

MrGobblez commented 10 years ago

Ok, got excited and rushed, you were right about the form! Just forgot to add id= prior to the $opened. Thanks so much! Love the videos, absolutely fantastic :)

thedigicraft commented 10 years ago

Thanks man! Thanks for watching!

roohit1234 commented 10 years ago

Im getting this error....please someone help me

page could not 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 1

UPDATE pages SET user = 1, slug = 'New page1', title = 'New page1', label = 'New page1', header = 'New page1', body = '

New page1

' WHERE id =