MySQL Insert Statement Problem

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
brookside
Forum Commoner
Posts: 30
Joined: Tue Mar 02, 2004 8:15 pm

MySQL Insert Statement Problem

Post by brookside »

I've searched on how to insert data into MySQL tables. I don't understand where I'm going wrong in my code. Some of these fields are specified as "not null" others are required. When I try to enter data I make sure the fields that are not null and required have information in them. The data is pulled from a form and inserted into variables and then inserted into the MySQL table with the statement written below. The error I get is a parse error for this line.

Any help is greatly appreciated.


INSERT INTO MEMBER VALUES('$Member_Number', '$Big_Brother', '$Title', '$First_Name', '$Middle_Name', '$Last_Name', '$Maiden_Name', '$Nickname', '$Animal_Family', '$Member_Type', '$Primary_Instrument', '$Extra_Information', '$Hometown_State', '$Hometwon_City', '$Primary_Major', '$Secondary_Major', '$Deceased_Date', '$Website', '$Primary_Email', '$Secondary_Email', '$Birthdate_Month', '$Birthdate_Day', '$Birthdate_Year', '$Last_Updated');
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

You'll need to put it in context, ie post the actual PHP code that does the inserting.
coreycollins
Forum Commoner
Posts: 67
Joined: Sun Feb 01, 2004 1:04 pm
Location: Michigan

Post by coreycollins »

One suggestion I have has to do with how you are doing this...

are you doing something like this:

Code: Select all

$strSQL = "INSERT INTO MEMBER VALUES('$Member_Number', '$Big_Brother', '$Title', '$First_Name', '$Middle_Name', '$Last_Name', '$Maiden_Name', '$Nickname', '$Animal_Family', '$Member_Type', '$Primary_Instrument', '$Extra_Information', '$Hometown_State', '$Hometwon_City', '$Primary_Major', '$Secondary_Major', '$Deceased_Date', '$Website', '$Primary_Email', '$Secondary_Email', '$Birthdate_Month', '$Birthdate_Day', '$Birthdate_Year', '$Last_Updated')";
If so, make sure the quotes are in the right spots like mine. Notice the ; is outside the quote, not right after the ). What is the exact error and how are you using this in your code. With more information I should be able to help you...
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

please post a couple of lines of code around that line.
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

I may be wrong as no one else pointed it out, but single quotes don't "translate" variables, do they? So...

Code: Select all

$var1="Name";
echo "$var1";
//returns Name

$var2="Home";
echo '$var2';
//returns $var2
....right?
penguinboy
Forum Contributor
Posts: 171
Joined: Thu Nov 07, 2002 11:25 am

Post by penguinboy »

^ Correct.
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

but in this case the single quotes are inside of the double quotes so they don't get parsed as quotes but as part of the string.
You need to list the names of the columns like this:

Code: Select all

INSERT INTO MEMBER (member_number, big_brother, etc) VALUES ('$member_number', '$big_brother', '$etc')
... at least i think you do.
brookside
Forum Commoner
Posts: 30
Joined: Tue Mar 02, 2004 8:15 pm

Post by brookside »

Ok, I'm new at MySQL and PHP. I am trying to insert data into a table and this is the statement I'm using:

Code: Select all

mysql_query("INSERT INTO MEMBER(Member_ID, Big_ID, Address_ID, Name_Title, Name_First, Name_Middle, Name_Last, Name_Maiden, Name_Nickname, Animal_Family, Member_Type, Primary_Instrument, Extra_Info, Hometown_State, Hometown_City, Primary_Major, Secondary_Major, Deceased_Date, Website, Primary_Email, Secondary_Email, Birthdate_Month, Birthdate_Day, Birthdate_Year, Last_Update) VALUES('Null', '$Big_Brother', '$Title', '$First_Name', '$Middle_Name', '$Last_Name', '$Maiden_Name', '$Nickname', '$Animal_Family', '$Member_Type', '$Primary_Instrument', '$Extra_Information', '$Hometown_State', '$Hometwon_City', '$Primary_Major', '$Secondary_Major', '$Deceased_Date', '$Website', '$Primary_Email', '$Secondary_Email', '$Birthdate_Month', '$Birthdate_Day', '$Birthdate_Year', '$Last_Updated')");


All of this data is retrieved from a form. When I go to submit the form, I don't get any errors. The problem is when I go back to look at the tables the data I submitted is not entered.

With my previous post I didn't know you had to put mysql_query("INSERT whatever"); to insert data.

I hope this makes sense. Please let me know if any clarification is needed or more information is needed.
Pozor
Forum Commoner
Posts: 74
Joined: Tue Mar 30, 2004 11:11 pm
Location: Switzerland

Post by Pozor »

hello,

look at this:

viewtopic.php?t=17096

try mysql_error() out

print the sql query out (that what you have just before you process it)

and then tell us about the result you got.


greez pozor
brookside
Forum Commoner
Posts: 30
Joined: Tue Mar 02, 2004 8:15 pm

Post by brookside »

I figured out the initial problem, I forgot to select the database to use after opening a connection to the server.

I do have another question though.

Is there a certain way variables have to be typed when using them an insert statement? Or can I just say:

Code: Select all

mysql_query("INSERT INTO tablename VALUES(variable1, variable2, etc...)");
Do I need single quotes or double quotes around the variable names? What if the field in the database isn't a VARCHAR data type, such as dates or ints and I want to insert variables with these data type values?

I hope that makes sense.

I appreciate all your help.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

$sql = "INSERT INTO tablename VALUES('$var1','$var2','$var3')";

But sure to read about escaping the values first though ;)
Post Reply