Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 6 of 6
  1. #1
    Member Anant's Avatar
    Join Date
    Feb 2005
    Location
    Indore , India
    Posts
    50
    Member #
    9049
    hello everyone...

    I have a problem ...i have a form with two fields ...name and address ...and thus table made in mysql according to it ...i am just taking input from user and updating my database .

    now the problem is that i want a unique name for each of the user ...like if some one inputes the name "anant"....second time then he should get the prompt that its being used my other person .....tell me how i can do that thing...

    pls help me out
    Anant

  2.  

  3. #2
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    I use similar code in a php script.

    PHP Code:
    <?php
    // start of php code
    $fullname $_POST['fullname'];
    $address $_POST['address'];
    // we have the variables, now check if name exists
    // the query in pseudocode = select the users whose column is equal to $fullname
    $query "SELECT full_name FROM table_name WHERE full_name='$fullname'";
    // now get the number of rows returned
    $result mysql_query($query);
    $numrows mysql_num_rows($result);
    // now we check the number of rows returned
    if ($numrows == "0") { // if username is unique
      
    $query "INSERT into table_name (fullname, address) VALUES ('$fullname','$address');
    mysql_query(
    $query) or die(mysql_error());
    } else {
      echo "
    The name $fullname is already in the database";
    }
    ?>
    Hope this helps.

  4. #3
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    Bfsog's solution does the trick.

    However, there are numerous ways of accomplishing what you ask for. You can either do it through PHP, or through MySQL depending on preferances.
    Obviously, you need to check what's in your database before you ask MySQL to write new entries.
    _______

    The following is just an observation, do not get insulted, it's not intended that way.

    When I started learning PHP, I utilized my (at that time) mediocre knowledge, to find solutions. Whenever I got stuck, I asked for help. Beeing stuck in this context, ment I had traversed through all my knowledge, and couldn't get PHP to do what I wanted.
    When I asked for help, I was presented with functionality I didn't know was available in PHP. Utilizing the new function calls, or string/array manipulation, I could accomplish my goal.
    I often built solutions that required a lot of coding, but gave me the required result, only to find (later) that there was a function that did all the things I had written, through ONE command.

    There are some topics that can't be easily explained.
    How to translate XML documents, is one of them. There are code functions in PHP (especially PHP 5.X) that handles this elegantly. However, you need to know/understand XML before you ask any help in this arena. Otherwise, it will forever be totally greek to you, and you will never understand what the code is actually doing, and can hence never customize it to your needs.

    The point is, don't be 'lazy'. Use your imagination. If you want to insert unique records into a database, then obviously you need to check wether that record exists or not.
    There are tools in MySQL to do that (hint::unique).

    Or you can solve it through scripting(bfsog suggestion). With regard to how to 'phrase' your script, there are a multitude of solutions.

    There are many "high performance" scripters/programmers here. They will tell you how to optimize your script.
    However, that requires you to do some self study, and come up with a preliminary solution.
    S. Rosland

  5. #4
    Senior Member
    Join Date
    Aug 2003
    Posts
    444
    Member #
    2801
    Quote Originally Posted by bfsog
    I use similar code in a php script.
    PHP Code:
    <?php
    // start of php code
    $fullname $_POST['fullname'];
    $address $_POST['address'];
    ...
    $query "SELECT full_name FROM table_name WHERE full_name='$fullname'"
    ?>
    You do realise that this is a very dangerous piece of software? You need to validate the user input. You never know what the $fullname might actually contain. Something maybe like "x; DROP TABLE ...". That ought to do it

    A very good way to clean up your code is to use this function, found on the PHP website:

    PHP Code:
    <?php
    function SQLCleanUp($value){
       
    // Stripslashes
       
    if (get_magic_quotes_gpc()) {
           
    $value stripslashes($value);
       }
       
    // Quote if not integer
       
    if (!is_numeric($value)) {
           
    $value mysql_real_escape_string($value);
       }
       return 
    $value;
        }
    ?>
    And read this page: http://uk.php.net/mysql_real_escape_string .

    Remember, we don't live in a happy lovey dovey world where everyone gets along.
    eKstreme
    eKstreme.com - Free website tools!
    fontfox - free fonts Hand-picked quality fonts.

  6. #5
    Member Anant's Avatar
    Join Date
    Feb 2005
    Location
    Indore , India
    Posts
    50
    Member #
    9049
    hi...

    Tried the way u told me to do ..but its not working ..the script is not taking input from the form ....

    i know something about unique ..but then my databse will show error ...i want that to be at user level ...

    what to do ???

  7. #6
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Quote Originally Posted by rosland
    Otherwise, it will forever be totally greek to you, and you will never understand what the code is actually doing, and can hence never customize it to your needs.
    < raises hand > Ummm... I know Greek. Does that mean I'll understand anyway?



    </smartass>


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 11:02 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com