justAnoob Posted April 18, 2009 Share Posted April 18, 2009 ok, i have a table that displays on a page. the table consists of mysql entries from a user. here is the code for the table that is displayed <?php echo "<tr><td align='center'>"; echo '<img src="' . $row['imgpath'] . '" width="125" alt="" />'; echo "</td><td align='center'>"; echo $row['pc_name']; echo "</td><td align='center'>"; echo $row['description']; echo "</td><td align='center'>"; echo $row['in_return']; echo "</td><td align='center'>"; echo "<a href=composemessage.php><img src=\"images/K_little.png\" border='0'></a>"; echo "</td></tr>"; ?> by clicking on the k_little.png it takes you to a screen where you can send a private message to another user. What I'm trying to do is when you click on that k_little picture, i would like it to get the username of the person that you are sending the message to and save it as a $_SESSION variable, so then when your taken to the compose message page of the messaging system, it will already have the username filled in that your sending to message to. Now the mysql table that has the info that is displayed in table format above also has a field in it called user_id, which of course is the same id that I have in another table called members. So how would I go about getting that username????.... I know it will be some sort of query,, but I'm not sure how to write it out? Understand???Basically all the rows that are echoed in the table are assoiciated with different users. Just want to get the username from that row. Quote Link to comment https://forums.phpfreaks.com/topic/154575-solved-help-please/ Share on other sites More sharing options...
Maq Posted April 18, 2009 Share Posted April 18, 2009 Why don't you just use the GET method and pass the the id of the user through the URL? echo ""; Then wherever you compose the message you can just grab the id with: $to_id = $_GET['to_id']; Quote Link to comment https://forums.phpfreaks.com/topic/154575-solved-help-please/#findComment-812819 Share on other sites More sharing options...
justAnoob Posted April 18, 2009 Author Share Posted April 18, 2009 when the table is display it looks like this,,, more than 1 user info in a table user1pic description address k_little.png user2pic description address k_little.png user3pic description address k_little.png by clicking the k_little.png i want the username to appear in the to box of the compose message page. the table that holds the info above has a user_id field that holds a number that matches with another table that has the id of that user, which has the username. understand now? sorry for the confusion. each k_little.png will retrieve the username of the user that uploaded the userpic. Quote Link to comment https://forums.phpfreaks.com/topic/154575-solved-help-please/#findComment-812831 Share on other sites More sharing options...
eRott Posted April 18, 2009 Share Posted April 18, 2009 Frankly, I see no reason to save it to a session. Just do as Maq suggested and use the GET method. Page with list of users: <?php echo "<tr><td align='center'>"; echo '<img src="' . $row['imgpath'] . '" width="125" alt="" />'; echo "</td><td align='center'>"; echo $row['pc_name']; echo "</td><td align='center'>"; echo $row['description']; echo "</td><td align='center'>"; echo $row['in_return']; echo "</td><td align='center'>"; echo '<a href="composemessage.php?uid='.$row['user_id'].'"><img src="images/K_little.png" style="border: 0px;" /></a>'; echo "</td></tr>"; ?> composemessage.php --> (replace table_name with the appropriate value) <?php $uid = $_GET['uid']; $query = "SELECT username FROM table_name WHERE user_id = '$uid'"; $result = mysql_query($query); $user = mysql_fetch_array($result); echo '<form>'; // form here ... echo '<input type="text" name="to" value="'.$user['username'].'" />'; // ... rest of form here echo '</form>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/154575-solved-help-please/#findComment-812862 Share on other sites More sharing options...
justAnoob Posted April 18, 2009 Author Share Posted April 18, 2009 I will give it a try.... Thanks everyone. Quote Link to comment https://forums.phpfreaks.com/topic/154575-solved-help-please/#findComment-812868 Share on other sites More sharing options...
justAnoob Posted April 18, 2009 Author Share Posted April 18, 2009 this gives an error in the line below <?php include "connection.php"; mysql_connect("$host", "$username", "$password") or die("Could not connect."); mysql_select_db("$db_name") or die("Could not find database"); $query = "SELECT username FROM members WHERE user_id = '$uid'"; $result = mysql_query($query); $user = mysql_fetch_array($result); //error here echo '<form>'; echo '<input type="text" name="to" value="'.$user['username'].'" />'; echo '</form>'; ?> so i did this and the text box appears but it is empty,, no username <?php include "connection.php"; mysql_connect("$host", "$username", "$password") or die("Could not connect."); mysql_select_db("$db_name") or die("Could not find database"); $query = "SELECT username FROM members WHERE user_id = '$uid'"; $result = mysql_query($query); //$user = mysql_fetch_array($result); //commented this line out just for fun echo '<form>'; echo '<input type="text" name="to" value="'.$user['username'].'" />'; echo '</form>'; ?> any ideas why the username isn't showing up? Quote Link to comment https://forums.phpfreaks.com/topic/154575-solved-help-please/#findComment-812894 Share on other sites More sharing options...
Maq Posted April 18, 2009 Share Posted April 18, 2009 You're never getting the user_id from the previous page. Try this (*comments in code*): mysql_select_db("$db_name") or die("Could not find database"); $uid = $_GET['uid']; // Added to get the id from the URL $query = "SELECT username FROM members WHERE user_id = '$uid'"; $result = mysql_query($query) or die(mysql_error()); // Added or die for debugging Quote Link to comment https://forums.phpfreaks.com/topic/154575-solved-help-please/#findComment-812903 Share on other sites More sharing options...
justAnoob Posted April 18, 2009 Author Share Posted April 18, 2009 unknow column 'user_id' in 'where clause' that is the error that I get instead of the username.. I'm still a little new with all this, but I think i figured it out... if anyone else wants to chime in,, feel free.. thanks. Quote Link to comment https://forums.phpfreaks.com/topic/154575-solved-help-please/#findComment-812969 Share on other sites More sharing options...
GregL83 Posted April 18, 2009 Share Posted April 18, 2009 i find it good practice to write a query like so when using variables: $query = "SELECT username FROM members WHERE user_id = '".$uid."'"; try doing this: mysql_select_db("$db_name") or die("Could not find database"); $uid = $_GET['uid']; // Added to get the id from the URL $query = "SELECT username FROM members WHERE user_id = '".$uid."'"; echo $query; // see what the query looks like exit; //stop script $result = mysql_query($query) or die(mysql_error()); // Added or die for debugging you should always learn to echo back a query if it is not working correctly...sometime you find that the $uid variable isn't gettting inserted into the query properly. let us know what you find. Quote Link to comment https://forums.phpfreaks.com/topic/154575-solved-help-please/#findComment-812980 Share on other sites More sharing options...
GregL83 Posted April 18, 2009 Share Posted April 18, 2009 also, make sure you have the correct table selected. basically the error indicates that the field titled user_id doesn't exist. maybe it was id instead?? Quote Link to comment https://forums.phpfreaks.com/topic/154575-solved-help-please/#findComment-812982 Share on other sites More sharing options...
justAnoob Posted April 19, 2009 Author Share Posted April 19, 2009 starting to get a little confused on why this is so difficult.... see anything wrong.... <?php $limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page; $query = "SELECT imgpath, item_name, description, in_return FROM member_trades WHERE category = 'Video Games' $limit"; $uid = $_GET['user_id']; $result = mysql_query($query) or trigger_error("SQL", E_USER_ERROR); echo "<table border='1' bgcolor='#FFFFFF'><tr><td bgcolor='#B8D2EO'><H3>Image</h3></td><td bgcolor='#B8D2EO'><H3>Item Name</H3></td><td bgcolor='#B8D2EO'><H3>Description</H3></td><td bgcolor='#B8D2EO'><H3>Seeking</H3></td><td bgcolor='#B8D2EO'><H3> Contact User</H3></td></tr>"; while ($row = mysql_fetch_array($result)) { echo "<tr><td align='center'>"; echo '<img src="' . $row['imgpath'] . '" width="125" alt="" />'; echo "</td><td align='center'>"; echo $row['item_name']; echo "</td><td align='center'>"; echo $row['description']; echo "</td><td align='center'>"; echo $row['in_return']; echo "</td><td align='center'>"; echo '<a href="composemessage.php?uid='.$row['user_id'].'"><img src="images/K_little.png" style="border: 0px;" /></a>'; echo "</td></tr>"; } ?> <?php include "connection.php"; mysql_connect("$host", "$username", "$password") or die("Could not connect."); mysql_select_db("$db_name") or die("Could not find database"); $uid = $_GET['uid']; // Added to get the id from the URL $query = "SELECT username FROM members WHERE user_id = '".$uid."'"; echo $query; // see what the query looks like exit; //stop script $result = mysql_query($query) or die(mysql_error()); // Added or die for debugging $user = mysql_fetch_array($result); echo '<form>'; echo '<input type="text" name="to" value="'.$uid['username'].'" />'; echo '</form>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/154575-solved-help-please/#findComment-814059 Share on other sites More sharing options...
Maq Posted April 19, 2009 Share Posted April 19, 2009 You took the exit; out, right? When you echo out the sql statement does it look correct? Are you still getting the "column not found" error? If so, go to your database and ensure it's exactly the same. Also, when you call the extracted you're using $uid rather than $user. $user = mysql_fetch_array($result); echo '</pre> <form>'; echo '';< needs to be: $user = mysql_fetch_array($result); echo '</pre> <form>'; echo ''; // CHANGED< Quote Link to comment https://forums.phpfreaks.com/topic/154575-solved-help-please/#findComment-814062 Share on other sites More sharing options...
justAnoob Posted April 19, 2009 Author Share Posted April 19, 2009 unknow colum "user_id" in "where clause" is the error i get when the exit; is commented out Quote Link to comment https://forums.phpfreaks.com/topic/154575-solved-help-please/#findComment-814067 Share on other sites More sharing options...
justAnoob Posted April 19, 2009 Author Share Posted April 19, 2009 and i did make the change as you said.... there are two tables that I'm working with..... members(which has the id and username) and member_trades(which has user_id, which is the same as the id back in the members table) Quote Link to comment https://forums.phpfreaks.com/topic/154575-solved-help-please/#findComment-814069 Share on other sites More sharing options...
Maq Posted April 19, 2009 Share Posted April 19, 2009 unknow colum "user_id" in "where clause" is the error i get when the exit; is commented out Ok, well does it exist? Quote Link to comment https://forums.phpfreaks.com/topic/154575-solved-help-please/#findComment-814070 Share on other sites More sharing options...
justAnoob Posted April 19, 2009 Author Share Posted April 19, 2009 ok,,, sorry,, i needed to change it to "id" not "user_id" in the line below.. But still having problem... All it shows is a empty text box. $query = "SELECT username FROM members WHERE id = '".$uid."'"; Quote Link to comment https://forums.phpfreaks.com/topic/154575-solved-help-please/#findComment-814081 Share on other sites More sharing options...
justAnoob Posted April 19, 2009 Author Share Posted April 19, 2009 i do not think that the GET command is working,, <?php $query = "SELECT imgpath, item_name, description, in_return FROM member_trades WHERE category = 'Video Games' $limit"; $uid = $_GET['user_id']; $result = mysql_query($query) or trigger_error("SQL", E_USER_ERROR); echo "<table border='1' bgcolor='#FFFFFF'><tr><td bgcolor='#B8D2EO'><H3>Image</h3></td><td bgcolor='#B8D2EO'><H3>Item Name</H3></td><td bgcolor='#B8D2EO'><H3>Description</H3></td><td bgcolor='#B8D2EO'><H3>Seeking</H3></td><td bgcolor='#B8D2EO'><H3> Contact User</H3></td></tr>"; while ($row = mysql_fetch_array($result)) { echo "<tr><td align='center'>"; echo '<img src="' . $row['imgpath'] . '" width="125" alt="" />'; echo "</td><td align='center'>"; echo $row['item_name']; echo "</td><td align='center'>"; echo $row['description']; echo "</td><td align='center'>"; echo $row['in_return']; echo "</td><td align='center'>"; echo '<a href="composemessage.php?uid='.$row['user_id'].'"><img src="images/K_little.png" style="border:0px;" /></a>'; echo "</td></tr>"; } ?> when i click on the picture and it goes to composemessage.php,,,,,, should the url have the username at the end of it. this is what it looks like now composemessage.php?uid= shouldn't the users name be after the = Quote Link to comment https://forums.phpfreaks.com/topic/154575-solved-help-please/#findComment-814085 Share on other sites More sharing options...
Maq Posted April 19, 2009 Share Posted April 19, 2009 When you echo out the query string what does it output? Post your current code. i do not think that the GET command is working,, Does it show up in the URL when you get to the next page? Quote Link to comment https://forums.phpfreaks.com/topic/154575-solved-help-please/#findComment-814086 Share on other sites More sharing options...
justAnoob Posted April 19, 2009 Author Share Posted April 19, 2009 no it does not. read my last post above,,, Quote Link to comment https://forums.phpfreaks.com/topic/154575-solved-help-please/#findComment-814088 Share on other sites More sharing options...
justAnoob Posted April 19, 2009 Author Share Posted April 19, 2009 in my query string shouldn't i be selecting the user_id also... i just noticed that it wasn't there... would that make a difference???? <?php "SELECT imgpath, item_name, description, in_return FROM member_trades WHERE category = 'Video Games' $limit"; ?> change it to <?php "SELECT imgpath, item_name, description, in_return, user_id FROM member_trades WHERE category = 'Video Games' $limit"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/154575-solved-help-please/#findComment-814090 Share on other sites More sharing options...
justAnoob Posted April 19, 2009 Author Share Posted April 19, 2009 suprisingly the change that I just made worked... Quote Link to comment https://forums.phpfreaks.com/topic/154575-solved-help-please/#findComment-814092 Share on other sites More sharing options...
justAnoob Posted April 19, 2009 Author Share Posted April 19, 2009 Thanks for all the help with everything. I do appreciate the time that people put out just to help someone else out. Quote Link to comment https://forums.phpfreaks.com/topic/154575-solved-help-please/#findComment-814094 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.