如何将主键的值插入到插入查询中

时间:2019-11-16 19:25:57

标签: php mysql

如何检索customer_information表中主键的值,以便可以将值输入INSERT语句以将数据存储到我的customer_payment_information表中?

我想调用主键的值并将其存储在变量中,这样我就可以将变量输入到INSERT语句中,以存储我的customer_payment_information表的数据。

不幸的是,我的数据库中没有任何存储。

这是我的代码:

$sql = "SELECT customer_id FROM customer_information ORDER BY customer_id DESC LIMIT 1";
if ($conn->query($sql)){
    $compile = $conn->prepare("INSERT INTO customer_payment_information (customer_id, fullName, creditcardNumber, expiry, ccv) VALUES (?, ?, ?, ?, ?)");            
    $compile->bind_param("issss", $sql, $ccname, $ccnumber, $expdate, $ccvnumber);
    $compile->execute();
    $compile->close();
    $conn->close();
} else {
    $errorMsg = "Connection failed: " . $conn->connect_error;
}

表1的主键具有auto_increment,因此是SQL查询

“从客户信息中选择客户ID,顺序为客户ID DESC LIMIT 1”

表1: TABLE 1 FORMAT

表2:TABLE 2 FORMAT

1 个答案:

答案 0 :(得分:0)

您需要执行您的inital select语句并获得结果。像这样的事情应该为你做。

class MyLayer: CALayer {
    override func draw(in ctx: CGContext) {
        let nsgc = NSGraphicsContext(cgContext: ctx, flipped: false)
        NSGraphicsContext.current = nsgc

        let font = NSFont.systemFont(ofSize: 18)
        let text = "TEXT"
        let textRect = CGRect(x: 100, y: 100, width: 100, height: 100)
        text.draw(in: textRect, withAttributes: [.font: font])
    }
}

您可能希望在插入记录后考虑使用$sql = "SELECT customer_id FROM customer_information ORDER BY customer_id DESC LIMIT 1"; $customerQuery = $conn->query($sql); $customerResult = $customerQuery->fetch_assoc(); $customerId = $customerResult['customer_id']; $compile = $conn->prepare("INSERT INTO customer_payment_information (customer_id, fullName, creditcardNumber, expiry, ccv) VALUES (?, ?, ?, ?, ?)"); $compile->bind_param("issss", $customerId, $ccname, $ccnumber, $expdate, $ccvnumber); $compile->execute(); $compile->close(); $conn->close(); 之类的东西来获取其ID以备将来使用。