现在发生了什么(只是分析):
function modifyRecord()
{
global $db;
$parameters = func_get_args();
// in this point you say that $paremeters is
// [0]=> string(62) "UPDATE applications SET service=?,acronym=?,email=? WHERE id=2"
// [1]=> string(42) ""value1","value2","value3""
// then you are doing this:
$query = array_shift($parameters);
// now with this array_shift you are giving the value
// "UPDATE applications SET service=?,acronym=?,email=? WHERE id=2"
// to the variable $query and the $parameters array has now only 1 element
// the $parameters[0] which has the value
// ""value1","value2","value3""
// NOTE THE VALUE IS IN $parameters[0] which is the only element left after the array_shift
$statement = $db->prepare($query); // here you are preparing the query, ok
try
{
$statement->execute($parameters); // here is the fault. execute expects an array with 3 elements like this:
// $parameters[0] = "value1";
// $parameters[1] = "value2";
// $parameters[2] = "value3";
// but instead execute gets this:
// $parameters[0] = ""value1","value2","value3"";
// so he says: i was expecting an array with 3 elements and all i got was an array which had only one element that had a string value.
// SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens
}
catch (PDOException $error)
{
echo $error -> getMessage();
}
}
解决方案是分解 $parameters[0] 字符串。注意:“是价值的一部分。你需要摆脱它们。所以正确的应该是:
所需的更改(解决方案):
function modifyRecord()
{
global $db;
$parameters = func_get_args();
// in this point you say that $paremeters is
// [0]=> string(62) "UPDATE applications SET service=?,acronym=?,email=? WHERE id=2"
// [1]=> string(42) ""value1","value2","value3""
// then you are doing this:
$query = array_shift($parameters);
// now with this array_shift you are giving the value
// "UPDATE applications SET service=?,acronym=?,email=? WHERE id=2"
// to the variable $query and the $parameters array has now only 1 element
// the $parameters[0] which has the value
// ""value1","value2","value3""
// NOTE THE VALUE IS IN $parameters[0] which is the only element left after the array_shift
// lets create the $parameter_array from the $parameters[0]
$parameter_array = explode(",",$parameters[0]);
// and lets remove the " from the begin and the end of every element
foreach ($parameter_array as $key => $a_parameter)
{
$parameter_array[$key] = trim($a_parameter, '"');
}
$statement = $db->prepare($query); // here you are preparing the query, ok
try
{
$statement->execute($parameter_array); // NOW its correct. we give execute what it wants: an array with 3 elements like this:
// $parameter_array[0] = "value1";
// $parameter_array[1] = "value2";
// $parameter_array[2] = "value3";
}
catch (PDOException $error)
{
echo $error -> getMessage();
}
}
在这里工作http://3v4l.org/kQsAV