Insert not working, except in mysql?

admin

Administrator
Staff member
Hello all,

I have a weird problem. I'm trying to submit data to two tables in a mysql database. The first insert works, but the second insert doesn't. If I print out the sql statement that I'm trying to use and cut and paste it into a mysql client it is accepted fine. Why won't it work in my script? Here is what the tables look like:
mysql> describe accounts;
+---------------+---------------------+------+-----+------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------------------+------+-----+------------+----------------+
| id | int(10) unsigned | | PRI | NULL | auto_increment |
| username | varchar(64) | | | | |
| password | varchar(32) | |
| | |
| gender | char(1) | | | | |
| preference | char(1) | | | | |
| birthday | date | | | 0000-00-00 | |
| description | varchar(64) | YES | | NULL | |
| zipcode | varchar(5) | | | | |
| num_images | tinyint(3) unsigned | YES | | NULL | |
| primary_image | varchar(255) | YES | | NULL | |
+---------------+---------------------+------+-----+------------+----------------+
10 rows in set (0.00 sec)

mysql> describe priv_account_info;
+------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+-------+
| account_id | int(10) unsigned | | PRI | 0 | |
| first_name | varchar(40) | | | | |
| last_name | varchar(40) | | | | |
| address | varchar(100) | | | | |
| city | varchar(40) | | | | |
| state | char(2) | | | | |
| phone | varchar(10) | | | | |
| email | varchar(255) | | | | |
+------------+------------------+------+-----+---------+-------+
8 rows in set (0.00 sec)


The code that I'm using looks like:

MYSQL_CONNECT("localhost","my_username","my_password");
mysql_select_db("mtss");


$sql = "INSERT INTO accounts SET " .
"username='$username', " .
"password='$password', " .
"gender='$gender', " .
"preference='$preference', " .
"birthday='$year-$month-$day', " .
"description='$description', " .
"zipcode='$zipcode', " .
"num_images='0', " .
"primary_image='';";

echo("The first SQL statement: $sql<br><br>\n");

$result=MYSQL_QUERY($sql);
if (!mysql_query($sql)) {
echo("<P>Error adding information to accounts: " .
mysql_error() . "</P>");
exit();
}

$insert_id= mysql_insert_id();

$sql = "INSERT INTO priv_account_info SET " .
"account_id='$insert_id', " .
"first_name='$first_name', " .
"last_name='$last_name', " .
"address='$address', " .
"city='$city', " .
"state='$state', " .
"phone='$phone', " .
"email='$email'; ";

echo("The second SQL statement: $sql\n");
$result=MYSQL_QUERY($sql);
if (!mysql_query($sql)) {
echo("<P>Error adding information to accounts: " .
mysql_error() . "</P>");
exit();
}

MYSQL_CLOSE();


The print outs and error I get is:

The first SQL statement: INSERT INTO accounts SET username='dlsf56', password='p', gender='M', preference='H', birthday='1985-12-15', description='lsdkjf', zipcode='flkdj', num_images='0', primary_image='';

The second SQL statement: INSERT INTO priv_account_info SET account_id='6', first_name='joe ', last_name='blow', address='ldskfj', city='lsfkj', state='AK', phone='dlsfkj', email='[email protected]';

Error adding information to accounts: You have an error in your SQL syntax near '; ' at line 1
 
Back
Top