Hi guys,
I need your help, have gone thru my syntax several times, can't seem to find why I am having this error.
The function is meant to take an array of inputs from an HTML form, format it using the 'sprintf' and then insert it into the database.
function insertRecord ($fieldarray)
{
$this->errors = array();
global $dbconnection, $query;
$dbconnection = db_connect($this->dbname) or trigger_error("SQL", E_USER_ERROR);
$query_str = "\"INSERT INTO $this->tablename SET ";
foreach ($fieldarray as $item => $value) {
$query_str .= $value."='%s', ";
} // foreach
$query_str = rtrim($query_str, ', ');
$query_str .= "\"";
$escape_str = NULL;
foreach ($fieldarray as $item => $value) {
$escape_str .= "mysql_real_escape_string('".$_REQUEST[$value]."'), ";
} // foreach
$escape_str = rtrim($escape_str, ', ');
// $query = sprintf($query_str, $escape_str);
$result = mysql_query(sprintf($query_str, $escape_str), $dbconnection);
if (mysql_errno() <> 0) {
if (mysql_errno() == 1062) {
$this->errors[] = "A record already exists with this ID.";
} else {
trigger_error("SQL", E_USER_ERROR);
} // if
} // if
return;
} // insertRecord
During my diagnosis I echoed $query_str and $escape_str just to make sure they contained what I wanted and they did, here is the output;
For $query_str:
"INSERT INTO registration_table SET ConfirmationID='%s', EventID='%s', ParticipantFirstName='%s', ParticipantLastName='%s', ParticipantAddress='%s', ParticipantCity='%s', ParticipantState='%s', ParticipantCountryID='%s', ParticipantEmail='%s', ParticipantPhoneNumber='%s', CompanyName='%s', JobTitle='%s', CompanyAddress='%s', CompanyCity='%s', CompanyState='%s', CompanyCountryID='%s', RegistrationDate='%s', EventDateID='%s'"
For $escape_str:
mysql_real_escape_string('8433544222JJJ'), mysql_real_escape_string('1'), mysql_real_escape_string('jkhfuhiuh'), mysql_real_escape_string('hjgjhkjhl'), mysql_real_escape_string('b,bjj,jjkhkj'), mysql_real_escape_string('b,kjkjhjkh'), mysql_real_escape_string('jhb,kjhkj'), mysql_real_escape_string('4'), mysql_real_escape_string('ag-silver@live.com'), mysql_real_escape_string('5748673687'), mysql_real_escape_string('jgkgkiulh'), mysql_real_escape_string('uiyiuhky'), mysql_real_escape_string('uiyuhki'), mysql_real_escape_string('kukluhll'), mysql_real_escape_string('uguilhli'), mysql_real_escape_string('3'), mysql_real_escape_string('2012-04-04'), mysql_real_escape_string('1')
I don't know what I am doing wrong, all i know is that the 'sprintf' isn't outputing anything. Can any one help?