thedigicraft / Atom.CMS

Atom.CMS
56 stars 52 forks source link

VIDEO 40: UNDEFINED VARIABLE OPENED #190

Open MJAzura opened 7 years ago

MJAzura commented 7 years ago

Hi, I am getting error Undefined Variable opened.

My code:

            <?php if(isset($_GET['id'])) 
                    {

                        $q= "SELECT * FROM pages WHERE id = $_GET[id]";
                        $r= mysqli_query($mysql, $q);

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

        <form action="index.php" method="post" role="form">

            <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>
creptor commented 7 years ago

It's simple, but to be very clear... one must _always_ define all variables 😋. so to fix this, you just have to change the first part of the code to this:

<?php
if(isset($_GET['id'])&&is_numeric($_GET['id'])){
    $q= "SELECT * FROM pages WHERE id = $_GET[id]";
    $r= mysqli_query($mysql, $q);
    $opened = mysqli_fetch_assoc($r);    
}else{
    $opened = null;
}               
?>

Just defined opened in case there's no id specified.

MJAzura commented 7 years ago

Hey! Thanks for the answer, but now, when I click the sidebar pages it still shows blank in the input fields.

creptor commented 7 years ago

Ok, so maybe you're not using numeric ids? In that case use if(isset($_GET['id'])){ instead of if(isset($_GET['id'])&&is_numeric($_GET['id'])){.

If it still doesn't work first check that the database table has valid values in them, and that the $_GET['id'] variable is correctly inputed in the url. Example: http://somewebsite.com/admin?page=pageEditor&id=1

MJAzura commented 7 years ago

I'm using numeric ID's.

creptor commented 7 years ago

do you actually get and url like the above one when clicking on a link inside the sidebar panel?

MJAzura commented 7 years ago

No. I get ndex.php?=2 (or whatever the ID is of the post I've clicked) where 2 is the ID of the post.

I didn't add slugs, not necessary for what I'm building.

creptor commented 7 years ago

please provide the full code of that page (the one where the sidebar is on) and i'll fix it for you 😋.

MJAzura commented 7 years ago

I have sent my code to your Live email. Please respond my email with the fixed code, I don't really want my functions to be public.