I am getting an error code "error on line 2" regarding apostrophes. I think what is happening is in the text fields when a ' is typed it is effecting the code outside the "" and causing an error. How do i make marks within the text field not effect the remainder of the code?
the html elements in question are q25 and q35
The site is live at http://educationofthedesigner.com/survey.html after you submit it sends you to the php page.
error __
Error: INSERT INTO survey2 (Email, School, Major, Degree, Status, Sex, Age, Q7, Q8, Q9, Classes,
Q11, Q12, Q13, Q14, Q15, Q16, Q17, Q18, Q19, Q20, Q21, Q22, Q23, Q24, Q25, Q26, Q27,
Q28, Q29, Q30, Q31, Q32, Q33, Q34, Q35, Q36, Q37, Q38, Q39, Q40, Q41, Q42, Q43)
VALUES ('', 'Purchase College', 'Graphic Design', 'BFA', 'Senior', 'Male', '1994', 'no',
'no', 'yes', 'branding, web/interactive, print, art direction, social design,
design theory, design authorship, type design, book arts, printmaking,
letterpress, design history', 'yes', '19 to 21', '11 to 20', 'no', '7', '3',
'1', '8', '2', '10', '3', '3', 'no', '30+', 'We don't focus on
skills/encouraging the development of a marketable portfolio at all.
There are students in my program who still don't understand basic precepts
of design, and more importantly don't understand how to teach themselves
new techniques. We're rooted in a homogenous visual culture that encourages
illiteracy in the tools of the trade as a hallmark of its style.', 'yes',
'studio', 'yes', 'yes', '20-25', '4', '2', '6', 'on capus', 'A drab closet
of a room filled with computers and devoid of windows and non-fluorescent
light. Big tables for cutting. No food or drink.', 'financial', 'yes', 'no',
'10', '7', 'yes', 'essential', 'Not Really')
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server
version for the right syntax to use near 't focus on skills/encouraging the development of
a marketable portfolio at all. ' at line 2
You just need to escape apostrophes inside values apostrophes with slashes
INSERT INTO survey2 (Email, School, Major, Degree, Status, Sex, Age, Q7, Q8, Q9, Classes, Q11, Q12, Q13, Q14, Q15, Q16, Q17, Q18, Q19, Q20, Q21, Q22, Q23, Q24, Q25, Q26, Q27, Q28, Q29, Q30, Q31, Q32, Q33, Q34, Q35, Q36, Q37, Q38, Q39, Q40, Q41, Q42, Q43) VALUES ('', 'Purchase College', 'Graphic Design', 'BFA', 'Senior', 'Male', '1994', 'no', 'no', 'yes', 'branding, web/interactive, print, art direction, social design, design theory, design authorship, type design, book arts, printmaking, letterpress, design history', 'yes', '19 to 21', '11 to 20', 'no', '7', '3', '1', '8', '2', '10', '3', '3', 'no', '30+', 'We don\'t focus on skills/encouraging the development of a marketable portfolio at all. There are students in my program who still don\'t understand basic precepts of design, and more importantly don\'t understand how to teach themselves new techniques. We\'re rooted in a homogenous visual culture that encourages illiteracy in the tools of the trade as a hallmark of its style.', 'yes', 'studio', 'yes', 'yes', '20-25', '4', '2', '6', 'on capus', 'A drab closet of a room filled with computers and devoid of windows and non-fluorescent light. Big tables for cutting. No food or drink.', 'financial', 'yes', 'no', '10', '7', 'yes', 'essential', 'Not Really')
A basic example
INSERT INTO table (a, b, c) VALUES (1, 'hello', 'don\'t know');
-- ^ note the slash here
Or otherwise you can use php functions to deal with apostrophes as addslashes()
The errors occurs because mysql end the string to the first apostrophe found after the opening one for any values, the rest of the string will be not surrounded with apostrophe.
You should filter your form before adding data to database.
The easiest solution is to filter the data and remove apostrophe:
$string = str_replace("'", "", $string);
or if you prefer to have an apostrophe in your data use:
$string = str_replace("'", "'", $string);
Think about filtering other data, not only apostrophe. Google for 'sanitize php' or 'filter input' for more information.
I think that you are looking for Prepared Statements which should prevent from error like this.
First you create your statement and declare how many items you want to update/insert or whatever. Than you declare type (s-string,i-integer) of variables and variables itself.
Look here: http://www.w3schools.com/php/php_mysql_prepared_statements.asp