MySQL Insert Statement Problem
Moderator: General Moderators
MySQL Insert Statement Problem
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');
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');
-
coreycollins
- Forum Commoner
- Posts: 67
- Joined: Sun Feb 01, 2004 1:04 pm
- Location: Michigan
One suggestion I have has to do with how you are doing this...
are you doing something like this:
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...
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')";I may be wrong as no one else pointed it out, but single quotes don't "translate" variables, do they? So...
....right?
Code: Select all
$var1="Name";
echo "$var1";
//returns Name
$var2="Home";
echo '$var2';
//returns $var2- andre_c
- Forum Contributor
- Posts: 412
- Joined: Sun Feb 29, 2004 6:49 pm
- Location: Salt Lake City, Utah
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: ... at least i think you do.
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')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:
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.
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.
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
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
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:
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.
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...)");I hope that makes sense.
I appreciate all your help.
$sql = "INSERT INTO tablename VALUES('$var1','$var2','$var3')";
But sure to read about escaping the values first though
But sure to read about escaping the values first though