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 6 of 6
  1. #1
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts

    undefined variable issue

    I am trying to create an update to a post function, while the update does occur, the page routing and selecting of posts to be updated is whats getting screwed up. I have so many files involved in this whole mess that I didnt know where to start with what. So I went to the trouble of creating a reference page summing up each file, variable, and included file to trace where elements are going.

    My website link is:

    http://royalvillicus.com/post_ads/controller_posts.php/

    I have error reporting on as you will notice.

    things to pay attention to:

    the url - when clicking on the edit post it will try to access a post id that does not exist

    saving changes - url will add a "update" to the end of it. This is meant to be a temp page thanking user for updating page then redirect to list of all posts created which is not happening. This occurs in post_update.php

    The link I sent - it is suppose to hold a list of the posts created. Currently there are two in my database. Only the first one is showing.


    Here is my reference page:

    Now I understand this page wont show the problem by itself. I find mainly to map through all the ways my files are connected together and where things start.

    I have included the file I believe to be the declaring file of the $post variable in the files that are having troubles, but still the problem persists.


    Here is my controller_posts.php file:


    PHP Code:
    ini_set('display_errors',1);
    error_reporting(E_ALL);


    include_once (
    'config.php');
    // super global includes everything in the server
    include_once('model_posts.php');

    //$action = $_GET['action'];

    switch ($route['view']) {



        case 
    "show":
            
    //$id = $_GET['id'];
            
            
    $post find_post($params['id']);
            
    //include($_SERVER['DOCUMENT_ROOT'].'/post_ads/view_posts.php');
        
    break;
        
        case 
    "index":
            
            
    $posts    find_posts();
        
        break;
        
        case 
    "edit":
        
            
    $post find_post($params['id']);
        break;
        
        case 
    "new":
                
        break;
        
        case 
    "create":
            if(
    create_post ($params['post'])) {
                
    //$_SESSION['flash']['notice'] = 'Successfully created post!';
                //flash_notice('Successfully created post!');
                //redirect_to('controller_posts.php/');
            
    }    
        break;


    // "update" is the case that is the one used to help go through the redirect to the temp page 
    // and then back to the controller_posts.php/ url link which I have provided above.
        
        
    case "update":
            
    $params['post']['id'] = $params['id'];
            if(
    update_post ($params['post'])) {
                
    //$_SESSION['flash']['notice'] = 'Successfully created post!';
                //flash_notice('Successfully updated post!');
                //redirect_to('post_update.php');
            
    }    
        break;
            
    }

    ?> 
    Here is my reference page:

    Now I understand this page wont show the problem by itself. I find mainly to map through all the ways my files are connected together and where things start.
    variables:

    $connection -
    model_posts.php

    $id -
    model_posts.php

    $_GET -
    index.php

    $matches -
    index.php

    $msg -
    controller.php

    $_POST -
    index.php

    $params -
    controller_posts.php
    create.php
    index.php
    model_posts.php
    post_create.php
    post_update.php

    $post -
    _form.php
    _post.php
    controller_posts.php
    view_post.php

    $posts -
    controller_posts.php
    view_post.php

    $query -
    model_posts.php

    $result -
    model_posts.php

    $route -
    index.php

    $routes -
    index.php

    $route_match -
    index.php

    $_SESSION -
    layout.php
    application.php
    controller_posts.php
    controller.php

    $url -
    index.php

    $urls -
    index.php





    includes:

    application.php -
    include_once(VIEW_PATH.'view_posts.php');

    config.php -
    include_once('controller.php');

    controller_posts.php -
    include_once ('config.php'); -> ('controller.php');
    include_once('model_posts.php'); -> ('config.php'); -> ('controller.php');

    edit.php -
    include_once('_form.php');

    index.php -
    include_once('config.php'); -> ('controller.php');
    include_once(CONTROL_PATH.$route['control'].'.php');
    include_once('controller_posts.php'); -> ('model_posts.php'); -> ('config.php'); -> ('controller.php');

    layout.php -
    include_once(VIEW_PATH.'view_posts.php');

    model_posts.php -
    include_once('config.php'); -> ('controller.php');

    new.php -
    include_once('_form.php');

    post_create.php -
    include_once('model_posts.php'); -> ('config.php'); -> ('controller.php');

    post_update.php -
    include_once('model_posts.php'); -> ('config.php'); -> ('controller.php');

    view_posts.php -
    include_once('_post.php'); -> ('controller_posts.php'); -> ('model_posts.php'); -> ('config.php'); -> ('controller.php');

    _post.php -
    include_once('controller_posts.php'); -> ('model_posts.php'); -> ('config.php'); -> ('controller.php');




    files:

    _form.php -
    no included files
    <fieldset>
    no <form>
    $post
    two text fields - title and body

    _post.php -
    href="<?php echo APP_ROOT; ?>controller_posts.php/<?php echo $post['id']; ?>"
    $post: four parameters - title, body, name, email
    no text fields

    application.php -
    <?php echo $_SESSION['flash']['notice']; ?>
    <?php include_once(VIEW_PATH.'view_posts.php'); ?>
    <h1>application layout</h1>
    no text fields
    html output for posts

    config.php -
    url array routes
    defined constants
    include_once('controller.php');

    controller.php -
    function redirect_to($address) {}
    flash_warning($msg) {}
    flash_notice($msg) {}
    $_SESSION['flash']['notice']
    $address
    $msg

    controller_posts.php -
    error reporting
    include_once ('config.php');
    include_once('model_posts.php');
    switch ($route['view']) {}
    $post = find_post($params['id']);
    $posts = find_posts();
    create_post ($params['post']
    $params['post']['id'] = $params['id'];
    update_post ($params['post']

    create.php -
    <?php print_r($params);?>

    edit.php -
    <form action="<?php echo APP_ROOT.$route['control']?>.php/<?php echo $post['id']; ?>/update"
    include_once('_form.php');

    index.php -
    include_once('config.php');
    function dispatcher($routes) {}
    $url = $_SERVER['REQUEST_URI'];
    $params = parse_params();
    $route_match
    $routes
    $route
    $urls
    $matches
    include_once(CONTROL_PATH.$route['control'].'.php');
    dispatcher($routes);
    function parse_params() {
    $params = array_merge($params, $_POST);
    $_POST
    $_GET }

    layout.php -
    $_SESSION['flash']['notice']
    include_once(VIEW_PATH.'view_posts.php');

    model_posts.php -
    include_once('config.php');
    function db_connect () {
    $connection
    !mysql_select_db(DATABASE, $connection) }
    function create_post ($params) {
    $query = sprintf();
    mysql_real_escape_string($params['title']),
    $result = mysql_query($query); }
    function update_post ($params) {
    $query = sprintf();
    mysql_real_escape_string($params['title']),
    $result = mysql_query($query); }
    function delete_post ($id) {
    $query = sprintf();
    mysql_real_escape_string($id)
    $result = mysql_query($query); }
    function find_posts() {
    $query =
    $result = mysql_query($query);
    $number_of_posts = @mysql_num_rows($result);
    $result = result_to_array($result) or die("SQL failure: "}
    function find_post($id) {
    $query = sprintf()
    $result = mysql_query($query);
    $number_of_posts = @mysql_num_rows($result);
    $row = mysql_fetch_array($result);}
    function result_to_array($result) {
    $result_array = array();
    for ($i=0; $row = @mysql_fetch_array($result); $i++) {
    $result_array[$i] = $row;}}

    new.php -
    action="<?php echo APP_ROOT.$route['control']?>.php/creation"
    include_once('_form.php');

    post_create.php -
    include_once('model_posts.php');
    create_post ($params['post']);
    <meta http-equiv="refresh" content="5;url=/post_ads/controller_posts.php/">

    post_update.php -
    include_once('model_posts.php');
    update_post ($params['post']);
    <meta http-equiv="refresh" content="5;url=/post_ads/controller_posts.php/">

    view_posts.php -
    $posts
    $post
    foreach($posts as $post) {
    include_once('_post.php');}
    [<a href="<?php echo APP_ROOT; ?>controller_posts.php/<?php echo $post['id']; ?>/edit">edit post</a>]

    I really appreciate any suggestions on how to bring this problem to a resolution.
    Last edited by surreal5335; 02-01-2010 at 05:44 AM. Reason: added note

  • #2
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Give all POST's and GET's a default value. Example.

    PHP Code:
    $id = isset($_GET['id']) ? $_GET['id'] : 1//Defaults to 1 
    I believe this is new for PHP 5.3



    -----------
    Leonard Whistler

  • #3
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts
    thanks for the reply, I just want to clarify that the problem is with $post not $_POST, or atleast that is what my error printing is saying. Would $_POST and $_GET still be related to this?

    Thanks for the help

  • #4
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Quote Originally Posted by surreal5335 View Post
    thanks for the reply, I just want to clarify that the problem is with $post not $_POST, or atleast that is what my error printing is saying. Would $_POST and $_GET still be related to this?

    Thanks for the help
    Any variable that gets a value from POST, GET needs a default value. Where is the value of $post coming from?


    ------------
    Leonard Whistler

  • #5
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts
    $post's value is coming from a database I created. it accesses different fields by $post['id']; It seems to be the 'id' that could be the focus of the problem child. when trying to click on edit post I am taking to an id field that doesnt exist, atleast not anymore.

  • #6
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by Len Whistler View Post
    I believe this is new for PHP 5.3
    What is?


  •  

    Posting Permissions

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