我与DB战斗,试图插入真值和假值到我的表列布尔,但总是得到只是错误:
无效参数号
测试:
$value = true
$value = "true"
$value = 1
谁能给我点建议?由于
编辑:完整代码如下:
// adding value to variabile
if (empty($row['vin']))
$vin = 0;
else
$vin = 1;
//calling insert method
$this->insertToTable($model_code, $typ, $kind, $ts, $vin, $smr, $ire, $manufacturer_code);
//full insert method:
public function insertToTable($code, $name, $kind, $ts, $vin, $smr, $ire, $manufacturer_code)
{
try {
$con = new PDO( DB_HOST, DB_USER, DB_PASS );
$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql = "INSERT INTO r_vehicle_model(code, name, kind, ts, vin, smr, ire, manufacturer_code) VALUES(:code, :name, :kind, :ts, :smr, :ire, :manufacturer_code)";
$stmt = $con->prepare( $sql );
$stmt->bindValue( 'code', $code, PDO::PARAM_STR );
$stmt->bindValue( 'name', $name, PDO::PARAM_STR );
$stmt->bindValue( 'kind', $kind, PDO::PARAM_STR );
$stmt->bindValue( 'ts', $ts, PDO::PARAM_STR );
$stmt->bindValue( 'vin', $vin, PDO::PARAM_STR );
$stmt->bindValue( 'smr', $smr, PDO::PARAM_STR );
$stmt->bindValue( 'ire', $ire, PDO::PARAM_STR );
$stmt->bindValue( 'manufacturer_code', $manufacturer_code, PDO::PARAM_STR );
$stmt->execute();
}
catch( PDOException $e ) {
echo $e->getMessage();
}
}
完全错误:
SQLSTATE[HY093]: Invalid parameter number::vin
在插入查询中忘记:vin
。值中参数个数不等于bindValue
$sql = "INSERT INTO r_vehicle_model(code, name, kind, ts, vin, smr, ire, manufacturer_code) VALUES(:code, :name, :kind, :ts, ,:vin ,:smr, :ire, :manufacturer_code)";
您当前使用PDO::PARAM_STR
指定您传递的所有参数都是字符串。
您应该为字段选择合适的类型,因此对于布尔值考虑使用PDO::PARAM_BOOL