Page 1 of 1

MySQL Insert Statement Problem

Posted: Wed Mar 17, 2004 8:01 pm
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');

Posted: Wed Mar 17, 2004 8:03 pm
by markl999
You'll need to put it in context, ie post the actual PHP code that does the inserting.

Posted: Wed Mar 17, 2004 8:07 pm
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...

Posted: Thu Mar 18, 2004 3:35 am
by Wayne
please post a couple of lines of code around that line.

Posted: Fri Mar 19, 2004 9:43 am
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?

Posted: Fri Mar 19, 2004 11:03 am
by penguinboy
^ Correct.

Posted: Fri Mar 19, 2004 12:24 pm
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.

Posted: Wed Mar 31, 2004 9:36 am
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.

Posted: Thu Apr 01, 2004 1:57 am
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

Posted: Thu Apr 01, 2004 9:40 am
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.

Posted: Thu Apr 01, 2004 9:45 am
by markl999
$sql = "INSERT INTO tablename VALUES('$var1','$var2','$var3')";

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