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 4 of 4
  1. #1
    New Coder
    Join Date
    Apr 2010
    Posts
    28
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Stored session variables

    Hay.

    I'm coding a social network thingy, and I'm having some problems. What I'm trying to do; If user is logged in there is a stored session variable who keep them logged in until the logout. And when they are logged in they can see the textarea were they can type "What's on your mind", and if they do that it works perfectly. But in the database it won't save who posted that massage, and that's the problem. Let me show you.

    PHP Code:
    <?php
    session_start
    (); // Must start session first thing
    /* 
    Created By Adam Khoury @ www.flashbuilding.com 
    -----------------------June 20, 2008----------------------- 
    */
    // See if they are a logged in member by checking Session data
    $toplinks "";
    if (isset(
    $_SESSION['id'])) {
        
    // Put stored session variables into local php variable
        
    $userid $_SESSION['id'];
        
    $username $_SESSION['username'];
        
    $toplinks '<a href="member_profile.php?id=' $userid '">' $username '</a> &bull; 
        <a href="member_account.php">Account</a> &bull; 
        <a href="logout.php">Log Out</a>'
    ;
    } else {
        
    $toplinks '<a href="join_form.php">Register</a> &bull; <a href="login.php">Login</a>';
    }
    $postmassage ""
    if (isset(
    $_SESSION['id'])) {
        
    $userid $_SESSION['id'];
        
    $username $_SESSION['username'];
        
    $postmassage '    <form action="post_massage.php" method="post">
        <textarea name="post" cols="55" rows="8" id="post">Whats on your mind today?</textarea><br />
        <input type="submit" />
        </form>'
    ;
    } else {
        
    $postmassage 'To write a massage, you need to be <a href="login.php">logged in</a>.';
    }
    ?>
    <?php
    // Use the URL 'id' variable to set who we want to query info about
    $id ereg_replace("[^0-9]"""$_GET['id']); // filter everything but numbers for security
    if ($id == "") {
        echo 
    "Missing Data to Run";
        exit();
    }
    //Connect to the database through our include 
    include_once "connect_to_mysql.php";
    // Query member data from the database and ready it for display
    $sql mysql_query("SELECT * FROM members WHERE id='$id' LIMIT 1");
    $count mysql_num_rows($sql);
    if (
    $count 1) {
        echo 
    "There is no user with that id here.";
        exit();    
    }
    while(
    $row mysql_fetch_array($sql)){
    $country $row["country"];
    $state $row["state"];
    $city $row["city"];
    $accounttype $row["accounttype"];
    $bio $row["bio"];
    // Convert the sign up date to be more readable by humans
    $signupdate strftime("%b %d, %Y"strtotime($row['signupdate']));
    }
    ?>
    <html>
    <head>
    <title>MyBubble - The new thingy</title>
    </head>
    <body>
        <h1>MyBubble!</h1>
        <div id="nav"><?php echo $toplinks?></div>
        <br />
    <?php echo $postmassage?>
        <?php
        
    include ("includes/includes.php");
        
        
    $blogPosts GetBlogPosts();
        
        foreach (
    $blogPosts as $post)
        {

            echo 
    "<p>" $post->post "</p";
            echo 
    "<span class='footer'><b>Posted By: " $post->author " Posted On: " $post->datePosted "</b></span";
        }
        
    ?>
    </body>
    </html>
    Here "<?php echo $postmassage; ?>" is the textarea with the form.

    PHP Code:
    <?php
    include_once "connect_to_mysql.php";
    session_start(); 
    $username "";
    if (isset(
    $_SESSION['id'])) {
        
    // Put stored session variables into local php variable
        
    $userid $_SESSION['id'];
        
    $username $_SESSION['username'];
        
    $username $userid;
    } else {
        
    $username 'You need to be logged in and registered to post a new massage. Click here to <a href="login.php">login</a> or <a href="join_form.php">register</a>. . </a>';
    }
    ?>
    <?php
    $con 
    mysql_connect("localhost","root","annelin38");
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }

    mysql_select_db("blog"$con);

    $sql="INSERT INTO blog_posts (id, post, author_id, date_posted)
    VALUES
    ('$_POST[id]','$_POST[post]','$_POST[date_posted]','$_POST[$username]')"
    ;

    if (!
    mysql_query($sql,$con))
      {
      die(
    'Error: ' mysql_error());
      }
        echo 
    'Your massage have been submitted. <br /><br />
    To return to your profile, <a href="member_account.php">click here</a>'
    ;
    exit();

    mysql_close($con)
    ?>
    When they have posted, it must be sent to the database. Here is the problem.

    Any idea?

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,862
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    $sql="INSERT INTO blog_posts (id, post, author_id, date_posted)
    VALUES
    ('$_POST[id]','$_POST[post]','$_POST[date_posted]','$_POST[$username]')";
    What do you get for
    PHP Code:
    echo $sql="INSERT INTO blog_posts (id, post, author_id, date_posted)
    VALUES
    ('$_POST[id]','$_POST[post]','$_POST[date_posted]','$_POST[$username]')"

    ?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New Coder
    Join Date
    Apr 2010
    Posts
    28
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by abduraooft View Post
    What do you get for
    PHP Code:
    echo $sql="INSERT INTO blog_posts (id, post, author_id, date_posted)
    VALUES
    ('$_POST[id]','$_POST[post]','$_POST[date_posted]','$_POST[$username]')"

    ?
    PHP Code:
    Warningsession_start() [function.session-start]: Cannot send session cache limiter headers already sent (output started at D:ProgramfilerXampp for Windowsxampphtdocspost_massage.php:2in D:ProgramfilerXampp for Windowsxampphtdocspost_massage.php on line 4
    INSERT INTO blog_posts 
    (idpostauthor_iddate_postedVALUES ('','Whats on your mind today?','','')Your massage have been submitted

    To return to your profile
    And btw. The thing I want to do is to get to say who is logged in and writes the massage typed.
    PHP Code:
        $userid $_SESSION['id'];
        
    $username $_SESSION['username']; 
    I have those session variables to do that.

  • #4
    New Coder
    Join Date
    Apr 2010
    Posts
    28
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by SmoothieVemund View Post
    PHP Code:
    Warningsession_start() [function.session-start]: Cannot send session cache limiter headers already sent (output started at D:ProgramfilerXampp for Windowsxampphtdocspost_massage.php:2in D:ProgramfilerXampp for Windowsxampphtdocspost_massage.php on line 4
    INSERT INTO blog_posts 
    (idpostauthor_iddate_postedVALUES ('','Whats on your mind today?','','')Your massage have been submitted

    To return to your profile
    And btw. The thing I want to do is to get to say who is logged in and writes the massage typed.
    PHP Code:
        $userid $_SESSION['id'];
        
    $username $_SESSION['username']; 
    I have those session variables to do that.
    Someting like this if you understand:
    PHP Code:
    $sql="INSERT INTO blog_posts (id, post, author_id, date_posted)
    VALUES
    ('$_POST[id]','$_POST[post]','$_POST[date_posted]','$_POST[author_id = $userid]')"



  •  

    Posting Permissions

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