thedigicraft / Atom.CMS

Atom.CMS
56 stars 50 forks source link

Video 35: Field 'user' doesn't have a default value error #188

Closed ozyedi closed 8 years ago

ozyedi commented 8 years ago

Hi, i'm following video series, in admin dashbord i'm trying to add new pages via save button but it gives me this error;

Pages could not be added because: Field 'user' doesn't have a default value

$q

I dont understand where is the problem in here, can you help me pls? Here is admin/index.php codes

<?php
# Start the session:
session_start();

if(!isset($_SESSION['username'])) {
    header('location: login.php');  
}

?>

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

<!DOCTYPE html>
<html>

    <head>
    <title><?php  echo $page['title'].' | '.$site_title; ?> | </title>
    <meta name="viewport" content="width=device-width, initial-scale=1">

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

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

</head>

<body>

<div id="wrap">

<?php include(D_TEMPLATE.'/navigation.php'); ?>
<h1>Admin Dashboard</h1>

<div class="row">
    <div class="col-md-3">

       <div class="list-group">
        <?php

        $q = "SELECT * FROM pages ORDER BY title ASC";
        $r = mysqli_query($dbc, $q);

        while($page_list = mysqli_fetch_assoc($r)) { 

        $blurb = substr(strip_tags($page_list['body']), 0, 160);

        ?>

        <a class="list-group-item" href="#">
            <h4 class="list-group-ite-heading"><?php echo $page_list['title']; ?></h4>
          <p class="list-group-item-text"><?php echo $blurb; ?></p>
        </a>

        <?php } ?>
        </div>

    </div>

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

    <?php 

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

        $q = "INSERT INTO pages (title, label, header, body) VALUES('$_POST[title]', '$_POST[label]', '$_POST[header]', '$_POST[body]')";
        $r = mysqli_query($dbc, $q);

        if($r){

            echo '<p>Page was added!</p>';
            } else {

                echo '<p>Pages could not be added because: '.mysqli_error($dbc);
                echo '<p>'.'$q'.'</p>';

                }

        }

     ?>

        <form acion="index.php" method="post" rol="form">

            <div class="form-group">
                <label for="title">Title:</label>
                <input class="form-control" type="text" name="title" id="title" placeholder="Page Title">
            </div>

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

            <div class="form-group">
                <label for="header">Header:</label>
                <input class="form-control" type="text" name="header" id="header" placeholder="Page Header">
            </div>

            <div class="form-group">
                <label for="body">Body:</label>
                <textarea class="form-control" type="text" name="body" id="body" rows="8" placeholder="Page Body"></textarea>
            </div>

            <button type="submit" class="btn btn-default">Save</button>
            <input type="hidden" name="submitted" value="1">

        </form>

    </div>
</div>

</div>

<?php include(D_TEMPLATE.'/footer.php'); ?>
<?php if($debug == 1) { include('widgets/debug.php'); } ?>
</body>
</html>
creptor commented 8 years ago

I believe it has something to do with the table. I think that when creating a page you need to define the user that made it.

To fix this you could try to replace your insert query to the following code:

$q = "INSERT INTO pages (title, label, header, body, user) VALUES('$_POST[title]', '$_POST[label]', '$_POST[header]', '$_POST[body]', '$user[id]')";
$r = mysqli_query($dbc, $q);
ozyedi commented 8 years ago

its done!! many thnx for help. tryin to solve many hours, thanx again