Jump to content


Photo

How to create a dropdown menu with items from a DB


  • Please log in to reply
7 replies to this topic

#1 techiefreak05

techiefreak05
  • Members
  • PipPipPip
  • Advanced Member
  • 494 posts
  • LocationER, MN

Posted 18 August 2006 - 05:53 AM

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 shortener with advanced, detailed statistics:

http://tyny.us/

#2 Jeremysr

Jeremysr
  • Members
  • PipPipPip
  • Advanced Member
  • 199 posts
  • LocationSaskatchewan, Canada

Posted 18 August 2006 - 05:55 AM

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.

#3 techiefreak05

techiefreak05
  • Members
  • PipPipPip
  • Advanced Member
  • 494 posts
  • LocationER, MN

Posted 18 August 2006 - 06:06 AM

I made this: but it dont work, it displays "Username does not exist" always, and it sends the message anyway

<?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>

Link shortener with advanced, detailed statistics:

http://tyny.us/

#4 techiefreak05

techiefreak05
  • Members
  • PipPipPip
  • Advanced Member
  • 494 posts
  • LocationER, MN

Posted 18 August 2006 - 06:08 AM

function userExists($username){
should be
function userExists($to){

i just realized that. lol. but it still deosnt work
Link shortener with advanced, detailed statistics:

http://tyny.us/

#5 Jeremysr

Jeremysr
  • Members
  • PipPipPip
  • Advanced Member
  • 199 posts
  • LocationSaskatchewan, Canada

Posted 18 August 2006 - 06:11 AM

Try this for the userExists function...

function userExists($to){
   global $conn;
   $q = "select username from users where name = $to";
   $result = mysql_query($q,$conn);
   return (mysql_numrows($result) > 0);
}

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..

#6 techiefreak05

techiefreak05
  • Members
  • PipPipPip
  • Advanced Member
  • 494 posts
  • LocationER, MN

Posted 18 August 2006 - 06:24 AM

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 shortener with advanced, detailed statistics:

http://tyny.us/

#7 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 18 August 2006 - 01:37 PM

try
function userExists($a){
   global $conn;
   $q = "select username from users where username = '$a'";
   $result = mysql_query($q,$conn);
   return (mysql_numrows($result) > 0);
}


#8 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 18 August 2006 - 01:46 PM

mysql_numrows() should be mysql_num_rows()...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users