1

As it said on the title I'm pretty sure I've been defined it, btw I am new to PHP so anybody could help me, and also I'm sorry that some variable is in bahasa but it's not the issue so...

I try every method that stack overflow provides in dealing similar issue but nothing work.

<?php
// Check existence of id parameter before processing further
if(isset($_GET["id"]) && !empty(trim($_GET["id"]))){
    // Include config file
   include "koneksi.php";

    // Prepare a select statement
    $sql ="SELECT tabel_barang.nama_barang, tabel_stock.quantity
        FROM tabel_barang
        LEFT JOIN tabel_stock ON tabel_barang.tabel_barang id=tabel_stock.tabel_stock id
        ORDER BY tabel_barang.nama_barang";

    if($stmt = mysqli_prepare($db, $sql)){
        // Bind variables to the prepared statement as parameters
        mysqli_stmt_bind_param($stmt, "i", $param_id);

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

        // Attempt to execute the prepared statement
        if(mysqli_stmt_execute($stmt)){
            $result = mysqli_stmt_get_result($stmt);

            if(mysqli_num_rows($result) == 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 = mysqli_fetch_array($result, MYSQLI_ASSOC);

                // Retrieve individual field value
                $nama_barang = $row["nama_barang"];
                $quantity = $row["quantity"];

            } else{ 
                // URL doesn't contain valid id parameter. Redirect to error page
                header("location: error.php");
                exit();
            }

        } else{
            echo "Terjadi kesalahan, silahkan coba lagi.";
        }


   // Close statement
    mysqli_stmt_close($stmt);

    // Close connection
    mysqli_close($db);

    } 

} else{
    // URL doesn't contain id parameter. Redirect to error page
    header("location: error.php");
    exit();
}
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Check Data</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
    <style type="text/css">
        .wrapper{
            width: 500px;
            margin: 0 auto;
        }
    </style>
</head>
<body>
    <div class="wrapper">
        <div class="container-fluid">
            <div class="row">
                <div class="col-md-12">
                    <div class="page-header">
                        <h1>Check Data</h1>
                    </div>
                    <div class="form-group">
                        <label>Nama Barang</label>
                        <p class="form-control-static"><?php echo $row["nama_barang"]; ?></p>
                    </div>
                    <div class="form-group">
                        <label>Quantity</label>
                        <p class="form-control-static"><?php echo $row["quantity"]; ?></p>
                    </div>
                    <p><a href="stock.php" class="btn btn-primary">Kembali</a></p>
                </div>
            </div>        
        </div>
    </div>
</body>
</html>

I expect that the data will be shown, but it giving me an error report like this

Notice: Undefined variable: row in C:\xampp\htdocs\test\transaksi_toko\read_stock.php on line 85

and also this

Notice: Undefined variable: row in C:\xampp\htdocs\test\transaksi_toko\read_stock.php on line 89

I'm sorry if my information or code doesn't give enough information it's my first time asking in this platform.

Sayed Mohd Ali
  • 2,156
  • 3
  • 12
  • 28
  • Your query has no placeholders, so it fails. Therefor you get to the else block of `if(mysqli_stmt_execute($stmt)){`, and there's nothing stopping the code there - so it continues down to your HTML where you use `$row`- – Qirel Jul 16 '19 at 08:03
  • your prepared statement does not have a placeholder yet you attempt to bind a parameter/value. You do not need to use a prepared statement with a simple query such as this where there is no user supplied data as input. – Professor Abronsius Jul 16 '19 at 08:03

0 Answers0