PHP + MySql null value.

Status
Not open for further replies.
Okay, I'm working with php to add a row to my MySql table that is called 'addresses'. I use the following code to add my row, which works (kind of) perfectly.

Code:
$query="INSERT INTO addresses VALUES
('','$fname','$lname','$bday','$address','$city','$state','$zip')";

Notice the null value in the first position there ('',

The code will not work without that value there, but if it is there, it offsets everything in my database by one. IE. it puts first name in the last name field.

Why? You are my hero if you can explain this to me. Thanks!
 

Don

Ars Tribunus Militum
2,673
First of all...You should make your fields NOT NULL whenever possible... there is no reason to it otherwise unless your talking about an index or something..

Second.. is that first field an auto incrementing integer? If so.. dont give it a value at all...

But DO THIS...

Use the more verbose format of the insert statement, were you specify the collums and the data....that will remove ANY CHANCE of there being something like your seeing occur due to ambiguity of your insert statement.


INSERT INTO addresses (first_name, last_name) VALUES ($firstname, $lastname);




you can also do it like this... which looks kinda like an UPDATE statement...

INSERT INTO addresses SET first_name=$firstname, last_name=$lastname;


But I like the first way better myself...
 
Status
Not open for further replies.