yii insert语句错误SQLSTATE [HY093]:参数号无效:未定义参数

时间:2015-07-01 07:46:33

标签: php mysql yii

在Yii中将数据插入表格时,我收到此错误:

CDbCommand failed to execute the SQL statement:
SQLSTATE[HY093]: Invalid parameter number: parameter was not defined.
The SQL statement executed was: insert into user_feedback (name,
desc,email,mobilenumber) values (:Name, :Desc,:email,:Mobilenmber).

下面是我的代码:

$sql = "insert into user_feedback (name, desc,email,mobilenumber) values (:Name, :Desc,:email,:Mobilenmber)";
      $parameters = array(":name"=>$_POST['Name'], ':desc' => $_POST['Desc'],':email'=>$_POST['email'],':mobile_number'=>$_POST['Mobile_nmber']);
      Yii::app()->db->createCommand($sql)->execute($parameters);

2 个答案:

答案 0 :(得分:1)

您的请求有2个错误。正如亚历山大所说,你的参数不匹配(所有人都希望发送邮件)。还有错字错误。

此请求/参数应该有效:

$sql = "insert into user_feedback (name, desc,email,mobilenumber) values (:name, :desc,:email,:mobile_number)";
      $parameters = array(":name"=>$_POST['Name'], ':desc' => $_POST['Desc'],':email'=>$_POST['email'],':mobile_number'=>$_POST['Mobile_number']);

答案 1 :(得分:1)

修复参数名称以匹配select语句中的参数名称,尝试使用queryAll执行此操作,如下所示:

laptop table

修改

我打赌这个问题确实在$sql = "insert into user_feedback (name, desc,email,mobilenumber) values (:Name, :Desc,:Email,:Mobilenmber)"; $parameters = array( ":Name"=>$_POST['Name'], // names match paremeters in select ':Desc' => $_POST['Desc'], // names match paremeters in select ':Email'=>$_POST['email'], // names match paremeters in select ':Mobilenmber'=>$_POST['Mobile_number']); // names match paremeters // in select plus there is a problem // with with key (typo) Mobile_number and not Mobile_nmber (unless you defined it this way on purpose !!!) $command = Yii::app()->db->createCommand($sql); $result = $command->queryAll(true,$parameters); 列,它是一个MYSQL关键字name,请将列名更改为其他内容然后尝试一下