I'm writing a generic function that will take a large number of fields from $_POST and build an SQL insert into a table. In this case, I have a number of Undefined indexes and from reading other posts on SO, I am using a ternary to test if the variable exists. This works perfectly when I use it in interactive php, especially since there are no $_POST variables defined.
But when I use it in my form, I seem to get a extra quote and a few returns but I cannot see where they are coming from. I've beaten about this in different ways but am hoping someone can help me see what I'm not seeing.
function SaveDonation($form)
{ try
{
$querystr = "INSERT INTO GeneralDonations(
donationForm, firstName, startYear)" . "VALUES(" . "'" . $form
."', " .
((!isset($_POST['firstName'])) ? "'', " : ("'" . mysql_real_escape_string($_POST['firstName'])."', ")) .
((isset($_POST['startDate'])) ? ("'" . mysql_real_escape_string($_POST['startDate'])."' ") : "'' ")
.")";
echo "query = " . $querystr;
$donation = $this->db->insertRow($querystr);
$result = true;
}
catch(MysqlException $e)
{
$result = false;
$this->errorMsg = $e->getMessage();
}
return $result;
}
The startDate is the undefined index value. This is the browser output. Pls help me see what's different here?
query = INSERT INTO GeneralDonations( donationForm, firstName, startYear)VALUES('buy-a-foot', 's', '
\r\n\r\n
Aucun commentaire:
Enregistrer un commentaire