html PHP和MySQL中未定义的变量

e5nqia27  于 2022-12-21  发布在  PHP
关注(0)|答案(2)|浏览(137)

我正在使用MySQL和PHP(OOP)制作一个Web应用程序,该应用程序具有包含多个表的CRUD,我尝试回显变量,但它返回此错误:* 警告:未定义的变量$id_customer*
有人能帮我做错了什么吗?谢谢

if (isset($_GET["id_customer"]) && !empty(trim($_GET["id_customer"]))) {
    require_once "../connectDB.php";

    $sql = "SELECT * FROM Customers 
            INNER JOIN Orders
            ON Customers.id_customer = Orders.id_customer
            WHERE Customers.id_customer = ?";

    if ($stmt = $mysqli->prepare($sql)) {
        // Bind variables to the prepared statement as parameters
        $stmt->bind_param("i", $param_id);

        // Set parameters
        $param_id = trim($_GET["id_customer"]);

        // Attempt to execute the prepared statement
        if ($stmt->execute()) {
            $result = $stmt->get_result();

            if ($result->num_rows == 1) {
                /* Fetch result row as an associative array. Since the result set
                contains only one row, we don't need to use while loop */
                $row = $result->fetch_array(MYSQLI_ASSOC);

                // Retrieve individual field value
                $id_customer = $row["id_customer"];
                $fn_customer = $row["fn_customer"];
                $ln_customer = $row["ln_customer"];
                $email_customer = $row["email_customer"];
                $id_order = $row["id_order"];
                $order_dt = $row["order_dt"];
                $order_total = $row["order_total"];
                /* ?>
                // <p><?php echo $id_customer; ?></p>
                / <?php */
            }
        }
    }
    // Close statement
    $stmt->close();
        
    // Close connection
    $mysqli->close();

}

?>

<p><?php echo $id_customer; ?></p>

我试着把echo和字段值的检索放在一起,并从第一条语句中取出,就像现在这样,但这些都不起作用。

rqqzpn5f

rqqzpn5f1#

在您的代码中有几个问题,首先,您似乎绑定了(bind_param)一个尚未定义的变量($param_id),这应该是错误的,但最终将从SQL查询中返回零行。
其次,在提供的代码中,$id_customer变量只会在特定情况下定义。在声明$id_customer之前,有4个if语句需要为真。如果这4个语句中的任何一个为假,它将不会被赋值。因此,当您在脚本末尾尝试echo它时,它将不会被定义。因此出现错误消息。
如果不了解脚本想要实现什么,就很难给出答案,但简而言之,$id_customer只在if语句if ($result->num_rows == 1) { /** ... **/ }中有一个值。
以下内容将为您提供所需的输出:

<?php
if (isset($_GET["id_customer"]) && !empty(trim($_GET["id_customer"]))) {
    require_once "../connectDB.php";

    $sql = "SELECT * FROM Customers 
            INNER JOIN Orders
            ON Customers.id_customer = Orders.id_customer
            WHERE Customers.id_customer = ?";

    if ($stmt = $mysqli->prepare($sql)) {

        // Set parameters
        // Define $param_id before use, otherwise you'll be binding an undefined or null variable = no results + error
        $param_id = trim($_GET["id_customer"]);

        // Bind variables to the prepared statement as parameters
        $stmt->bind_param("i", $param_id);

        // Attempt to execute the prepared statement
        if ($stmt->execute()) {
            $result = $stmt->get_result();

            if ($result->num_rows == 1) {
                /* Fetch result row as an associative array. Since the result set
                contains only one row, we don't need to use while loop */
                $row = $result->fetch_array(MYSQLI_ASSOC);

                // Retrieve individual field value
                $id_customer = $row["id_customer"];
                $fn_customer = $row["fn_customer"];
                $ln_customer = $row["ln_customer"];
                $email_customer = $row["email_customer"];
                $id_order = $row["id_order"];
                $order_dt = $row["order_dt"];
                $order_total = $row["order_total"];
                // $id_customer will be defined here
                ?>
                   <p><?php echo $id_customer; ?></p>
                <?php
            }
            // $id_customer MAY NOT be defined here
        }
        // $id_customer MAY NOT be defined here
    }
    // Close statement
    $stmt->close();
        
    // Close connection
    $mysqli->close();
    
    // $id_customer MAY NOT be defined here
}

// $id_customer MAY NOT be defined here

?>
pkln4tw6

pkln4tw62#

如果isset($_GET['id_customer'])为true,并且成功准备并成功执行了语句,则运行查询并初始化$id_customer。但是,如果没有此类参数,则不会初始化$id_customer,并且会出现错误。无论如何,请尝试在if之前初始化它:

id_customer = '';
if (isset($_GET["id_customer"]) && !empty(trim($_GET["id_customer"]))) {
    require_once "../connectDB.php";

    $sql = "SELECT * FROM Customers 
            INNER JOIN Orders
            ON Customers.id_customer = Orders.id_customer
            WHERE Customers.id_customer = ?";

    if ($stmt = $mysqli->prepare($sql)) {
        // Bind variables to the prepared statement as parameters
        $stmt->bind_param("i", $param_id);

        // Set parameters
        $param_id = trim($_GET["id_customer"]);

        // Attempt to execute the prepared statement
        if ($stmt->execute()) {
            $result = $stmt->get_result();

            if ($result->num_rows == 1) {
                /* Fetch result row as an associative array. Since the result set
                contains only one row, we don't need to use while loop */
                $row = $result->fetch_array(MYSQLI_ASSOC);

                // Retrieve individual field value
                $id_customer = $row["id_customer"];
                $fn_customer = $row["fn_customer"];
                $ln_customer = $row["ln_customer"];
                $email_customer = $row["email_customer"];
                $id_order = $row["id_order"];
                $order_dt = $row["order_dt"];
                $order_total = $row["order_total"];
                /* ?>
                // <p><?php echo $id_customer; ?></p>
                / <?php */
            }
        }
    }
    // Close statement
    $stmt->close();
        
    // Close connection
    $mysqli->close();

}

?>

<p><?php echo $id_customer; ?></p>

相关问题