Inserting

He has: 296 posts

Joined: May 2002

I'm trying to add a User CP to my site and I have a problem. Nothing gets inserted into the database. Here's my code for the insert:

<?php
                    $q1
= mysql_query(\"INSERT INTO `ata_users` (`id`, `username`, `password`, `email`, `aim`, `icq`, `msn`, `active`) VALUES (\'\', $username, $password, $email, $aim, $icq, $msn, \'0\')\");
?>

I've done this before, and I see nothing wrong with the code. But it could just be me. My DB is set up like this:

ata_users
------------------
id TINYINT(6) NOT NULL auto_increment PRIMARY KEY,
username VARCHAR(30) NOT NULL,
password VARCHAR(30) NOT NULL,
email VARCHAR(255) NOT NULL,
aim VARCHAR(255) NOT NULL,
icq TINYINT(100) NOT NULL DEFAULT '0',
msn VARCHAR(255) NOT NULL,
active TINYINT(6) NOT NULL DEFAULT '0'
'

[James Logsdon]

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

omit the `id` field from your query

nike_guy_man's picture

They have: 840 posts

Joined: Sep 2000

Don't you also need quotes around the values?? I've ran into that problem a few times...
Are you getting any errors?

Peter J. Boettcher's picture

They have: 812 posts

Joined: Feb 2000

Mark is right, you don't need to include the id field since it is auto increment. nike_guy_man is also correct, while it will work without quotes around your variables you really should include them because if your variable has a comma or a double quote in it then it will break your query.

PJ | Are we there yet?
pjboettcher.com

mairving's picture

They have: 2,256 posts

Joined: Feb 2001

You can use the id field if you want. Before I knew better, I used to do it as insert into tablename (id) values ('NULL'). This forces the auto-increment to happen. It is much cleaner and easier to leave it off though. Any time I run into a problem like this, I usually try echoing the statement to the screen to make sure that I am picking up the right data and then try inserting the same SQL statement without using the script, either through the command line or phpmyadmin. This tends to help solve all of those mysteries.

Mark Irving
I have a mind like a steel trap; it is rusty and illegal in 47 states

He has: 296 posts

Joined: May 2002

Thanks guys! It works like a dream now! I thought it would be something simple like that.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.