I'm a relative newcomer to php and am working on a script that will let me enter information into a database. I was debating a couple ways to create the script, and someone at work suggest the sprintf function with a bit of guidance, but he's now disappeared and the error I'm getting is driving me mad because I can't get enough information on exactly how to use sprintf to be sure what I'm doing wrong!

The error message I get is

Warning: sprintf(): Too few arguments in /home/moswell/public_html/add_parts3.php on line 89

Your submission could not be processed due to a system error.

Now, I'm running the print_r function so I can see that the data from the html form is getting submitted, but I can't find my error. Here's the code:

<?php
// Start creating the array for adding parts.

$pn = array($_POST['Part_Name']);
$clef = array($_POST['Clef']);
$ts = array($_POST['Time_Sig']);
$ks = array($_POST['Key_Sig']);
$notes = array($_POST['Other_Notes']);
if (isset($pn[0])) {
	for ($pn = 1; $pn <=2; $pn++) {

// Validate the form.
// Check for a song title.
if (!empty($_POST['Song_Title'])) {
	$st = escape_data($_POST['Song_Title']);
} else {
	$st = FALSE;
	echo '<p><font color="red">Please enter the song title!</font></p>';
}

//Check for a part name.
if (!empty($_POST['Part_Name'])) {
	$pn = escape_data($_POST['Part_Name']);
} else {
	$pn = FALSE;
	echo '<p><font color="red">Please enter the part name!</font></p>';
}

// Check for a clef (not required).
if (!empty($_POST['Clef'])) {
	$clef = escape_data($_POST['Clef']);
} else {
	$clef = NULL;
}

// Check for a time signature (not required).
if (!empty($_POST['Time_Sig'])) {
	$ts = escape_data($_POST['Time_Sig']);
} else {
	$ts = NULL;
}

// Check for a key signature (not required).
if (!empty($_POST['Key_Sig'])) {
	$ks = escape_data($_POST['Key_Sig']);
} else {
	$ks = NULL;
}

// Check for other notes (not required).
if (!empty($_POST['Other_Notes'])) {
	$notes = escape_data($_POST['Other_Notes']);
} else {
	$notes = NULL;
}
if ($st && $pn) {//If the two required fields are okay.
// Run the query.
print_r($_POST);
$partquery=sprintf("INSERT INTO Part (Song_Title, Part_Name, Clef, Time_Sig, Key_Sig, Other_Notes) VALUES (%s, %s, %s, %s, %s, %s)");
if ($partresult = mysql_query ($partquery))  {
 // Worked.
		echo '<p>The part(s) have been added.  You may now add another part or click on the book display to choose a song from another book.</p>';

	} else { // If the query did not run OK.
		echo '<p><font color="red">Your submission could not be processed due to a system error.</font></p>'; 
	}
}

}

} else { // Display the form.
?>
<form action="add_parts3.php" method="post">

<fieldset><legend>Enter your information in the form below:</legend>
<form>
<p><b>Song Title:</b>
<select name="Song_Title"><option>Select One</option>
<?php // Retrieve song titles from book selected.
//Run the query.
$songquery="SELECT Song_Title, Source_Siglia FROM Song WHERE Source_Siglia='$_GET[Source_Siglia]'";
$songresult=mysql_query($songquery) or die("Invalid query: " . mysql_error());
while ($row = mysql_fetch_array ($songresult)) {
		echo "<option value=\"{$row['Song_Title']}\">{$row['Song_Title']}</option>\n";
		}
?>
	</select></p>
	<p><b>Part Name:</b>
	<input type="text" name="Part_Name[]" size="10" /></p>
	<p><b>Clef:</b>
	<input type="text" name="Clef[]" size="15" /></p>
	<p><b>Time Signature:</b>
	<input type="text" name="Time_Sig[]" size="20" /></p>
	<p><b>Key Signature:</b>
	<input type="text" name="Key_Sig[]" size="15" /></p>
	<p><b>Other Notes:</b>
	<input type="text" name="Other_Notes[]" size="50" maxlength="255" /></p>
	<p><b>Part Two Name:</b>
	<input type="text" name="Part_Name[]" size="10" /></p>
	<p><b>Clef:</b>
	<input type="text" name="Clef[]" size="15" /></p>
	<p><b>Time Signature:</b>
	<input type="text" name="Time_Sig[]" size="20" /></p>
	<p><b>Key Signature:</b>
	<input type="text" name="Key_Sig[]" size="15" /></p>
	<p><b>Other Notes:</b>
	<input type="text" name="Other_Notes[]" size="50" maxlength="255" /></p>
	<input type="submit" name="submit" value="Submit" />
	<input type="hidden" name="submitted" value="TRUE" />
	</form>
	</fieldset>
<?php
include ('./includes/footer.html');
} // End of main conditional.
?>
</body>
</html>

Can someone see what might be the problem, or failing that, provide a link or some guidance on using this function? Thanks a lot.

    You are not passing any values to sprintf

      But I seem to be passing values, the print_r function returns the following:

      Array ( [Song_Title] => Al ye whom love or fortune hath betraid [Part_Name] => Array ( [0] => test [1] => ) [Clef] => Array ( [0] => g4 [1] => ) [Time_Sig] => Array ( [0] => cut [1] => ) [Key_Sig] => Array ( [0] => b [1] => ) [Other_Notes] => Array ( [0] => [1] => ) [submit] => Submit [submitted] => TRUE )

      Doesn't that mean that something is getting to the sprintf?

        Fore each "%s" place-holder in your sprintf() format string, you need an additional argument for the value that is to replace that place-holder:

        $partquery=sprintf("INSERT INTO Part (Song_Title, Part_Name, Clef, Time_Sig, Key_Sig, Other_Notes) VALUES (%s, %s, %s, %s, %s, %s)", $st, $pn, $clef, $ts, $ks, $notes);
        

          If it weren't for the fact that it's so satisfying when something finally works, I'd have given up ages ago! Now when I print the sql, I get

          INSERT INTO Part (Song_Title, Part_Name, Clef, Time_Sig, Key_Sig, Other_Notes) VALUES (Al ye whom love or fortune hath betraid, Array, Array, Array, Array, Array)

          And it seems to be looping indefinitely.

            I didn't see before that you're using array names for those form elements. Also, you'll need to quote the values, so see if this comes any closer to working:

             $partquery=sprintf("INSERT INTO Part (Song_Title, Part_Name, Clef, Time_Sig, Key_Sig, Other_Notes) VALUES ('%s', '%s', '%s', '%s', '%s', '%s')", $st, $pn[0], $clef[0], $ts[0], $ks[0], $notes[0]);
            

              No, alas, now instead of Array for a value, it's giving me "A, A, A" etc. etc. and still looping. I think maybe I need to scrap the whole idea and go with something simpler. I suspect it's something towards the top of the code, something to do with the initial couple parts (like the for statement isn't written correctly or I need to fix something with how I'm creating the array.

                Write a Reply...