dongque3797 2012-12-12 05:40
浏览 117
已采纳

如果在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);
   $stmt->execute();   
    } 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']'";
      $result=mysql_query($qry);          
if($result) {
    if(mysql_num_rows($result) == 1) {
        //user_name Successful
        session_regenerate_id();
        $member = mysql_fetch_assoc($result);
        $_SESSION['SESS_USER_ID'] = $member['user_id'];
        session_write_close();
        header("location: flatter_iframe.html");
        exit();
    }else {        
        header("location: login_failed.html");
        exit();
    }   

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

  • 写回答

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.

    $stmt->execute();
    $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().

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

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

    PDO lastInsertId documentation here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?