Home > OS >  Undefined variable in PHP and MySQL
Undefined variable in PHP and MySQL

Time:12-18

I'm making an web app with MySQL and PHP (OOP) thats has CRUD with multiple tables, and I'm trying to echo the variable but it returns this error: Warning: Undefined variable $id_customer

Can someone help on what am I doing wrong? Thanks

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>

I tried putting the echo together with the retrieve of the fields values, and out of the first statement, as it is now. None of this worked.

CodePudding user response:

There are a couple of issues in your code, firstly you appear to be binding (bind_param) a variable ($param_id) which isn't yet defined which should error, but ultimately will return zero rows from your SQL query.

Secondly, where it is in the provided code, the $id_customer variable will only be defined in certain circumstances. There are 4 if statements which need to be true before $id_customer is declared. Should any of those 4 statements be false, it won't be assigned a value. So when you try to echo it at the end of your script, it won't be defined. Hence the error message.

It's difficult to provide you with an answer without understanding exactly what your script wants to achieve. But in short, $id_customer will only have a value inside the if statement if ($result->num_rows == 1) { /** ... **/ }.

The following will provide you with the desired output:

<?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

?>

CodePudding user response:

If isset($_GET['id_customer']) is true AND a statement is successfully prepared AND successfully executed, then you run a query and initialize $id_customer. Yet, if you have no such parameter, it will not be initialized and you will get an error. Try initializing it no matter what before the 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>
  • Related