Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Can't add new pages

    Hi,

    When I try to add a new page by clicking on a button, it won't work. The new page is added in the database but not in my menu. What's the problem?

    ----> CREATE_PAGE.PHP <----
    PHP Code:
    <?php require_once("includes/connection.php"); ?>
    <?php 
    require_once("includes/functions.php"); ?>

    <?php
        $errors 
    = array();
        
        
    $required_fields = array('menu_name''position''visible''content');
        foreach(
    $required_fields as $fieldname) {
            if(!isset(
    $_POST[$fieldname]) || empty($_POST[$fieldname])) {
            
    $errors[] = $fieldname;
            }    
        }
        
        
    $fields_with_lengths = array('menu_name' =>30);
        foreach(
    $fields_with_lengths as $fieldname => $maxlength) {
            if(
    strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength) {
                
    $errors[] = $fieldname;
            }
        }    
        
        if(!empty(
    $errors)) {
            
    redirect_to('new_page.php');
        }
    ?>

    <?php
        $menu_name 
    mysql_prep($_POST['menu_name']); 
        
    $position mysql_prep($_POST['position']);
        
    $visible mysql_prep($_POST['visible']);
        
    $content mysql_prep($_POST['content']);
    ?>

    <?php
        $query 
    "INSERT INTO pages (
                    menu_name, position, visible, content
                 ) VALUES (
                     '{$menu_name}', {$position}, {$visible}, '{$content}'
                 )"
    ;
        
    $result mysql_query($query$connection);
        if(
    $result) {
            
    redirect_to('content.php');
        } else {
            echo 
    "<p>Page creation failed.</p>";    
            echo 
    "<p>" mysql_error() . "</p>";    
        }
    ?>

    <?php mysql_close($connection); ?>
    ----> NEW_PAGE.PHP <----
    PHP Code:
    <?php require_once("includes/functions.php"); ?>
    <?php 
    require_once("includes/connection.php"); ?>
    <?php find_selected_page
    (); ?>
    <?php 
    include("includes/header.php"); ?>
    <?php 
    if(!isset($new_page)) { $new_page false; } ?>
    <table id="structure">
        <tr>
            <td id="navigation">&nbsp;
                <?php echo navigation($sel_subject$sel_page); ?>
            </td>
            <td id="page">
                <h2>Add new page</h2>
                <?php if(!empty($message)) { echo "<p class=\"message\">" $message .
    "</p>"; } ?>
                <?php if(!empty($errors)) { display_errros($errors); } ?>
                <form action="create_page.php?subj=<?php echo $sel_page['id']; ?>" method="post">
                <?php $new_page true?>
                    <p>Page name:
                        <input type="text" name="menu_name" value="" id="menu_name" />
                    </p>
                    <p>Position:
                        <select name="position">
                            <?php
                                
    if(!$new_page) {
                                    
    $page_set get_pages_for_subject($sel_page['subject_id']);
                                    
    $page_count mysql_num_rows($page_set);    
                                } else {
                                    
    $page_set get_pages_for_subject($sel_subject['id']);
                                    
    $page_count mysql_num_rows($page_set) + 1;
                                }
                                for(
    $count=1$count <= $page_count$count++) {
                                    echo 
    "<option value=\"{$count}\"";
                                    if(
    $sel_page['position'] == $count) { echo " selected"; }
                                    echo 
    ">{$count}</option>";    
                                }
                            
    ?>
                        </select>
                    </p>
                    <p>Visible:
                        <input type="radio" name="visible" value="0"<?php if($sel_page['visible'] == 0) { echo " checked"; }
                        
    ?> /> No
                        &nbsp;
                        <input type="radio" name="visible" value="1"<?php
                        
    if($sel_page['visible'] == 1) { echo " checked"; }
                        
    ?> /> Yes
                    </p>
                    <p>Content:<br />
                    <textarea name="content" rows="20" cols="80"><?php echo $sel_page['content'];
                
    ?></textarea>
                </p>
                    <input type="submit" name="submit" value"Add Page" />    
                </form>
                <br />
                <a href="edit_subject.php?subj=<?php echo $sel_subject['id'];
    ?>">Cancel</a><br />
            </td>
        </tr>
    </table>
    <?php require("includes/footer.php"); ?>

  • #2
    12k
    12k is offline
    New Coder
    Join Date
    Jan 2012
    Posts
    29
    Thanks
    0
    Thanked 6 Times in 6 Posts
    Any chance you can post the code where its pulling from the database to fetch the menu?

  • #3
    New Coder
    Join Date
    Nov 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by 12k View Post
    Any chance you can post the code where its pulling from the database to fetch the menu?
    PHP Code:

    function get_pages_for_subject($subject_id) {
        global 
    $connection;
        
    $query "SELECT * 
                 FROM pages 
                 WHERE subject_id = {$subject_id} 
                 ORDER BY position ASC"
    ;
        
    $page_set mysql_query($query$connection);
        
    confirm_query($page_set);
        return 
    $page_set;
    }

    function 
    navigation($sel_subject$sel_page) {
        
    $output "<ul class=\"subjects\">";
        
    $subject_set get_all_subjects();
        
        while(
    $subject mysql_fetch_array($subject_set)) {
            
    $output .= "<li";
            if (
    $subject["id"] == $sel_subject['id']) { $output .= 
    class=\"selected\""
    ; }
            
    $output .= "><a href=\"edit_subject.php?subj=" urlencode($subject["id"]) . 
                
    "\">{$subject["menu_name"]}</a></li>";
            
    $page_set get_pages_for_subject($subject["id"]);
            
    $output .= "<ul class=\"pages\">";
            
            while(
    $page mysql_fetch_array($page_set)) {
                
    $output .= "<li";
                if (
    $page["id"] == $sel_page['id']) { $output .= 
    class=\"selected\""
    ; }
                
    $output .= "><a href=\"edit_page.php?page=" urlencode($page["id"]) . 
                    
    "\">{$page["menu_name"]}</a></li>";
            }
            
    $output .= "</ul>";
        }
        
    $output .= "</ul>";    
        return 
    $output;


  • #4
    12k
    12k is offline
    New Coder
    Join Date
    Jan 2012
    Posts
    29
    Thanks
    0
    Thanked 6 Times in 6 Posts
    Hmm, it appears when your creating your page, your not setting the "subject_id" on the mysql insertion here:

    PHP Code:
        $query "INSERT INTO pages ( 
                    menu_name, position, visible, content 
                 ) VALUES ( 
                     '{$menu_name}', {$position}, {$visible}, '{$content}' 
                 )"

    In your create_page.php

    Then you are trying to access it here:

    PHP Code:
    function get_pages_for_subject($subject_id) { 
        global 
    $connection
        
    $query "SELECT *  
                 FROM pages  
                 WHERE subject_id = {$subject_id}  
                 ORDER BY position ASC"

        
    $page_set mysql_query($query$connection); 
        
    confirm_query($page_set); 
        return 
    $page_set


  • #5
    New Coder
    Join Date
    Nov 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by 12k View Post
    Hmm, it appears when your creating your page, your not setting the "subject_id" on the mysql insertion here:

    PHP Code:
        $query "INSERT INTO pages ( 
                    menu_name, position, visible, content 
                 ) VALUES ( 
                     '{$menu_name}', {$position}, {$visible}, '{$content}' 
                 )"

    In your create_page.php

    Then you are trying to access it here:

    PHP Code:
    function get_pages_for_subject($subject_id) { 
        global 
    $connection
        
    $query "SELECT *  
                 FROM pages  
                 WHERE subject_id = {$subject_id}  
                 ORDER BY position ASC"

        
    $page_set mysql_query($query$connection); 
        
    confirm_query($page_set); 
        return 
    $page_set

    How can I solve this? I just started with php

  • #6
    12k
    12k is offline
    New Coder
    Join Date
    Jan 2012
    Posts
    29
    Thanks
    0
    Thanked 6 Times in 6 Posts
    You will have to change your insertion query.

    $query = "INSERT INTO pages (
    menu_name, position, visible, content, subject_id
    ) VALUES (
    '{$menu_name}', {$position}, {$visible}, '{$content}', '{VALUE OF SUBJECT ID}'
    )";

    I highlighted in red what need to be put there. That should be all you need. The old page won't work because it was inserting into mysql incorrectly. So you will have to create a new one.

  • #7
    New Coder
    Join Date
    Nov 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by 12k View Post
    You will have to change your insertion query.

    $query = "INSERT INTO pages (
    menu_name, position, visible, content, subject_id
    ) VALUES (
    '{$menu_name}', {$position}, {$visible}, '{$content}', '{VALUE OF SUBJECT ID}'
    )";

    I highlighted in red what need to be put there. That should be all you need. The old page won't work because it was inserting into mysql incorrectly. So you will have to create a new one.
    I tried that, but it is still not working.

  • #8
    12k
    12k is offline
    New Coder
    Join Date
    Jan 2012
    Posts
    29
    Thanks
    0
    Thanked 6 Times in 6 Posts
    sorry i dont get on here much. Feel free to add me to msn: twelve_k@live.com


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •