Forum Moderators: coopster

Message Too Old, No Replies

Dynamic sprintf

         

DailyAmerican

10:31 pm on Feb 12, 2010 (gmt 0)

10+ Year Member



I'm trying to generate a dynamic sprintf statement. If I echo the columns, items and entries out it displays correctly. Yet when I insert it into the sprintf to do a table insert I can't get it to work. Could someone let me know what I'm doing wrong or an alternative way to do this? I'm parsing out a Post array in order to pull the columns to insert into along with the items to insert. Here's how the sprintf looks when I echo out all the other parts.

sprintf('INSERT INTO `users` (`firstName`, `email`, `password`, `zipCode`, `gender`, `maritalStatus`, `children`, `annualIncome`, `emailOffers`, `categories`, `dateOfBirth`, `authorization`) VALUES ("%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s")', mysql_real_escape_string("username"), mysql_real_escape_string("dummy@example.com"), mysql_real_escape_string("password"), mysql_real_escape_string("11111"), mysql_real_escape_string("M"), mysql_real_escape_string(""), mysql_real_escape_string("0"), mysql_real_escape_string(""), mysql_real_escape_string("1"), mysql_real_escape_string("1,3,10,17"), mysql_real_escape_string("1989-03-4"), mysql_real_escape_string("NALXq5aRm7J7"));



This is the error I get:
Warning: sprintf() [function.sprintf]: Too few arguments in /Applications/xampp/xamppfiles/htdocs/slh/core/functions.php on line 918

Query was empty



Here's my code:

foreach($array as $key => $val){
if(is_array($val)){
foreach($val as $key2 => $val2){
$val2 = cleanData($val2);
$array[$key].$val[$key2] = $val2;
}
} else {
$val = cleanData($val);
$array[$key] = $val;
}
}
$dob = $array['date_of_birth_year'].'-'.$array['date_of_birth_month'].'-'.$array['date_of_birth_day'];
foreach ($array as $key => $val){
if($key != 'date_of_birth_year' && $key != 'date_of_birth_month' && $key != 'date_of_birth_day' && $key != 'recaptcha_challenge_field' && $key != 'recaptcha_response_field'){
$col .= '`';
$col .= $key;
$col .= '`, ';
}
}
$col .= '`dateOfBirth`, `authorization`';
foreach ($array as $key => $val){
if($key != 'date_of_birth_year' && $key != 'date_of_birth_month' && $key != 'date_of_birth_day' && $key != 'recaptcha_challenge_field' && $key != 'recaptcha_response_field'){
$items .= '"';
$items .= '%s';
$items .= '",';
}
}
$items .= '"%s","%s"';
foreach ($array as $key => $val){
if(is_array($val)){
$entries .= 'mysql_real_escape_string("';
foreach($val as $key2 => $val2){
$entries .= $val2;
$entries .= ',';
}
$entries = rtrim($entries, ',');
$entries .= '"';
} elseif($key != 'date_of_birth_year' && $key != 'date_of_birth_month' && $key != 'date_of_birth_day' && $key != 'recaptcha_challenge_field' && $key != 'recaptcha_response_field'){
$entries .= 'mysql_real_escape_string("';
$entries .= $array[$key];
$entries .= '"), ';
}
}
$code = activationCode(12);
$entries .= '), mysql_real_escape_string("'.$dob.'"), mysql_real_escape_string("'.$code.'")';
}
$query = "INSERT INTO `users` (".$col.") VALUES (".$items.")";
$sql = sprintf($query, $entries);
$inserted = mysql_query($sql) or die(print($sql).' <br /> '. mysql_error());

Readie

1:01 am on Feb 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Could you highlight which line is 918 please?

eelixduppy

1:09 am on Feb 13, 2010 (gmt 0)




$query = "INSERT INTO `users` (".$col.") VALUES (".$items.")";
$sql = sprintf($query, $entries);



The problem here is in the query string there is no formatting being done, which means it cannot add $entries to the string. The $query should contain something like
%s
...

DailyAmerican

1:37 pm on Feb 15, 2010 (gmt 0)

10+ Year Member



Line 918 is the entire sprintf statement once compiled.
$sql = sprintf($query, $entries);


eelixduppy - so, I can't use $items in order to insert the %s into the values option? If I can't is there anything in php that will allow me to do this dynamically. I just want it so that when the form is submitted that the post variable are automatically parsed and inserted into the db without me having to adjust my php.

Readie

5:44 pm on Feb 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The reason you're getting your error is you're not applying a format within your sprintf() function. See [uk.php.net...]

DailyAmerican

6:18 pm on Feb 15, 2010 (gmt 0)

10+ Year Member



Wouldn't this
INSERT INTO `users` (`firstName`, `email`, `password`, `zipCode`, `gender`, `maritalStatus`, `children`, `annualIncome`, `emailOffers`, `categories`, `dateOfBirth`, `authorization`) VALUES ("%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s")
count as the formatting? Is it the fact that I'm trying to pass a variable into the sprintf statement? I even tried making a string of the sprintf statement. So, the the statement is fully compiled as a string. Then I tried to pass it to eval in order to make it register as a sprintf function, but that didn't work either. I ended up with the query is empty again.