【问题标题】:PDO return last ID in a classPDO 返回类中的最后一个 ID
【发布时间】:2018-07-11 20:05:23
【问题描述】:

我对如何使用 PDO 使用返回最后一个 ID 感到有些困惑。我找到的所有教程/答案都是基于它不在课堂上所以很难找到答案。

我得到的代码如下。除了返回最后一个 id 之外,这一切都有效。当返回 true 时,它​​会插入数据库并重定向到另一个页面。我是否正确使用退货 ID?如果是这样,我如何在用户被重定向到的页面上回显 ID?

编辑:感谢您的回答-我已更新代码以将“true”替换为 return last id 部分。但是,我仍然在努力返回 ID,在下面添加了新代码。数据库插入工作正常,我只是在努力输出返回的内容。

谢谢,

数据库查询:

public function insertNewCustomer($data){
    $this->db->query("INSERT INTO Customers (First_Name, Surname, Email) VALUES (:firstname, :surname, :email)");
    //Bind Data
    $this->db->bind(':firstname', $data['firstname']);
    $this->db->bind(':surname', $data['surname']);
    $this->db->bind(':email', $data['email']);


    //Execute
    if($this->db->execute()){
        return true;
    } else {
        return false;
    }

    //Get ID of new customer
    $lastID = $this->db->lastInsertId();



}

我如何尝试回显 ID:

$itinerary = new Itinerary;

$template->ID = $itinerary->insertNewCustomer();

echo $ID->lastID;

尝试回显 ID 的新代码:

if(isset($_GET['firstname'])){

$data = array();
$data['firstname'] = $_GET['firstname']; 
$data['surname'] = $_GET['surname']; 
$data['email'] = $_GET['email']; 

if($itinerary->insertNewCustomer($data)){

echo $itinerary->insertNewCustomer();


} else { echo 'did not work';}

【问题讨论】:

  • $lastID = $this->db->lastInsertId(); 是正确的,但您的代码永远不会到达它,因为您在 $this->db->execute() 之后返回
  • 这个echo $ID->lastID 没有任何意义
  • 没有执行返回后写入的所有内容,因此您的代码永远不会传递给 $lastID = $this->db->lastInsertId();

标签: php class object pdo


【解决方案1】:
<?php
// Use this function

    public function insertNewCustomer($data){
        $this->db->query("INSERT INTO Customers (First_Name, Surname, Email) VALUES (:firstname, :surname, :email)");
        //Bind Data
        $this->db->bind(':firstname', $data['firstname']);
        $this->db->bind(':surname', $data['surname']);
        $this->db->bind(':email', $data['email']);


        //Execute
        if($this->db->execute()){
        //Get ID of new customer
            return $this->db->lastInsertId();
        } 
        return false;
    }

// 超出函数范围 $itinerary = 新行程;

$template->ID = $itinerary->insertNewCustomer(); // Call to function

if($template->ID!==false){echo "Customer ID". $template->ID;}
else{
echo "Customer insertion failed";
}

【讨论】:

  • $ID-&gt;lastID 对我来说毫无意义。
  • @DanielO。固定的。谢谢
  • 这个解决方案没有添加任何我没有添加的内容。 is_numeric 甚至不是必需的,因为 $itinerary-&gt;insertNewCustomer(); 只会产生 false 或整数。
  • 最好通过 PDO 构造函数选项启用 PDO Exceptions 来处理数据库相关的错误。
  • @DanielO.我同意你可以在插入函数内部处理 PDO 异常。
【解决方案2】:

问题是因为您在运行db-&gt;lastInsertId();之前返回

改变这个:

//Execute
if($this->db->execute()){
    return true;
} else {
    return false;
}   

//Get ID of new customer
$lastID = $this->db->lastInsertId();

收件人:

//Execute
if($this->db->execute()){
    return $this->db->lastInsertId();
} else {
    return false;
}

$itinerary-&gt;insertNewCustomer(); 现在将在执行失败或插入 ID 时返回 false。

【讨论】:

  • @4each 您到底想通过这些编辑实现什么目标?您建议的最后一次编辑仅添加了一个空格字符。请在我要求模组介入之前停下来。
猜你喜欢
  • 2013-09-13
  • 1970-01-01
  • 1970-01-01
  • 2020-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-31
  • 2014-06-19
相关资源
最近更新 更多