Closed Thread
Results 1 to 5 of 5

Thread: SQL or die issue

  1. #1
    hudbarnett is offline Newbie
    Join Date
    Jan 2010
    Posts
    23
    Rep Power
    0

    SQL or die issue

    I have this page and when the user added a username that is in the database already i get a horrid text error. All you see is the white page with the error. I would like to redirect the user to a page or add text which includes all the page HTML so it still has the look of the over all site.

    The line in question is "or die ("Sorry there has been a problem with the registration, please click back and try again");"

    Here is the php code

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <?php
    
    // Check if he wants to register:
    if (!empty($_POST[username]))
    {
        // Check if passwords match.
        if ($_POST[password] != $_POST[password2])
            exit("Error - Passwords don't match. Please go back and try again.");
    
        // Assign some variables.
        $date = mktime("d - m - Y");
        $ip = $_SERVER[REMOTE_ADDR];
    
        require_once("connect.php");
    
        // Register him.
        $query = mysql_query("INSERT INTO members 
        (title, username, firstname, lastname, address, town, county, postcode, mobile, tennis_number, lta_number, itf_itn_number, age, club, website, showdetails, password, date, ip)
        VALUES    ('$_POST[title]','$_POST[username]','$_POST[firstname]','$_POST[lastname]','$_POST[address]','$_POST[town]','$_POST[county]','$_POST[postcode]','$_POST[mobile]','$_POST[tennis_number]','$_POST[lta_number]','$_POST[itf_itn_number]','$_POST[age]','$_POST[club]','$_POST[website]','$_POST[showdetails]','$_POST[password]','$date','$ip')")
        or die ("Sorry there has been a problem with the registration, please click back and try again");
        
        header("location:new_account.php");
            
        exit();
    }
    
    ?>
    Thanks

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    DEViANT's Avatar
    DEViANT is offline Programming Expert
    Join Date
    Aug 2010
    Location
    South Africa
    Posts
    359
    Blog Entries
    4
    Rep Power
    8

    Re: SQL or die issue

    Try the following:

    CODE REMOVED - CHECK NEXT POST

    Check the comment fields for where you have to make changes. Untested - but it SHOULD work.

  4. #3
    DEViANT's Avatar
    DEViANT is offline Programming Expert
    Join Date
    Aug 2010
    Location
    South Africa
    Posts
    359
    Blog Entries
    4
    Rep Power
    8

    Re: SQL or die issue

    Updated :

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <?php
    
    // Check if he wants to register:
    if (!empty($_POST[username]))
    {
        // Check if passwords match.
        if ($_POST[password] != $_POST[password2])
            exit("Error - Passwords don't match. Please go back and try again.");
    
        // Assign some variables.
        $date = mktime("d - m - Y");
        $ip = $_SERVER[REMOTE_ADDR];
    
        require_once("connect.php");
    
        // Register him.
        $q = mysql_query("SELECT username FROM members ORDER BY username ASC") or die(mysql_error());
        while($row = mysql_fetch_assoc($q))
        {
        	$arrnames[$i] = $row['username'];
        	$i++;
        }
        
        if(!in_array($_POST['username'],$arrnames))
        {
        	/*
        		Place what you want to 
        		have displayed in this 
        		block right here. This 
        		will be displayed if a 
        		username was found in 
        		the database that matches 
        		the given username
        		
        	*/
        	exit();
        } else {    
        	$query = mysql_query("INSERT INTO members 
        (title, username, firstname, lastname, address, town, county, postcode, mobile, tennis_number, lta_number, itf_itn_number, age, club, website, showdetails, password, date, ip)
        VALUES    ('$_POST[title]','$_POST[username]','$_POST[firstname]','$_POST[lastname]','$_POST[address]','$_POST[town]','$_POST[county]','$_POST[postcode]','$_POST[mobile]','$_POST[tennis_number]','$_POST[lta_number]','$_POST[itf_itn_number]','$_POST[age]','$_POST[club]','$_POST[website]','$_POST[showdetails]','$_POST[password]','$date','$ip')")
        or die ("Sorry there has been a problem with the registration, please click back and try again");
        
        header("location:new_account.php");
            
        exit();
    }
    
    ?>

  5. #4
    Join Date
    Jun 2010
    Location
    Vancouver, Eh.
    Posts
    4,027
    Blog Entries
    7
    Rep Power
    39

    Re: SQL or die issue

    It's much simpler than that.

    Code:
    $result mysql_query("SELECT username FROM `members` WHERE `username` = '$username'");

    if(
    mysql_num_rows($result) !== 0){
               die(
    "Sorry! The user exists already");

    Note 1: In your top code, do wrap non-constants in single or double quotes, $_POST[username] should be $_POST['username']
    Note 2: mktime() is not correct, use date("d - m - Y")
    Note 3: Consider using mysql_real_escape_string() around your $_POST elements in your query, this will prevent SQL injection.
    Last edited by Alexander; 09-01-2010 at 12:59 AM. Reason: whoops!
    Be sure to read the updated FAQ || Health is achieved through 10,000 different steps.
    A textual description can be only part of your question, be sure to provide sample results, errors and your platform in the appropriate forums while asking.

  6. #5
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Posts
    3,087
    Blog Entries
    7
    Rep Power
    42

    Re: SQL or die issue

    the sql needs to be like this, of course
    Code:
    SELECT username FROM `members` WHERE username = '$username'
    __________________________________________
    I study Information Systems at Karlstad University when I'm not on CodeCall

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VERY simple issue.
    By 12rmcmillin in forum JavaScript and CSS
    Replies: 2
    Last Post: 10-07-2011, 09:19 AM
  2. gcc issue
    By newprog in forum C and C++
    Replies: 4
    Last Post: 11-06-2010, 11:14 PM
  3. PHP 5.3 issue?
    By Pope Viper in forum PHP Development
    Replies: 8
    Last Post: 08-28-2009, 06:28 PM
  4. IE6 Issue
    By Xav in forum JavaScript and CSS
    Replies: 5
    Last Post: 09-27-2008, 01:34 PM
  5. IE Issue
    By Xav in forum JavaScript and CSS
    Replies: 15
    Last Post: 05-30-2008, 01:35 PM

Tags for this Thread

Bookmarks

Posting Permissions

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