Jump to content

Comments into id that matches person


kvnirvana

Recommended Posts

When the user types the information in the fields, and click the submit button, the information doesn’t get stored in the mysql id which matches the person they are commenting, it simply creates a new id. How can I make it so the comments are put into the id of the person they are commenting instead of making a new id?

 

 

<?php



//connect to your database 
$dbhost = 'localhost';
$dbuser = '***';
$dbpass = '****';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error connecting to mysql');

$dbname = '****';
mysql_select_db($dbname);



//query comments for this page of this article 
$inf = "SELECT * FROM `****` WHERE page = '".stripslashes($_SERVER['REQUEST_URI'])."' ORDER BY time ASC"; 
$info = mysql_query($inf); 
     if(!$info) die(mysql_error()); 

   $info_rows = mysql_num_rows($info); 
if($info_rows > 0) { 
   echo '<h5>Kommentarer til '.$_GET['b'].' '.$_GET['na'].'</h5>'; 
   echo '<table width="95%">'; 
    
while($info2 = mysql_fetch_object($info)) {     
echo '<tr>';    
echo '<td><B>Emne: </B>'.stripslashes($info2->subject).'<B> Skrevet af:</B> '.stripslashes($info2->username).'</td> <td><div align="right"> '.date('h:i:s a', $info2->time).' on '.$info2->date.'</div></td>'; 
echo '</tr><tr>'; 
echo '<td colspan="2"> '.stripslashes($info2->comment).'<hr width="95%" noshade>  </td>'; 
echo '</tr>'; 

}//end while 
echo '</table>'; 

} else echo 'Der er endnu ingen kommentarer til '.$_GET['b'].' '.$_GET['na'].'<br>'; 

if(isset($_POST['submit'])) { 
  if(!addslashes($_POST['username'])) die('<u>ERROR:</u> Du skal indtaste navn for at tilføje kommentar.'); 
  if(!addslashes($_POST['subject']))  die('<u>ERROR:</u> Du skal indtaste emne for at tilføje kommentar.'); 
  if(!addslashes($_POST['comment']))  die('<u>ERROR:</u> Du skal skrive en kommentar!'); 



//try to prevent multiple posts and flooding... 
$c = "SELECT * from `****` WHERE ip = '".$_SERVER['REMOTE_ADDR']."'"; 
  $c2 = mysql_query($c); 
     while($c3 = mysql_fetch_object($c2)) { 
      $difference = time() - $c3->time; 
    
      } //end while 

//add comment 
$q ="INSERT INTO `komi`  (page, date, time, username, ip, subject, comment) VALUES ('".$_POST['page']."', '".$_POST['date']."', '".$_POST['time']."', '".addslashes(htmlspecialchars($_POST['username']))."', '".$_SERVER['REMOTE_ADDR']."', '".addslashes(htmlspecialchars($_POST['subject']))."', '".addslashes(htmlspecialchars(nl2br($_POST['comment'])))."')"; 

$q2 = mysql_query($q); 
  if(!$q2) die(mysql_error()); 

//refresh page so they can see new comment 
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_POST['page'] . "#comments"); 

} else {  //display form 
?> 
<form name="comments"  method="post" action="<?php $_SERVER['PHP_SELF']; ?>"> 

<input type="hidden" name="page" value="<?php echo($_SERVER['REQUEST_URI']); ?>"> 
<input type="hidden" name="date" value="<?php echo(date("F j, Y.")); ?>"> 
<input type="hidden" name="time" value="<?php echo(time()); ?>"> 

<table width="90%" border="0" cellspacing="0" cellpadding="0"> 
   <tr> 
      <td><div align="right">Navn:   </div></td> 
       <td><input name="username" type="text" size="30" value=""></td> 
   </tr> 
    
    <td><div align="right">Emne:   </div></td> 
    <td><input type="text" name="subject" size="30" value=""></td> 
    </tr> 
    <tr> 
      <td><div align="right">Kommentar:   </div></td> 
      <td><textarea name="comment" cols="45" rows="5" wrap="VIRTUAL"></textarea></td> 
    </tr> 
    <tr> 
      <td></td> 
      <td colspan="2"><input type="reset" value="Slet alt">      
        <input type="submit" name="submit" value="Tilføj kommentar"></td> 
    </tr> 
  </table> 
</form> 
<?php 
} // end else 
?>

Link to comment
Share on other sites

If it is creating a new id, then that would imply you set the field up as an auto-incrementing field - abnd very likely a unique field. I would assume that if the comment is to be associated with a user, that there can be multiple comments associated with a user. If that is the case, then you need to change the field to not be auto-incrementing and not a field that must be unique. Then you simply need to provide the ID to pupulate the field in your query. I don't see anything in the query now that provides for a user ID.

Link to comment
Share on other sites

OK so how should I provide the ID to pupulate the field in my query I'm pretty new to this so please bare with me :=)

Should it be something like this?

$q ="INSERT INTO `komi`  (id, page, date, time, username, ip, subject, comment) VALUES ('".$_POST['id']."','".$_POST['page']."', '".$_POST['date']."', '".$_POST['time']."', '".addslashes(htmlspecialchars($_POST['username']))."', '".$_SERVER['REMOTE_ADDR']."', '".addslashes(htmlspecialchars($_POST['subject']))."', '".addslashes(htmlspecialchars(nl2br($_POST['comment'])))."')";

Link to comment
Share on other sites

That would work, but you should not call the field "id" as it would imply it is the id for the records in that table. The field needs to be a foreign key (the id from a different table). In fact, I would keep an ID in the table with unique values only to maintain separate records in that table.

 

So, in this case I would have an auto-incrementing "id" field in the table. Don't pass any value for it and let the ISERT statement do it automatically. But, to reference the comments back to the author, I would use a name such as "user_id" and populate that with the id of the author from the users table.

Link to comment
Share on other sites

Actually I had the comments in the same table as the users, so I've made a new table called Comments and made a field called User_id and made a relation to the id from the other table and added this

$q ="INSERT INTO `comments`  (user_id, page, date, time, username, ip, subject, comment) VALUES ('','".$_POST['page']."', '".$_POST['date']."', '".$_POST['time']."', '".addslashes(htmlspecialchars($_POST['username']))."', '".$_SERVER['REMOTE_ADDR']."', '".addslashes(htmlspecialchars($_POST['subject']))."', '".addslashes(htmlspecialchars(nl2br($_POST['comment'])))."')";

is that how it should be done, i'm totally lost at the moment?

Link to comment
Share on other sites

You need to define the user_id to be set. I would do something like this

$userID   = mysql_real_escape_string($_POST['user_id']);
$page     = mysql_real_escape_string($_POST['page']);
$date     = mysql_real_escape_string($_POST['date']);
$time     = mysql_real_escape_string($_POST['time']);
$username = mysql_real_escape_string($_POST['username']);
$remtAddr = mysql_real_escape_string($_SERVER['REMOTE_ADDR']);
$subject  = mysql_real_escape_string($_POST['subject']);
$comment  = mysql_real_escape_string($_POST['comment']);

$q = "INSERT INTO `comments`
      (user_id, page, date, time, username, ip, subject, comment)
      VALUES ('{$userID}','{$page}', '{$date}', '{$_POST['time']}', '{$username}', '{$remtAddr}', '{$subject}', '{$comment}')";

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.