Create table with sql

Discussion in 'PHP' started by fex, Mar 10, 2009.

  1. #1
    Hello,

    I wish to use a database to allow admins to register/login on my webpage.

    I've setup the database in phpMyAdmin with this query:
    CREATE TABLE Admins
    (
    	id int(10) AUTO_INCREMENT,
    	PRIMARY KEY(id),
    	username varchar(16),
    	UNIQUE(username),
    	password char(16),
    	email varchar(30),
    	UNIQUE(email)
    );
    Code (markup):
    When I clear my database and try to register there's being created 1 row in table Admins with id=1 and the other fields (username, password, email) empty. If I try to register with other data, it just won't let me.

    Also I've noticed that when viewing my table Admins in phpMyAdmin the standard value of fields username, password, email are set to NULL. I'm assuming my query isn't correct..
     
    fex, Mar 10, 2009 IP
  2. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I don't think it has anything to do with your table creation, but with your insert query in the registration process.
     
    SmallPotatoes, Mar 10, 2009 IP
  3. fex

    fex Peon

    Messages:
    89
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Mmmmh.. Here's what that piece of code looks like:
    $q = "INSERT INTO `Admins` (`username`,`password`,`email`) "
    ."VALUES ('".$_POST["username"]."', "
    ."PASSWORD('".$_POST["password"]."'), "
    ."'".$_POST["email"]."')";
    
    $r = mysql_query($q);
    
    Code (markup):
    EDIT:

    This error pops up trying to register a second user:
    if (!mysql_insert_id()){
    echo "mysql_insert_id: " . mysql_insert_id();
    die("<p style='margin-left:50px; font-style:italic; color:red;'>Error: User not added to database.</p>");
    }
    Code (markup):
    I was interested in the mysql_insert_id, which shows a value of 0 when throwing the error..
     
    fex, Mar 10, 2009 IP
  4. fex

    fex Peon

    Messages:
    89
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You were right SmallPotatoes,,

    Working:
    $q = "INSERT INTO Admins (username, password, email) VALUES ('".$_POST["username"]."', '".$_POST["password"]."', '".$_POST["email"]."');";
    Code (markup):
    The mysql_insert_id thingy is solved too. I have a simple 'validation code' set up on the register page. Because you do not everyone want to be able to register as admin of course. What happened was that that protection popped up again between entering your info and between actually sending it, so mysql_insert_id got lost and the error showed. I fixed that by adding a hidden field to the 'admin register' form with the value set to the code. As simple as that to prevent it from popping up again.

    Thx for ur fast response, it put me to thinking ;)
     
    fex, Mar 10, 2009 IP
  5. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #5
    Something else to thing about.

    If your going to use the password() mysql function make the varchar 41 bytes long.
     
    exodus, Mar 10, 2009 IP