I have a table in my database with the following optional fields:
Field Type Null Default
defend varchar(45) No
cause varchar(15) No
offense varchar(45) No
victim varchar(45) No
def_atty varchar(45) Yes NULL
def_atty2 varchar(35) Yes NULL
disp_date date Yes 0000-00-00
disp_com varchar(255) Yes NULL
sent_date date Yes NULL
sent_com varchar(255) Yes NULL
details text Yes NULL
set_date date Yes 0000-00-00
court int(11) Yes NULL
judge tinyint(4) No 0
chair1 tinyint(4) No 0
chair2 tinyint(4) No 0
My HTML form has these fields available for the user, however quite a few are optional fields and won't be filled in. I wrote a function to add the contents of my HTML form to the database.
function addSummary($defend,$cause,$offense,$victim,$def_atty,$def_atty2='',$disp_date=00000000,$disp_com='',$sent_date=00000000,$sent_com='',$detail,$set_date,$court,$judge,$chair1,$chair2) {
$dbmgr = new DBManager();
$addSummary = "Insert INTO tblsummary (defend,cause,offense,victim,def_atty,def_atty2,disp_date,disp_com,sent_date,sent_com,details,set_date,court,judge,chair1,chair2)
VALUES ('$defend','$cause','$offense','$victim','$def_atty','$def_atty2',$disp_date,'$disp_com',$sent_date,'$sent_com','$detail',$set_date,$court,$judge,$chair1,$chair2)";
$dbmgr->execQuery($addSummary);
echo mysql_errno() . "; " . mysql_error() . "<br>\n";
}
The Optional fields are def_atty2 disp_date disp_com sent_date sent_com details
I've tried to unset($var) the values of the variable if the value is '' for each of the optional fields, however I am still getting a syntax error from MySql.
Variables passed in Function:
$defend,$cause,$offense,$victim,$def_atty,$def_atty2,$disp_date,$disp_com,$sent_date,$sent_com,$detail,$set_date,$court,$judge,$chair1,$chair2
Error:
1064; 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 ''',,'', 'Type the details here',20031201,167,2,5, 9)' a
Data elements that were passed
Marlyn Brando,02-09-13214,Murder,Toby Smith,Jackie Strong,,,,,,Type the details here,20031201,167,2,5,9
Acording to this error message the error has to do with the 7, 8, or 9th variable from the end of the argument. That means that it doesn't like the format of one of the date fields, possibley because it's a string?? I've tried the default value for the date fields in several formats each producing the same results.
How do I insert the fields that are filled out in the HTML Form and let the fields that are empty be passed as NULL to MySql?
I appreciate any advice on this subject.
Thank you,
Brian
Brian
Field Type Null Default
defend varchar(45) No
cause varchar(15) No
offense varchar(45) No
victim varchar(45) No
def_atty varchar(45) Yes NULL
def_atty2 varchar(35) Yes NULL
disp_date date Yes 0000-00-00
disp_com varchar(255) Yes NULL
sent_date date Yes NULL
sent_com varchar(255) Yes NULL
details text Yes NULL
set_date date Yes 0000-00-00
court int(11) Yes NULL
judge tinyint(4) No 0
chair1 tinyint(4) No 0
chair2 tinyint(4) No 0
My HTML form has these fields available for the user, however quite a few are optional fields and won't be filled in. I wrote a function to add the contents of my HTML form to the database.
function addSummary($defend,$cause,$offense,$victim,$def_atty,$def_atty2='',$disp_date=00000000,$disp_com='',$sent_date=00000000,$sent_com='',$detail,$set_date,$court,$judge,$chair1,$chair2) {
$dbmgr = new DBManager();
$addSummary = "Insert INTO tblsummary (defend,cause,offense,victim,def_atty,def_atty2,disp_date,disp_com,sent_date,sent_com,details,set_date,court,judge,chair1,chair2)
VALUES ('$defend','$cause','$offense','$victim','$def_atty','$def_atty2',$disp_date,'$disp_com',$sent_date,'$sent_com','$detail',$set_date,$court,$judge,$chair1,$chair2)";
$dbmgr->execQuery($addSummary);
echo mysql_errno() . "; " . mysql_error() . "<br>\n";
}
The Optional fields are def_atty2 disp_date disp_com sent_date sent_com details
I've tried to unset($var) the values of the variable if the value is '' for each of the optional fields, however I am still getting a syntax error from MySql.
Variables passed in Function:
$defend,$cause,$offense,$victim,$def_atty,$def_atty2,$disp_date,$disp_com,$sent_date,$sent_com,$detail,$set_date,$court,$judge,$chair1,$chair2
Error:
1064; 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 ''',,'', 'Type the details here',20031201,167,2,5, 9)' a
Data elements that were passed
Marlyn Brando,02-09-13214,Murder,Toby Smith,Jackie Strong,,,,,,Type the details here,20031201,167,2,5,9
Acording to this error message the error has to do with the 7, 8, or 9th variable from the end of the argument. That means that it doesn't like the format of one of the date fields, possibley because it's a string?? I've tried the default value for the date fields in several formats each producing the same results.
How do I insert the fields that are filled out in the HTML Form and let the fields that are empty be passed as NULL to MySql?
I appreciate any advice on this subject.
Thank you,
Brian
Brian