Jump to content

Recommended Posts

I am having a problem with my coding below, this script saves a users comment for my website. The problem is, if a user saves a comment then saves another comment, and so on. There will be tons of that users comments saved in the database.

 

What needs to be done to remove their previous comment from the database when saving a new one or only allowing the latest comment to be stored in the database.

 

THE CODE:

------------------------------------

 

<?php

$character_limit = 500;

session_start();

 

if(isset($_SESSION['logged_id']) && isset($_SESSION['logged_user']) && isset($_SESSION['pass']))

{

$check = mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['logged_id']." AND username='".$_SESSION['logged_user']."' AND password='".$_SESSION['pass']."'");

 

if(mysql_num_rows($check)==0)

{

header("Location:index.php?msg=notlogged");

}

} else {

header("Location:index.php?msg=notlogged");

}

 

$has_error = 0;

$error = array();

 

if(isset($_POST['submit']))

{

 

if(isset($_POST['comment']) && !empty($_POST['comment']))

{

$comment = mysql_real_escape_string(strip_tags($_POST['comment']));

 

if(strlen($comment) > $character_limit)

{

$error['comment'] = "Only $character_limit chars. allowed";

}

} else {

$error['comment'] = "At least 1 char. is needed";

}

 

if(empty($error))

{

$insert = mysql_query("INSERT INTO comments(`id`,`user_id`,`comment`,`created_at`) VALUES(0,".$_SESSION['logged_id'].",'".$comment."','".time()."')");

 

$check_comment = mysql_query("SELECT * FROM comments WHERE user_id=".$_SESSION['logged_id']);

 

if(mysql_num_rows($check_comment) > 0)

{

$update = mysql_query("UPDATE comments SET comment='".$comment."',created_at='".time()."' WHERE user_id=".$_SESSION['logged_id']);

 

} else {

 

$insert = mysql_query("INSERT INTO comments(`id`,`user_id`,`comment`,`created_at`) VALUES(0,".$_SESSION['logged_id'].",'".$comment."','".time()."')");

}

} else {

$has_error = 1;

}

}

else if(isset($_POST['logout']))

{

header("Location:logout.php");

}

 

$get_comment_result = mysql_query("SELECT * FROM comments WHERE user_id=".$_SESSION['logged_id']." ORDER BY id DESC LIMIT 0,1");

 

if(mysql_num_rows($get_comment_result) > 0)

{

$get_comment_data = mysql_fetch_array($get_comment_result);

$saved_comment = stripslashes($get_comment_data['comment']);

} else {

$saved_comment = "";

}

?>

Link to comment
https://forums.phpfreaks.com/topic/179391-multiple-entries-storing-in-database/
Share on other sites

I cleaned up your query, and had it execute (setting a variable to a query does no good, if you don't call the variable).


<?php
$character_limit = 500;
session_start();

if(isset($_SESSION['logged_id']) && isset($_SESSION['logged_user']) && isset($_SESSION['pass']))
{
$check = mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['logged_id']." AND username='".$_SESSION['logged_user']."' AND password='".$_SESSION['pass']."'");

if(mysql_num_rows($check)==0)
{
	header("Location:index.php?msg=notlogged");
}
} else {
header("Location:index.php?msg=notlogged");
}

$has_error = 0;
$error = array();
if(isset($_POST['submit']))
{
if(isset($_POST['comment']) && !empty($_POST['comment']))
{
	$comment = mysql_real_escape_string(strip_tags($_POST['comment']));
	if(strlen($comment) > $character_limit)
	{
		$error['comment'] = "Only $character_limit chars. allowed";
	}
}
else {
	$error['comment'] = "At least 1 char. is needed";
}
if(empty($error))
{
	$insert = mysql_query("INSERT INTO comments(`id`,`user_id`,`comment`,`created_at`) VALUES(0,".$_SESSION['logged_id'].",'".$comment."','".time()."')");
	$check_comment = mysql_query("SELECT COUNT(*) as `total_found` FROM comments WHERE user_id=".$_SESSION['logged_id']);
	$row = mysql_fetch_assoc($check_comment);
	$total_found = $row['total_found'];
	if($total_found != 0)
	{
		mysql_query("UPDATE comments SET comment='".$comment."',created_at='".time()."' WHERE user_id=".$_SESSION['logged_id']);

	} else {

		mysql_query("INSERT INTO comments(`id`,`user_id`,`comment`,`created_at`) VALUES(0,".$_SESSION['logged_id'].",'".$comment."','".time()."')");
	}
} else {
	$has_error = 1;
}
}
else if(isset($_POST['logout']))
{
header("Location:logout.php");
}

$get_comment_result = mysql_query("SELECT * FROM comments WHERE user_id=".$_SESSION['logged_id']." ORDER BY id DESC LIMIT 0,1");

if(mysql_num_rows($get_comment_result) > 0)
{
$get_comment_data = mysql_fetch_array($get_comment_result);
$saved_comment = stripslashes($get_comment_data['comment']);
} else {
$saved_comment = "";
}
?>

Notice how I have it do a count on the database side? this makes the polling much easier. It will either return 0 or 1 (or more, if it's form before). If it returns more than one, ALL comments that that user posted will be changed over to the new comment.

Maybe I am off in what you are wanting.  If a user can only have one comment then why not just search for user and if found update the comment.  If not the add user and post comment.

 

I am wanting to drop the old 'comments' entry per user when they save a new comment, or overwrite the existing entry with their new comment, currently the script is flooding the database with a entry for everytime the user saves.

I am wanting to drop the old 'comments' entry per user when they save a new comment, or overwrite the existing entry with their new commen

 

jonsjava code should do that fine,

 

however to save confusion remove this line

$insert = mysql_query("INSERT INTO comments(`id`,`user_id`,`comment`,`created_at`)  VALUES(0,".$_SESSION['logged_id'].",'".$comment."','".time()."')");

as its not used

 

Try clearing all comments from a user in your database and re-testing

(or create a new user)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.