Results 1 to 2 of 2

Thread: how to register unique data into my table using php?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    208

    how to register unique data into my table using php?

    i have the following table.
    create table tbuser
    (username varchar(50) unique,
    password varchar(255))
    then again i hv the following html code register.php
    Code:
    <html>
    <body>
    register now<p>
    <table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
    <tr>
    <td><form name="form1" method="post" action="insert_ac.php">
    <table width="100%" border="0" cellspacing="1" cellpadding="3">
    <tr>
    <td colspan="3"><strong>Insert Data Into mySQL Database </strong></td>
    </tr>
    <tr>
    <td width="71">Name</td>
    <td width="6">:</td>
    <td width="301"><input name="username" type="text" id="username"></td>
    </tr>
    <tr>
    <td>Lastname</td>
    <td>:</td>
    <td><input name="password" type="password" id="password"></td>
    </tr>
    <tr>
    <td>Email</td>
    <td>:</td>
    
    </tr>
    <tr>
    <td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
    </tr>
    </table>
    </form>
    </td>
    </tr>
    </table>
    </body>
    and the insert_ac.php is as follow.
    Code:
    <?php
    
    $host="localhost"; // Host name
    $username="root"; // Mysql username
    $password=""; // Mysql password
    $db_name="test"; // Database name
    $tbl_name="users"; // Table name
    
    // Connect to server and select database.
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");
    
    // Get values from form
    $username=$_POST['username'];
    $password=$_POST['password'];
    
    
    // Insert data into mysql
    $sql="INSERT INTO $tbl_name(username, password)VALUES('$username', '$password')";
    $result=mysql_query($sql);
    
    // if successfully insert data into database, displays message "Successful".
    if($result){
    echo "Successful";
    echo "<BR>";
    echo "<a href='insert.php'>Back to main page</a>";
    }
    
    else {
    echo "ERROR";
    }
    
    // close connection
    mysql_close();
    ?>
    now this code inserts data seccessfuly, but i want to make sure that evey user must have unique USERNAME,
    i register using username=testuser and password with="test"
    then if someone tries to register using my username which is already in the databas, there must be an error message that indicates him, the username is already taken, try an other username.
    so how can i code this?thanks

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: how to register unique data into my table using php?

    you need to make a quick SQL query before inserting to check if that user exists.
    PHP Code:
    $sql "SELECT username FROM $tbl_name WHERE username='{$_POST['username']}'";
    $namecheck mysql_query($sql);
    if(
    mysql_num_rows($namecheck) > 0){
      echo 
    "this name already exists.";
    }else{
      echo 
    "this name does not exist.";


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width