MySQL problems

Discussion in 'PHP' started by derek34, Dec 1, 2007.

  1. #1
    Hi I'm trying to write the contents of a form to a MySQL database but when the PHP script tries to write to the table and error comes up:
    Here's the code I have:
    <?php
    $cid = mysql_connect('host', 'username', 'password')or die(mysql_error());
    
     
     mysql_select_db('lanreg', $cid) or die(mysql_error());
    
    
     $q = "CREATE TABLE IF NOT EXISTS lanreg(
    	id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
    	screenname VARCHAR(9),
    	firstname VARCHAR(32) NOT NULL,
    	lastname VARCHAR(32) NOT NULL,
    	age INT(2) NOT NULL,
    	school VARCHAR(64) NOT NULL,
    	email VARCHAR(64) NOT NULL
    	phone INT(64) NOT NULL,
    	city VARCHAR(64) NOT NULL,
    	renting VARCHAR(64) NOT NULL,
    	pizza CHAR(25) NOT NULL,
    	games VARCHAR(64),
    	movies VARCHAR(64),
    	tournaments VARCHAR(64),
    	comments VARCHAR(64)
     
     );";
    
     mysql_query($q) or die(mysql_error());
    
    
    
    
     $q = "INSERT INTO lanreg (
    	id,
    	screenname,
    	firstname,
    	lastname,
    	age,
    	school,
    	email,
    	phone,
    	city,
    	renting, 
    	pizza,
    	games,
    	movies,
    	tournaments,
    	comments
    	) VALUES(
    	'$_POST[screen]',
    	'$_POST[first]',
    	'$_POST[last]',
    	'$_POST[age]',
    	'$_POST[email]',
    	'$_POST[phone]',
    	'$_POST[city]',
    	'$_POST[rent]',
    	'$_POST[pizza]',
    	'$_POST[games]',
    	'$_POST[movies]',
    	'$_POST[tourn]',
    	'$_POST[comments]'
    	);";
     
     mysql_query($q) or die(mysql_error());
     mysql_close($cid);
    }
    ?>
    PHP:
    It connects fine, but there are just some errors.
    Any idea why?
    Any help is greatly appreciated.
    Thanks
    - Derek
     
    derek34, Dec 1, 2007 IP
  2. *louie*

    *louie* Peon

    Messages:
    48
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    before you execute the query use echo to see the format it goes in:
    
    echo $q;
    mysql_query($q) or die(mysql_error()); 
    mysql_close($cid);
    
    PHP:
    also you need to sanitize the data before doing the insert.
     
    *louie*, Dec 2, 2007 IP
  3. Evoleto

    Evoleto Well-Known Member

    Messages:
    253
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #3
    You are missing a comma after the following line in the table creation code:

    
    email VARCHAR(64) NOT NULL
    
    Code (markup):
     
    Evoleto, Dec 2, 2007 IP
  4. derek34

    derek34 Guest

    Messages:
    34
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks for the tips Louie, but the comma was the only problem. Thanks to Evoleto for noticing that.
     
    derek34, Dec 2, 2007 IP