PHP未定义变量——如何正确定义它们



在表单中输入客户名称并单击删除后,该客户及其所有数据也应被删除。表格包括<form method="POST">

当我运行它时,我会得到这个:

PHP Notice: Undefined variable: customer_name in /banking_delete.php on line 14

这是哪条线路:

$data = array ('customer_name' => $customer_name);

整个代码是这样的:

<?php
include_once 'banking_db.php';
include 'banking_display.php';
# form data
if(isset($_POST['customer_name'])){
$customer_name = $_POST['customer_name'];
}
if(isset($customer_name)){ 
echo $customer_name;
}
$sql = "delete from customer where customer_name = :customer_name;";
$stmt = $conn->prepare($sql);
# data stored in an associative array
$data = array ('customer_name' => $customer_name);
if($stmt->execute($data)){
$rows_affected = $stmt->rowCount();
echo "<h2>".$rows_affected." row deleted sucessfully!</h2>";
display("select customer_name as customer_name, customer_city as customer_city, customer_street as customer_street from customer;");
} 
else
{
echo "PDOStatement::errorInfo():n";
print_r($stmt->errorInfo());
}
$stmt = null;
$conn = null;
?>

您忘记定义数组在使用阵列之前写入这一行

$data = array();
$data = array ('customer_name' => $customer_name);

最新更新