2012-12-12 05:40
浏览 111

如果在PDO预处理语句中插入成功,则为if / else语句的语法

I'm trying to switch from mySql statements to PDO prepared statements, but I'm having trouble figuring out the correct syntax for the if/else statements that I have to use if the insert was successful (which were previously if($result) {...}).

I know that $stmt->execute(); returns true on success or false on failure, but I haven't been able to determine how to set the statement up to act on that.

The new code (PDO prepared statement)

$gender = $_POST['gender'];  
if ($gender==="female" ) {
try {      
   $stmt = $conn->prepare('INSERT INTO customer_info (fname...) VALUES(:fname...)');
   $stmt->bindParam(':fname', $_POST['fname'], PDO::PARAM_STR);
    } catch(PDOException $e) {
  echo $e->getMessage();

This is the rest of the original if ($gender==="female") function

$result = @mysql_query($qry);    
    if($result) {          
      $qry="SELECT * FROM customer_info WHERE user_name='$_POST['user_name']' AND password='$_POST['password']'";
if($result) {
    if(mysql_num_rows($result) == 1) {
        //user_name Successful
        $member = mysql_fetch_assoc($result);
        $_SESSION['SESS_USER_ID'] = $member['user_id'];
        header("location: flatter_iframe.html");
    }else {        
        header("location: login_failed.html");

I've deleted most of the variables in order to simplify things (since the code is the same)

图片转代码服务由CSDN问答提供 功能建议

我正在尝试从mySql语句切换到PDO预处理语句,但我无法弄清楚是否正确 如果插入成功,我必须使用的if / else语句的语法(以前是 if($ result){...} )。

我知道$ stmt-> execute(); 成功时返回true,失败时返回false,但我无法确定如何设置语句以对其执行操作。


  $ gender = $ _POST ['gender']  ;  
if($ gender ===“female”){
try {
 $ stmt = $ conn-> prepare('INSERT INTO customer_info(fname ...)VALUES(:fname ...)'); \  n $ stmt-> bindParam(':fname',$ _POST ['fname'],PDO :: PARAM_STR); 
 $ stmt-> execute();  
} catch(PDOException $ e){
 echo $ e-> getMessage(); 

这是剩下的 原始 if($ gender ===“female”) function

  $ result = @mysql_query($ qry);  
 if($ result){
 $ qry =“SELECT * FROM customer_info WHERE user_name ='$ _ POST ['user_name']'AND password ='$ _ POST ['password']'”; 
 $ result = mysql_query  ($ QRY);  
if($ result){
 if(mysql_num_rows($ result)== 1){
 // user_name Successful 
 $ member = mysql_fetch_assoc($ result); 
 $ _SESSION ['  SESS_USER_ID'] = $ member ['user_id']; 
} else {
 header(“location:login_failed  .html“); 

我删除了大部分变量以简化事情(因为 代码是相同的)

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongpangbu4016 2012-12-12 11:39

    There are a number of ways you can check whether an INSERT worked correctly.

    1. Return value from $stmt->execute()

    As you said, $stmt->execute(); returns true on success or false on failure. So you can use:

    $result = $stmt->execute();
    if ($result) {...}

    PDO Execute documentation here

    2. Row Count

    rowCount will return the number of rows afffected by a query. After a successful insert, this should be 1.

    $affected_rows = $stmt->rowCount();
    if ($affected_rows == 1) {...} 

    PDO rowCount documentation here

    3. Last Inserted Id

    If your table has an ID column, you can return the ID of the last inserted row using lastInsertId().

    $newCustomerInfoId = $pdo->lastInsertId();
    if ($newCustomerInfoId) {...}

    Note: You must call lastInsertId on the PDO object, not the $stmt.

    PDO lastInsertId documentation here

    解决 无用
    打赏 举报

相关推荐 更多相似问题