if record doesn't exists insert into table else display message

Discussion in 'PHP' started by Kyriakos, Jun 23, 2008.

  1. #1
    hi,

    i want to check in my database table if "username" exists. this is my php script:
    <?php
    $query = "select username from members where username='$username'";
    $ter = mysql_query($query);
    $num = mysql_num_rows($ter);
    
    if($num == 0)
    {
    $sql = "INSERT INTO members (id, username, password, email) VALUES ('', '$_POST[username]', md5('$_POST[password]'), '$_POST[email]')";
    $result = @mysql_query($sql,$connection) or die(mysql_error());
    }
    elseif($num > 0)
    {
    echo "user exists";
    }
    ?>
    PHP:
    this script is working just fine but i want a way to display a message like "this username already exists" without refresh the page. can do it with javascript or AJAX?

    thanks in advance.
     
    Kyriakos, Jun 23, 2008 IP
  2. mmkrulz

    mmkrulz Peon

    Messages:
    197
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    personally, i think this is overkill with something like AJAX. When they submit the whole form, just do your check as part of validation, and when if the form is invalidated, just post all your errors at once, including this one.
     
    mmkrulz, Jun 23, 2008 IP