SmoothieVemund
11-03-2010, 08:14 AM
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
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> •
<a href="member_account.php">Account</a> •
<a href="logout.php">Log Out</a>';
} else {
$toplinks = '<a href="join_form.php">Register</a> • <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
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?
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
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> •
<a href="member_account.php">Account</a> •
<a href="logout.php">Log Out</a>';
} else {
$toplinks = '<a href="join_form.php">Register</a> • <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
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?