Undefined variable: comments
Moderator: General Moderators
Thanks it all works now cheers.
just a quick question
i have set a user_id for guestbook and registration and the same for username two different tables though. - so at the moment someone could enter a new username into the guestbook even though that would not be the one they registered with.
is there a way of making sure the username entered in the guestbook is the one that they logged into the site with?
just a quick question
i have set a user_id for guestbook and registration and the same for username two different tables though. - so at the moment someone could enter a new username into the guestbook even though that would not be the one they registered with.
is there a way of making sure the username entered in the guestbook is the one that they logged into the site with?
-
magicrobotmonkey
- Forum Regular
- Posts: 888
- Joined: Sun Mar 21, 2004 1:09 pm
- Location: Cambridge, MA
-
magicrobotmonkey
- Forum Regular
- Posts: 888
- Joined: Sun Mar 21, 2004 1:09 pm
- Location: Cambridge, MA
-
magicrobotmonkey
- Forum Regular
- Posts: 888
- Joined: Sun Mar 21, 2004 1:09 pm
- Location: Cambridge, MA
thanks so i get that bit now where i can recall the username using sessions.
but how do the two user_id's in the two tables link up so that the same user_id matches that they have logged in with. don't know how to link the two.
for example
register table - has user_id, username first_name etc
guestbook will have user_id, comments.
but how do the two user_id's in the two tables link up so that the same user_id matches that they have logged in with. don't know how to link the two.
for example
register table - has user_id, username first_name etc
guestbook will have user_id, comments.
-
magicrobotmonkey
- Forum Regular
- Posts: 888
- Joined: Sun Mar 21, 2004 1:09 pm
- Location: Cambridge, MA
when the user logs in, you check against the register table to make sure its a valid user_id, right? Well, then store it in the session like
$_SESSION['user_id'] = $user;
then, when inserting into guest book,
$query = "INSERT INTO guestbook (user_id, comments) VALUES ($_SESSION[user_id], $comments)";
$_SESSION['user_id'] = $user;
then, when inserting into guest book,
$query = "INSERT INTO guestbook (user_id, comments) VALUES ($_SESSION[user_id], $comments)";
Thanks for your help
$_SESSION['user_id'] = $user;
where abouts would i put this statement
ive entered the new query in my guestbook form.
so far code looks like
$_SESSION['user_id'] = $user;
where abouts would i put this statement
ive entered the new query in my guestbook form.
so far code looks like
Code: Select all
<?php # - guestbook2.php
//This is the guestbook page for the site
// include the configuration file for error management and such
require_once ('includes/config.inc');
//Set the page title and include the HTML header.
$page_title = 'Guestbook';
include ('includes/header.html');
if (isset($_POST['submit'])) { // Handle the form.
require_once ('connections/MyConn_shopbots.php'); // Connect to database
if(isset($_POST['comments'])){
$comments = $_POST['comments'];
//check comment has been entered
if (strlen($comments) > 0) {
$comments = stripslashes($comments);
}
else
{
$comments = NULL;
echo '<p><font color="red" size"+1">You forgot to enter your comments!</b</p>';
}
if ($comments) {
$query = "INSERT INTO guestbook (user_id, comments) VALUES ($_SESSION[user_id], $comments)";
$result = mysql_query ($query) or die("Error submitting comment: ".mysql_error());
// if ($result) { // If it ran ok.
echo '<h3>Thank you for leaving a comments!</h3>';
exit();
} else { // If it did not run ok.
echo '<p><font color="red" size="+1">You could not leave a comment due to a system error. We apologise for any inconvenience.</font></p>';
}
mysql_close(); // Close the database connection.
}
}
?>Code: Select all
<h1>Guestbook</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset>
<p></p><b>Comments:<b> <textarea name="comments" rows="5" cols="30">
<?php if (isset($_POST['comments'])) echo $_POST['comments']; ?></textarea></p>
</fieldset>
<div align="center"><input type="submit" name="submit" value="Submit" /></div>
</form><!-- End of form -->
<?php // inlcude the HTML footer.
include ('includes/footer.html');
?>what i have is a
header and footer file keep all the basic links and ccs file that goes on every page of web site
index.php - home page
register.php
login.php goes to home page if successful
guestbook - that's all i have. as info will go into database if correct
i haven't a view guestbook yet. but will hope to make one somehow.
header and footer file keep all the basic links and ccs file that goes on every page of web site
index.php - home page
register.php
login.php goes to home page if successful
guestbook - that's all i have. as info will go into database if correct
i haven't a view guestbook yet. but will hope to make one somehow.
-
magicrobotmonkey
- Forum Regular
- Posts: 888
- Joined: Sun Mar 21, 2004 1:09 pm
- Location: Cambridge, MA
im using this in my login page - it works ok is that right
shall i put session_start then on my guestbook page also.
Code: Select all
// Query the database.
$query = "SELECT user_id, first_name FROM users WHERE username='$u' AND password=PASSWORD('$p')";
$result = @mysql_query ($query);
$row = mysql_fetch_array ($result, MYSQL_NUM);
if ($row) { // A match was made.
// Start the session, register the values & redirect.
$_SESSION['first_name'] = $row[1];
$_SESSION['user_id'] = $row[0];
$_SESSION['var'] = $_POST['var'] ; //returned result of query or the info that the user posted in the form
ob_end_clean(); // Delete the buffer.-
magicrobotmonkey
- Forum Regular
- Posts: 888
- Joined: Sun Mar 21, 2004 1:09 pm
- Location: Cambridge, MA