PDA

View Full Version : INSERT using () or not?


rattlsnak
08-18-2008, 07:07 AM
I was looking through some of the tutorials on here and came across a question. In comparing those to a few books and other sites i'm reading, when using the INSERT statement I see two ways, one using the () around the INSERT and one that doesn't.

a. mysql_query("INSERT INTO person (FirstName, LastName, Age)
VALUES ('Peter', 'Griffin', '35')");

or

b. $sql="INSERT INTO person (FirstName, LastName, Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
Does it matter which way you do it? Is the first one for directly insertingand the second one for use through a form?I have seen it both ways is why its confusing.Thanks!

davidj
08-18-2008, 08:46 AM
in your examples...

b. This is just a string assigned to a variable

example...

$sql="INSERT INTO person (FirstName, LastName, Age) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";

what you would have to do after assigning the SQL to a string is this...

mysql_query($sql);

this is the same as doing...
mysql_query("INSERT INTO person (FirstName, LastName, Age)VALUES('Peter', 'Griffin', '35')");

rattlsnak
08-18-2008, 03:58 PM
Thats kind of what i was thinking, but I have also seen it written like this:

$sql=("INSERT INTO person (firstname, lastname, age) VALUES etc...

In other words, I have seen the '(' before the INSERT on a variable statement. Is that wrong?, or could it work that way?

Thanks, as always!!

davidj
08-18-2008, 04:00 PM
i reckon thats invalid

never seen that or done it myself that way before

rattlsnak
08-18-2008, 04:05 PM
ok, i would rather learn the correct way!

one thing ive realized quickly about php, is there are a million different ways to write one task.!

davidj
08-18-2008, 04:14 PM
yes

and when working professionally you have to pickup other programmers work which will be in a totally different style

its a nightmare which is why i always encourage the use of comments and indentation