kvnirvana Posted May 17, 2010 Share Posted May 17, 2010 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 ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 17, 2010 Share Posted May 17, 2010 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. Quote Link to comment Share on other sites More sharing options...
kvnirvana Posted May 17, 2010 Author Share Posted May 17, 2010 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'])))."')"; Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 17, 2010 Share Posted May 17, 2010 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. Quote Link to comment Share on other sites More sharing options...
kvnirvana Posted May 17, 2010 Author Share Posted May 17, 2010 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? Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 17, 2010 Share Posted May 17, 2010 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}')"; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.