2

This line is giving error: "Too few arguments". What is the problem?

$InsertQuery = sprintf("INSERT INTO listing (ldate, places, company, designation, projectdetails, desiredcandidate, hrname, hrcontact, email) VALUES (DATE_FORMAT(%s,'%Y %m %d),%s,%s,%s,%s,%s,%s,%s,%s)", $ldate,$places,$company,$designation, htmlentities($projectdetails), htmlentities($desiredcandidate),$hrname,$hrcontact,$email);
RKh
  • 13,818
  • 46
  • 152
  • 265

2 Answers2

6

Well, your string specifies 12 placeholders and you only provide 9 values. The sprintf function requires that you pass as many values as the number of placeholders you specify in the format string. I actually think the error message is strikingly clear and is about as good an error message as you will ever see.

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • Exactly the error is too clear. I am passing 9 arguments and their are 9 %s. Can you please clear where are 12 place holders? – RKh Oct 25 '09 at 06:22
  • 2
    You are forgetting about the `%Y %m %d` - those are placeholders as well. – Andrew Hare Oct 25 '09 at 06:23
  • Well, at least `%d` is - I am not sure whether or not PHP considers `%Y` and `%m` valid type specifiers. – Andrew Hare Oct 25 '09 at 06:25
  • But those are part of MySQL Data_Format function. I forgot to end with '. That makes it '%Y %m %d', but that is not solving my problem. Is it that sprintf considers everything that starts with % as a placeholder? – RKh Oct 25 '09 at 06:25
  • Not everything - only strings that are valid type specifiers. I believe that you should be okay with `%Y` and `%m` since they don't appear to be type specifiers (see http://php.net/manual/en/function.sprintf.php) but `%d` _is_ a type specifier so you will need to double up the percent symbol (like this: `%%d`) if you want `sprintf` to output a literal `%d`. – Andrew Hare Oct 25 '09 at 06:28
  • 3
    Escaping any `%` you want to output literally is a good idea, whether what follows is a legal type specifier or not. It may become one in the future. – Matthew Scharley Oct 25 '09 at 06:57
5

The arguments are the expressions, and they should match in number the % format specifiers. If you really just need a % char, use %%.

DigitalRoss
  • 143,651
  • 25
  • 248
  • 329