Jump to content

How to create a dropdown menu with items from a DB


techiefreak05

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.