//存储的过程名称:insertName,_id是自动递增
try {
$sql = "CALL insertName(:_id, :_name)";
$res = $pdo->prepare($sql);
$res -> bindValue(":_id","NULL");
$res -> bindValue(":_name",$name);
$res->execute();
$res->setFetchMode(PDO::FETCH_ASSOC);
} catch( PDOException $e ) {
die( "ERROR ..." .$e->getMessage());
}
//如果我尝试直接插入数字,一切正常
try {
$sql = "CALL insertName(:_id, :_name)";
$res = $pdo->prepare($sql);
$res -> bindValue(":_id",1);
$res -> bindValue(":_name",$name);
$res->execute();
$res->setFetchMode(PDO::FETCH_ASSOC);
} catch( PDOException $e ) {
die( "ERROR ..." .$e->getMessage());
}
//即使我不使用存储过程就尝试了
try {
$sql = 'INSERT INTO (id,name) VALUES (NULL,"'.$name.'" )';
$res = $pdo->prepare($sql);
$res->execute();
} catch( PDOException $e ) {
die( "ERROR ..." .$e->getMessage());
}
答案 0 :(得分:0)
仅插入名称值,因为id是自动递增的,所以它将自动获取值
答案 1 :(得分:0)
您应将变量NULL
设置为字符串,然后将其传递给bindValue
:
try {
$id = "NULL"
$sql = "CALL insertName(:_id, :_name)";
$res = $pdo->prepare($sql);
$res -> bindValue(":_id", $id);
您必须确保可以将NULL保存为该列的值!
答案 2 :(得分:0)
//I changed the stored procedure
开始交易;
DELIMITER |
创建过程insertName(_name)
BEGIN
//operations
INSERT INTO TABLE VALUES (NULL,_name);
END;
| DELIMITER;
提交;
//and then i call insertName(_name)