techiefreak05 Posted August 18, 2006 Share Posted August 18, 2006 How would i do this? I have a message system on my site, and the user has to manually enter the username o send the message to, what if they spell it wrong? then it would just take up space on my DB, so instead of a text box, id like a dropdown menu that has all the usernames on my site. Link to comment https://forums.phpfreaks.com/topic/17913-how-to-create-a-dropdown-menu-with-items-from-a-db/ Share on other sites More sharing options...
Jeremysr Posted August 18, 2006 Share Posted August 18, 2006 Why not just check to see if the username they entered exists in the username database? That would be much easier for you and your users. Link to comment https://forums.phpfreaks.com/topic/17913-how-to-create-a-dropdown-menu-with-items-from-a-db/#findComment-76585 Share on other sites More sharing options...
techiefreak05 Posted August 18, 2006 Author Share Posted August 18, 2006 I made this: but it dont work, it displays "Username does not exist" always, and it sends the message anyway[code]<?php if($_POST['sendMess']){$to = $_POST['to'];$from = $_SESSION['username'];$message = $_POST['message'];$subject = $_POST['subject'];$date = date("D M j G:i:s T Y");function userExists($username){ global $conn; $q = "select username from users"; $result = mysql_query($q,$conn); return (mysql_numrows($result) > 0);} if(userExists($_POST['to'])){mysql_query("INSERT INTO `messages` ( `from` , `to` , `message` , `date` , `status` , `subject`)VALUES ('$from', '$to', '$message', '$date', 'no', '$subject');") or die("There was an error sending your message");mysql_query("INSERT INTO `outbox` ( `from` , `to` , `message` , `date` , `status` , `subject`)VALUES ('$from', '$to', '$message', '$date', 'no', '$subject');") or die(mysql_error());echo "<font color=red>Your Message has been sent!</font>";} }else{echo "That username does not exist";}?><form action="" method="post"> <table align="center" border="0" cellspacing="0" cellpadding="3"><tr><td ><font color=black>To:</font></td><td><input type="text" name="to" size="35"></td></tr><tr><td ><font color=black>Subject</font></td><td><input type="text" name="subject" size="35"></td></tr><tr><td ><font color=black>Message:</font></td><td><textarea name="message" rows="10" cols="55"></textarea></td></tr><tr><td colspan="2" align="right"><input type="submit" name="sendMess" value="-Send Message-"></td></tr></table></form>[/code] Link to comment https://forums.phpfreaks.com/topic/17913-how-to-create-a-dropdown-menu-with-items-from-a-db/#findComment-76588 Share on other sites More sharing options...
techiefreak05 Posted August 18, 2006 Author Share Posted August 18, 2006 [code]function userExists($username){[/code]should be[code]function userExists($to){[/code]i just realized that. lol. but it still deosnt work Link to comment https://forums.phpfreaks.com/topic/17913-how-to-create-a-dropdown-menu-with-items-from-a-db/#findComment-76589 Share on other sites More sharing options...
Jeremysr Posted August 18, 2006 Share Posted August 18, 2006 Try this for the userExists function...[code=php:0]function userExists($to){ global $conn; $q = "select username from users where name = $to"; $result = mysql_query($q,$conn); return (mysql_numrows($result) > 0);}[/code]I dunno how your database is set up, so I don't know if it's "where name = $to" or "where username = $to" or whatever.. Link to comment https://forums.phpfreaks.com/topic/17913-how-to-create-a-dropdown-menu-with-items-from-a-db/#findComment-76591 Share on other sites More sharing options...
techiefreak05 Posted August 18, 2006 Author Share Posted August 18, 2006 It worked. but it didnt send the message even if the username did exist! its screwed up. could someone write something that checks to see if, the vaule of the text box(to) equals a username in my DB, called users Link to comment https://forums.phpfreaks.com/topic/17913-how-to-create-a-dropdown-menu-with-items-from-a-db/#findComment-76594 Share on other sites More sharing options...
sasa Posted August 18, 2006 Share Posted August 18, 2006 try[code]function userExists($a){ global $conn; $q = "select username from users where username = '$a'"; $result = mysql_query($q,$conn); return (mysql_numrows($result) > 0);}[/code] Link to comment https://forums.phpfreaks.com/topic/17913-how-to-create-a-dropdown-menu-with-items-from-a-db/#findComment-76720 Share on other sites More sharing options...
HeyRay2 Posted August 18, 2006 Share Posted August 18, 2006 [b]mysql_numrows()[/b] should be [b]mysql_num_rows()[/b]... Link to comment https://forums.phpfreaks.com/topic/17913-how-to-create-a-dropdown-menu-with-items-from-a-db/#findComment-76733 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.