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.