Page 1 of 1

hidden username code

Posted: Tue Apr 12, 2011 4:31 pm
by bella1010
just a simple question. what the coding to identify and get the logged user's name who had leave a comment in forum. below is my comment box coding. and how to put it into my coding?

***** PLEASE USE PHP CODE TAGS *****

view_topics.php

Code: Select all

<form action="storeaddanswer.php" method="post" name="form1" id="form1">
      <table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
        <tr>
          <td><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
              <tr>
                <td valign="top"><strong>Answer</strong></td>
                <td valign="top">:</td>
                <td><textarea name="a_answer" cols="45" rows="3" id="a_answer"></textarea></td>
              </tr>
              <tr>
                <td>&nbsp;</td>
                <td><input name="id" type="hidden" value="<?php echo $id; ?>" /></td> ---------------------------------->(this $id is forum topic id number) 
                <td><input type="submit" name="Submit" value="Submit" />
                    <input type="reset" name="Submit2" value="Reset" /></td>
              </tr>
          </table></td>
        </tr>
      </table>
    </form>



storeaddanswer.php

Code: Select all

<?php
$host="localhost"; // Host name
$username="22222222"; // Mysql username
$password="2222222222"; // Mysql password
$db_name="rrr"; // Database name
$tbl_name="bbbbbbbb"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


// Get value of id that sent from hidden field
$id=$_POST['id'];---------------------------------------------------------------------------------------------->(the forum topic id has choose by user)

// Find highest answer number.
$sql="SELECT MAX(a_id) AS Maxa_id FROM $tbl_name WHERE question_id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);

// add + 1 to highest answer number and keep it in variable name "$Max_id". if there no answer yet set it = 1
if ($rows) {
$Max_id = $rows['Maxa_id']+1;
}
else {
$Max_id = 1;
}

// get values that sent from form
//$a_name=$_POST['a_name'];
$a_answer=$_POST['a_answer'];

$datetime=date("d/m/y H:i:s"); // create date and time

// Insert answer
$sql2="INSERT INTO $tbl_name(question_id, a_id, a_name, a_answer, a_datetime)VALUES('$id', '$Max_id', '$a_name', '$a_answer', '$datetime')";
$result2=mysql_query($sql2);

if($result2){
echo "<script language='javascript'>
alert('Submitted!')
</script>
<script>
window.location='view_topics.php?id=$id'
</script>";

// If added new answer, add value +1 in reply column
$tbl_name2="vrpost";
$sql3="UPDATE $tbl_name2 SET reply='$Max_id' WHERE id='$id'";
$result3=mysql_query($sql3);

}
else {
echo "ERROR";
}

mysql_close();
?>

view_topics.php
(comment display table)

Code: Select all

<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
        <tr>
          <td><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
              <tr>
                <td bgcolor="#F8F7F1"><strong>ID</strong></td>
                <td bgcolor="#F8F7F1">:</td>
                <td bgcolor="#F8F7F1"><?php echo $rows['a_id']; ?></td>
              </tr>
              <tr>
                <td bgcolor="#F8F7F1"><strong>Answer</strong></td>
                <td bgcolor="#F8F7F1">:</td>
                <td bgcolor="#F8F7F1"><?php echo $rows['a_answer']; ?></td>
              </tr>
              <tr>
                <td bgcolor="#F8F7F1"><strong>Date/Time</strong></td>
                <td bgcolor="#F8F7F1">:</td>
                <td bgcolor="#F8F7F1"><?php echo $rows['a_datetime']; ?></td>
              </tr>
          </table></td>
        </tr>
      </table>

I need to display username in comment table. For example: "Posted by : Jack"... hope someone know how to do it...

Re: hidden username code

Posted: Tue Apr 12, 2011 6:10 pm
by danwguy
do you have the username stored in the database associated with that id? If so then all you have to do is add...

Code: Select all

<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
        <tr>
          <td><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
              <tr>
                <td bgcolor="#F8F7F1"><strong>ID</strong></td>
                <td bgcolor="#F8F7F1">:</td>
                <td bgcolor="#F8F7F1"><?php echo $rows['a_id']; ?></td>
              </tr>
              <tr>
                <td bgcolor="#F8F7F1"><strong>Answer</strong></td>
                <td bgcolor="#F8F7F1">:</td>
                <td bgcolor="#F8F7F1"><?php echo $rows['a_answer']; ?></td>
              </tr>
              <tr>
                <td bgcolor="#F8F7F1"><strong>Date/Time</strong></td>
                <td bgcolor="#F8F7F1">:</td>
                <td bgcolor="#F8F7F1"><?php echo $rows['a_datetime']; ?></td>
              </tr>
             <tr>
               <td bgcolor="#F8F7F1"><strong>Posted by</strong></td>
               <td bgcolor="#F8F7F1">:</td>
               <td bgcolor="#F8F7F1"><>php echo $rows['username']; ?></td>
            </tr>
          </table></td>
        </tr>
      </table>
assuming your username field is called username, if not just change it to whatever is your username field.

Re: hidden username code

Posted: Tue Apr 12, 2011 11:24 pm
by bella1010
the problem is, while user comment, comment table cannot get the user's name

Re: hidden username code

Posted: Wed Apr 13, 2011 11:11 am
by danwguy
so then how are you associating the username with the post? do you have 2 tables, one with the user info and one with the posting info? if you do do the post info and the username have the same ID?