Home>

It is a script that creates the processing of the shopping cart, registers the registered contents in mysql, and throws it by e-mail.
If i know why this error is displayed, please let me know.
The corresponding line is written as "here" in the comment. By the way, it is about the 89th line slightly above the center.

Notice: Undefined index: LAST_INSERT_ID () in C: \ xampp \ htdocs \ shop \ shop_form_done.php on line 89
Corresponding source code

php7.4
session_start ();
session_regenerate_id (true);
?>



Rokumaru Farm

try
{

require_once ('../ common/common.php');

$post = sanitize ($_ POST);

$onamae = $post ['onamae'];
$email = $post ['email'];
$postal1 = $post ['postal1'];
$postal2 = $post ['postal2'];
$address = $post ['address'];
$tel = $post ['tel'];

print $onamae.'sama'.'
';
print'Thanks for your order.
';
Please check the email sent to print $email.'.
';
print'Products will be shipped to the following address.
';
print $postal1.'-'. $Postal2.'
';
print $address.'
';
print $tel.'
';

$honbun ='';
$honbun. = $onamae. "\ n \ n Thanks for your order. \ N";
$honbun. = "\ n";
$honbun. "Ordered items \ n";
$honbun. = "--------------------- \ n";

$cart = $_SESSION ['cart'];
$kazu = $_SESSION ['kazu'];
$max = count ($cart);

$dsn ='mysql: host = localhost;dbname = shop;charset = utf8';
$user ='root';
$password ='';
$dbh = new PDO ($dsn, $user, $password);
$dbh->setAttribute (PDO :: ATTR_ERRMODE, PDO :: ERRMODE_EXCEPTION);

for ($i = 0;$i<$max;$i ++)
{
$sql ='SELECT name, price FROM mst_product WHERE code =?';
$stmh = $dbh->prepare ($sql);
$data [0] = $cart [$i];
$stmh->execute ($data);

$rec = $stmh->fetch (PDO :: FETCH_ASSOC);

$name = $rec ['name'];
$price = $rec ['price'];
$kakaku [] = $price;
$suryo = $kazu [$i];
$shokei = $price * $suryo;

$honbun. = $name.'';
$honbun. = $price.'Yen x';
$honbun. = $suryo.'pieces =';
$honbun. = $shokei.'Yen \ n';

}

$sql ='INSERT INTO dat_sales (code_member, name, email, postal1, postal2, address, tel) VALUES (?,?,?,?,?,?,?)';
$stmh = $dbh->prepare ($sql);
$data = array ();
$data [] = 0;
$data [] = $onamae;
$data [] = $email;
$data [] = $postal1;
$data [] = $postal2;
$data [] = $address;
$data [] = $tel;
$stmh->execute ($data);

$sql ='select last_insert_id ()';
$stmh = $dbh->prepare ($sql);
$stmh->execute ();
$rec = $stmh->fetch (PDO :: FETCH_ASSOC);
$lastcode = $rec ['LAST_INSERT_ID ()'];//<= here
echo $lastcode;
var_dump ($rec);

$dbh = null;

for ($i = 0;$i<$max;$i ++)
{
$sql ='INSERT INTO dat_sales_product (code_sales, code_product, price, quantity) VALUES (?,?,?,?)';
$stmh = $dbh->prepare ($sql);
$data = array ();
$data [] = $lastcode;
$data [] = $cart ['$i'];
$data [] = $kakaku ['$i'];
$data [] = $kazu ['$i'];
$stmh->execute ($data);
}

$dbh = null;

$honbun. = "Free shipping. \ N";
$honbun. = "-------------------- \ n";
$honbun. = "\ n";
$honbun. = "Please transfer the price to the following account. \ N";
$honbun. = "Rokumaru Bank Yasai Branch Ordinary Account 1234567 \ n";
$honbun. = "\ n";
$honbun. = "\ n";
$honbun. = "~ Reliable Rokumaru Farm ~ \ n";
$honbun. = "\ n";
$honbun. = "○○ cases 123-4 Maru-mura, Rokumaru-gun \ n";
$honbun. = "Telephone 090-6060-xxxx \ n";
$honbun. = "Email [email protected] \ n";
$honbun. = "\ n";
// print'
';
// print nl2br ($honbun);

$title ='Thanks for your order. ';
$header ='From: [email protected]';
$honbun = html_entity_decode ($honbun, ENT_QUOTES,'UTF-8');
mb_language ('Japanese');
mb_internal_encoding ('UTF-8');
mb_send_mail ($email, $title, $honbun, $header);

$title ='You have placed an order. ';
$header ='From:'. $Email;
$honbun = html_entity_decode ($honbun, ENT_QUOTES,'UTF-8');
mb_language ('Japanese');
mb_internal_encoding ('UTF-8');
mb_send_mail ('[email protected]', $title, $honbun, $header);
}
catch (Exception $e)
{

print'We are sorry for the inconvenience caused by the failure. ';
exit ();

}

?>


`` ```

What I tried

After checking with var_dump, it seems that the value with LAST_INSERT_ID, which is the latest INSERT result, as a subscript is obtained.
Result of var_dump ();➡ array (1) {["last_insert_id ()"] =>string (2) "14"}

Supplementary information (FW/tool version, etc.)

I am using xampp 3.2.4.