PHP - getting last statements PK ID to use in second function Insert statement

Gorgon_Union

Duplicate Edit: This question is different because I'm trying to return a specific value, the primary key ID to be used in another functions INSERT statement as a foreign key, within the same action. The "duplicate" question does not answer this, rather it only shows how to get a return value from a function. Not how to get it and insert it correctly in another functions prepare statement. Which I wasn't sure I was doing correctly.

I have two tables, orders and customers that are to have data inserted from one form within the same action. The orders table has a primary key = orderID which also needs to be added into the customers table.

DB Relationship Diagram Relationship Diagram

I can upload the data to the orders table without a problem, but the second query function to the customers table does nothing. I'm pretty sure it's due to the foreign key constraint I've set. I've done some research and realize I need to get the foreign key id, possibly using $mysqli->insert_id or PDO::lastInsertId, but I'm lost as to 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!");
    }
}
?>

Now the $orderID and :orderID IN THE order_custData function are just there for me as a visual representation to figure out the problem. I wasn't try to execute the sql statement with it originally. However, anything I've tried seems to throw errors underfined variable or fatal calls to the prepare function of the first function.

Thank you for your time.

jensgram

The utility function order_Data() already returns the ID:

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

(Rewrite to return $db->lastInsertId();, though.)

Simply carry the return value by assigning to a variable:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Getting ID of last insert

From Dev

Use function in insert Statement Oracle

From Dev

Codeigniter insert_id() function retrieve correctly the last insert id?

From Dev

Phalcon last insert id in aftercreate function

From Dev

Phalcon last insert id in aftercreate function

From Dev

use of where cluase for last_insert_id

From Dev

PHP : return last inserted Id on statement

From Dev

Return the id of the last MySQL insert in PHP

From Dev

Can't return the last known ID after an INSERT statement

From Dev

PHP function only getting last value of array

From Dev

How to Get last Insert Id in php without insert any data?

From Dev

Use ID inserted with `INSERT OR IGNORE` in next `INSERT` statement

From Dev

php foreach statement to insert data into sql only inserting last line

From Dev

Insert element as second to last in JQuery

From Dev

Cant use id in function after insert returning into

From Dev

Getting the second to last working day

From Dev

Getting the second to last working day

From Dev

MySQL Function with LAST_INSERT_ID() giving unexpected results

From Dev

Mysqli multiple prepared statements with temp table and stored function. Last prepared statement not binding

From Dev

how to get LAST_INSERT_ID via stored procedure in php

From Dev

Using select last_insert_id() from php not working

From Dev

How to get last id after insert a value in php

From Dev

How to get last insert id from mysql database in php?

From Dev

Getting last group_id from MySQL in PHP

From Dev

PHP mysqli_insert_id() Function without INSERT

From Dev

AngularJS getting "Missing 'use strict' statement"-errors, even tough I've included the statements

From Dev

Both sql LAST_INSERT_ID() and PHP insert_id return 0

From Dev

Use SELECT statements as value to insert

From Dev

Using MySQLi prepared statements in PHP, getting "No data supplied for parameters in prepared statement "

Related Related

  1. 1

    Getting ID of last insert

  2. 2

    Use function in insert Statement Oracle

  3. 3

    Codeigniter insert_id() function retrieve correctly the last insert id?

  4. 4

    Phalcon last insert id in aftercreate function

  5. 5

    Phalcon last insert id in aftercreate function

  6. 6

    use of where cluase for last_insert_id

  7. 7

    PHP : return last inserted Id on statement

  8. 8

    Return the id of the last MySQL insert in PHP

  9. 9

    Can't return the last known ID after an INSERT statement

  10. 10

    PHP function only getting last value of array

  11. 11

    How to Get last Insert Id in php without insert any data?

  12. 12

    Use ID inserted with `INSERT OR IGNORE` in next `INSERT` statement

  13. 13

    php foreach statement to insert data into sql only inserting last line

  14. 14

    Insert element as second to last in JQuery

  15. 15

    Cant use id in function after insert returning into

  16. 16

    Getting the second to last working day

  17. 17

    Getting the second to last working day

  18. 18

    MySQL Function with LAST_INSERT_ID() giving unexpected results

  19. 19

    Mysqli multiple prepared statements with temp table and stored function. Last prepared statement not binding

  20. 20

    how to get LAST_INSERT_ID via stored procedure in php

  21. 21

    Using select last_insert_id() from php not working

  22. 22

    How to get last id after insert a value in php

  23. 23

    How to get last insert id from mysql database in php?

  24. 24

    Getting last group_id from MySQL in PHP

  25. 25

    PHP mysqli_insert_id() Function without INSERT

  26. 26

    AngularJS getting "Missing 'use strict' statement"-errors, even tough I've included the statements

  27. 27

    Both sql LAST_INSERT_ID() and PHP insert_id return 0

  28. 28

    Use SELECT statements as value to insert

  29. 29

    Using MySQLi prepared statements in PHP, getting "No data supplied for parameters in prepared statement "

HotTag

Archive