无法运行查询:SQLSTATE [HY093]:参数号无效:未定义参数

时间:2013-10-17 15:01:41

标签: php mysql sql

我是php的新手,并试图摆脱这个错误一段时间了。我相信我有正确数量的参数,但无法找出问题所在。这是我下面的PHP代码..任何答案都非常感谢。感谢

require ("common.php");
$mySessionName = $_SESSION['user']['UserName'];
if(!empty($_POST)){
$sql = "INSERT INTO compliance_requirement(ComplianceName, ComplianceGoal,    ComplianceDescription, ComplianceStartDate, ComplianceEndDate, UserName)''VALUES (:compName, :compGoal, :compDes, :compStart, :compEnd, :$mySessionName)";

$query_paramsm = array(':ComplianceName' => $_POST['compName'], ':ComplianceGoal'=>$_POST['compGoal'], 'ComplianceDescription' => $_POST['compDes'], 'ComplianceStartDate'=>$_POST['compStart'], 'ComplianceEndDate'=>$_POST['compEnd'], 'UserName'=>$_POST['UserName']);

try{ 
$stmt = $db->prepare($sql);
$result = $stmt->execute($query_paramsm); 
} 
catch(PDOException $ex)
{ 

die("Failed to run query: " . $ex->getMessage()); 
} 
header("Location: compliance.php");
}
?>

2 个答案:

答案 0 :(得分:0)

很多问题。 1.您正在查询中间 2.

$sql = "VALUES (:compName, ...";
$query_paramsm = array(':ComplianceName' => $_POST['compName'] ...);

compName和ComplianceName是两个非常不同的字符串。

答案 1 :(得分:0)

您在查询:compName, :compGoal, :compDes, :compStart, :compEnd, :$mySessionName

中拥有的变量

与param数组中的名称不同:

array(':ComplianceName' => $_POST['compName'], ':ComplianceGoal'=>$_POST['compGoal'], 'ComplianceDescription' => $_POST['compDes'], 'ComplianceStartDate'=>$_POST['compStart'], 'ComplianceEndDate'=>$_POST['compEnd'], 'UserName'=>$_POST['UserName'])

看:

:compName <> :ComplianceName
:compGoal <> :ComplianceGoal
...