sprintf error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
cto1mac
Forum Commoner
Posts: 54
Joined: Tue Jan 27, 2004 6:11 am
Location: Virginia Beach, VA

sprintf error

Post by cto1mac »

I am getting a sprintf error,

Code: Select all

Warning: sprintf(): Too few arguments in /var/www/localhost/htdocs/new_account.php on line 31

here is the code:

Code: Select all

<?php

    $form_values = array($_POST[uname], $_POST[pword], $_POST[fname] , $_POST[lname], $_POST[address], $_POST[city] , $_POST[state], $_POST[zip], $_POST[email]); 	


        function quote_smart($value) {

		      if (get_magic_quotes_gpc()) {

			     $value = stripslashes($value);

		      }

		      if (!is_numeric($value)) {

			     $value = "'" . mysql_real_escape_string($value) . "'";

		      }

		   return $value;
        }

        
        $size = count($form_values);
        
        for ($i = 1; $i <= $size; $i++) {
            $format .= "%s,";
        }
        $alt_size = count($form_values) - 1;
        for ($j = 0; $j <= $alt_size; $j++) {
        
            $format1 .= quote_smart($form_values[$j]); 
            if ($j <> $alt_size) {
                $format1 .= ", ";
            }
        
        }

       $sql = sprintf("INSERT INTO accounts VALUES ('', $format now(), now())", $format1);
        

print $sql;

?>
I had the system output the values for $format, and $format1 and they appear correct, the number of %s and values is correct, but still I get the error. Any help would be greatly appreciated.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Print out the variables before you place them in sprintf and see what they display. One will have more items than the other.

It seems a bit long winded to use sprintf here when you could just use $format1 or implode(",", $form_values) to list the data in the string.
Post Reply