PHP - getting the latest PK ID statements for use in the second function. Insert expression

Duplicate editing . This question is different from the fact that I'm trying to return a specific value, the identifier of the primary key, which will be used in other functions INSERTas a foreign key, within the same action. The "repeating" question does not answer this, but only shows how to get the return value from the function. Not how to receive it and correctly insert into another report on the preparation of functions. Which I was not sure what I was doing right.

I have two tables, ordersand customerswhich should have data inserted from the same form within the same action. The table ordershas a primary key = orderID, which also needs to be added to the table customers.

DB Relationship Diagram Relationship diagram

I can load the data into the table orderswithout problems, but the second query function in the Customers table does nothing. I am sure that this is due to the foreign key restrictions that I have set. I did some research and realized that I need to get the foreign key identifier, possibly using $mysqli->insert_idor PDO::lastInsertId, but I'm lost about how to use it with my current functions.

index.php

$product = $_POST['product'];
$fName = $_POST['fName'];
$lName = $_POST['lName'];
$address = $_POST['address'];
$address2 = $_POST['address2'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$country = $_POST['country'];
$phoneNumber = $_POST['phoneNumber'];
$email = $_POST['email'];
/* Functions */
order_Data ($db, $product, $fName, $lName, $email);
// Need to have orderID  from ^ table used in order_custData sql statement
order_custData($db, $orderID, $fName, $lName, $address, $address2, $city, $state, $zip, $country, $phoneNumber, $email);

functions.php

<?php 

/**  Order Data Function
 */
function order_Data($db, $product, $fName, $lName, $email) {
    try {
        $sql = "INSERT INTO orders SET product = :product, fName = :fName, lName = :lName, email = :email";
        $ps = $db->prepare($sql);
        $ps->bindValue(':product', $product);
        $ps->bindValue(':fName', $fName);
        $ps->bindValue(':lName', $lName);
        $ps->bindValue(':email', $email);
        $ps->execute();
        return $orderID = $db->lastInsertId();
    } catch (PDOException $e) {
        die("Sorry, There was a problem order table.");
    }
}

/**  Customer Purchase Information Function
 * @param $orderID -- I need to insert the orderID from orders table?
 */
function order_custData($db, $orderID, $fName, $lName, $address, $address2, $city, $state, $zip, $country, $phoneNumber, $email) {
    try {
        $sql = "INSERT INTO customers SET  orderID = :orderID, fName = :fName, lName = :lName, address = :address, address2 = :address2,city = :city, state = :state, zip = :zip, country = :country, phoneNumber = :phoneNumber, email = :email";
        $ps = $db->prepare($sql);
        $ps->bindValue(':orderID', $orderID); // Foreign key from orders table
        $ps->bindValue(':fName', $fName);
        $ps->bindValue(':lName', $lName);
        $ps->bindValue(':address', $address);
        $ps->bindValue(':address2', $address2);
        $ps->bindValue(':city', $city);
        $ps->bindValue(':state', $state);
        $ps->bindValue(':zip', $zip);
        $ps->bindValue(':country', $country);
        $ps->bindValue(':phoneNumber', $phoneNumber);
        $ps->bindValue(':email', $email);
        return $ps->execute();
    } catch (PDOException $e) {
        die("Sorry, There was a problem with the customer table!");
    }
}
?>

$orderID :orderID IN THE order_custData , . sql. , , , underfined variable .

.

+4
3

order_Data() :

return $orderID = $db->lastInsertId();

( return $db->lastInsertId();.)

, :

$orderID = order_Data ($db, $product, $fName, $lName, $email);
order_custData($db, $orderID, $fN …);
+3

. ,

<?php
require_once ("models/dbConn.php");
require_once ("models/functions.php");
$action = $_REQUEST['action'];
if ($action == NULL || empty($action)):
    $action = '';
endif;
include_once ("views/header.php");
switch ($action) :
    case '':
        include ("views/main.php");
        break;
    case 'checkoutCart':
        // Once form button is clicked action = completePurchase
        include ("views/checkout.php");
        break;
    case 'completePurchase':
        $product = $_POST['product'];
        $fName = $_POST['fName'];
        $lName = $_POST['lName'];
        $address = $_POST['address'];
        $address2 = $_POST['address2'];
        $city = $_POST['city'];
        $state = $_POST['state'];
        $zip = $_POST['zip'];
        $country = $_POST['country'];
        $phoneNumber = $_POST['phoneNumber'];
        $email = $_POST['email'];
        /* Functions */

$orderID = order_Data ($ db, $product, $fName, $lName, $email);

        // Need to have orderID  from ^ table used in order_custData sql statement
        order_custData($db, $orderID, $fName, $lName, $address, $address2, $city, $state, $zip, $country, $phoneNumber, $email);
        include ("views/complete.php");
        break;
endswitch;
include_once ("views/footer.php");
?>
+1

order_Data() , index.php $objectID.

.. ...

order_Data ($db, $product, $fName, $lName, $email);

... ...

$orderID = order_Data ($db, $product, $fName, $lName, $email);

I recommend checking the error reporting level for PHP in your development environment to enable notifications, since PHP will trigger a notification about the use of the undefined variable.

+1
source

Source: https://habr.com/ru/post/1653736/


All Articles