重复编辑:这个问题有所不同,因为我正在尝试返回一个特定值,即在另一个函数INSERT
语句中用作外键的主键ID,同样的行动。 “重复”问题没有回答这个问题,而只是说明了如何从函数中获取返回值。不是如何获取它并将其正确插入另一个函数prepare语句中。我不确定我做得对。
我有两个表,orders
和customers
,可以在同一个操作中从一个表单插入数据。 orders
表的主键= orderID
也需要添加到customers
表中。
我可以毫无问题地将数据上传到orders
表,但对customers表的第二个查询功能什么都不做。我很确定这是由于我设置的外键约束。我做了一些研究并意识到我需要获取外键ID,可能使用$mysqli->insert_id
或PDO::lastInsertId
,但我对如何将其与当前函数一起使用感到迷茫。
的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
在order_custData
函数中就可以作为直观表示来解决问题。我最初并没有尝试用它来执行sql语句。但是,我尝试过的任何东西似乎都会抛出错误underfined variable
或致命的第一个函数的prepare函数。
感谢您的时间。
答案 0 :(得分:3)
效用函数order_Data()
已经返回ID:
…
return $orderID = $db->lastInsertId();
(尽管重写为return $db->lastInsertId();
。)
只需通过赋值变量来携带返回值:
$orderID = order_Data ($db, $product, $fName, $lName, $email);
order_custData($db, $orderID, $fN …);
答案 1 :(得分:1)
试试这个。只需通过赋值变量
来携带返回值
<?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");
?>
答案 2 :(得分:1)
您的order_Data()
函数正在返回订单ID,但您没有将index.php
中的返回值分配给$objectID
。
即。改变这个......
order_Data ($db, $product, $fName, $lName, $email);
......对此...
$orderID = order_Data ($db, $product, $fName, $lName, $email);
我建议在开发环境中检查PHP的错误报告级别以包含通知,因为PHP将触发使用未定义变量的通知。