Jump to content

[SOLVED] tricky quistion about mysql and php select statement.


redarrow

Recommended Posts

Advance thank you.

 

The following code will let the user only edit a comment within 5 minutes ok

but the problam is that it will also let the user alter any other comment within a

diffrent comment page.

 

how do i get the code to only alter the last comment that the user posted last only

witihn the last comment page thay posted.

 

at the moment the user can goto any comment page and alter the last comment

but i need it to only alter the last comment within the comment they just posted to.

 

the only thing i can think of is to make another database field and enter the user's information

ie date_added to the new field and cross match it with the below select statement.?

 

 

<?php

$select2 = "SELECT * 

                FROM blog_comments 
                WHERE id='".$_SESSION['id']."' 
                ORDER BY date_added DESC 
                LIMIT 1";

$res2=mysql_query($select2)or die(mysql_error());


while($dat2=mysql_fetch_assoc($res2)){


if($dat2['blog_comment']){

$future=time()-(5*60*1);



if($dat2['date_added'] > $future){


echo"<table width='300' border='0' bordercolor='black' align='center'>
<td align='center'>

<a href='edit_comment.php?id=".$dat2['id']."&date_added=".$dat2['date_added']." '>Edit comment</a>

<br><br>
<font size='1' color='yellow'>WARNING EDIT COMMENTS LAST <font size='1' color='blue'>5</font> MINTUES</font>
</td></table>";
}
}
}
?>

Link to comment
Share on other sites

This should pull only their comments that were added in the last 5 minutes

 

$select2 = "SELECT * 
           FROM blog_comments 
           WHERE id='".$_SESSION['id']."'
           AND date_added > UNIX_TIMESTAMP(NOW() - INTERVAL 300 SECONDS) 
           ORDER BY date_added DESC 
           LIMIT 1";

Link to comment
Share on other sites

here the database i need it to get the last entry of user_blog_date then

only let a user to have the link showing for 5 mins from now.

 

this way when a user goes to a selected blog where thay can add comments and view

them they only get the edit link to that comment not all comments in all blogs.

 

 

what it is the code below works but let's users alter all there last

comments posted but in every blog.

 

i only want them to be able to use the edit link under the blog there in.

 

id smallint(5)  UNSIGNED ZEROFILL Yes NULL                
  posted_by varchar(25)  Yes NULL                
  date_added int(11)  Yes NULL                
  title_comment varchar(100)  Yes NULL                
  blog_comment longtext  Yes NULL                
  user_blog_title varchar(100)  Yes NULL                
  user_blog_date int(11)  Yes NULL                
  user_blog_id 

Link to comment
Share on other sites

id is the user posting.

posted_by is the user that has posted.

date_added is the date they posted.

title_comment is the title of there comment.

blog_comment is the comment post.

user_blog-title is the copy of blog title of the blog

user_blog_date is the date of the blog

user_blog_id is the users id that made the blog

 

  id  posted_by  date_added  title_comment  blog_comment  user_blog_title  user_blog_date  user_blog_id  
      00001 john 1174617900 well done well good all about me 1174617306 00001 
      00001 john 1174625672 good 1 mate my name 1174625650 00001 
      00001 john 1174629404 hi how are you then mate my name 1174625650 00001 
      00001 john 1174723237 hi hi my name 1174625650 00001 
      00001 john 1174723586 koko ko my name 1174625650 00001 

Link to comment
Share on other sites

this is what i tried but might exsplain what i wanted to do ok.

 

<?php

$select3 = "SELECT * 

                FROM blog_comments 
                WHERE ORDER BY user_blog_date DESC 
                LIMIT 1";

$res3=mysql_query($select3)or die(mysql_error());


while($dat3=mysql_fetch_assoc($res3)){


$select2 = "SELECT * 

                FROM blog_comments 
                WHERE id='".$_SESSION['id']."' and 
                user_blog_date=".$dat3['user_blog_date']." 
                ORDER BY date_added DESC 
                LIMIT 1";

$res2=mysql_query($select2)or die(mysql_error());


while($dat2=mysql_fetch_assoc($res2)){


if($dat2['blog_comment']){

$future=time()-(5*60*1);



if($dat2['date_added'] > $future){


echo"<table width='300' border='0' bordercolor='black' align='center'>
<td align='center'>

<a href='edit_comment.php?id=".$dat2['id']."&date_added=".$dat2['date_added']." '>Edit comment</a>

<br><br>
<font size='1' color='yellow'>WARNING EDIT COMMENTS LAST <font size='1' color='blue'>5</font> MINTUES</font>
</td></table>";
}
}
}
}
?>

Link to comment
Share on other sites

The query I gave you should work

 

<?php
$select2 = "SELECT id 
           FROM blog_comments 
           WHERE id='".$_SESSION['id']."'
           AND date_added > UNIX_TIMESTAMP(NOW() - INTERVAL 300 SECONDS) 
           ORDER BY date_added DESC 
           LIMIT 1";
$res = mysql_query($select2) or die (mysql_error());

if (mysql_num_rows($res) > 0) {
    echo "<a href='blog_comment_edit.php> Edit latest comment</a>";
}
else echo "No recent comment to edit";
?>

 

Use a similar query (SELECTing fields to edit) in the edit page to get the record.

Link to comment
Share on other sites

Got it going and yes your code worked cheers now the link only display where the user posted now your see what i was on about ok and thank you so much mate.

 

 

 

<?php

// this seesion is set to see if there is a match.
//the session is set via the blog query what is also on the same page.

$_SESSION['date_added']=$rec['date_added'];

}
}

}
}


$select2 = "SELECT * 

                FROM blog_comments 
                WHERE id='".$_SESSION['id']."' 
                ORDER BY date_added DESC 
                LIMIT 1";

$res2=mysql_query($select2)or die(mysql_error());


while($dat2=mysql_fetch_assoc($res2)){

// cheeking for a match.

if($dat2['user_blog_date']==$_SESSION['date_added']){

if($dat2['blog_comment']){

$future=time()-(5*60*1);



if($dat2['date_added'] > $future){


echo"<table width='300' border='0' bordercolor='black' align='center'>
<td align='center'>

<a href='edit_comment.php?id=".$dat2['id']."&date_added=".$dat2['date_added']." '>Edit comment</a>

<br><br>
<font size='1' color='yellow'>WARNING EDIT COMMENTS LAST <font size='1' color='blue'>5</font> MINTUES</font>
</td></table>";
}
}
}
}
?>

Link to comment
Share on other sites

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.