Any Any - 9 months ago 132
SQL Question

Invalid parameter number no parameters were bound

I got undefined index error for the variables in the loop and also :

Error: SQLSTATE[HY093]: Invalid parameter number: no parameters were

I'm not sure what i did wrong. My WHERE clause is from a session, $_SESSION['customer_num'];

<table class="table table-striped table-bordered">
<th>order id</th>
<th>total amount (inc tax)</th>
try {
$custnum = $_SESSION['customer_num'];
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$stmt = $conn->prepare("SELECT * FROM tbl_order WHERE customer_num = :custnum");
$stmt->bindParam(':custnum', $custnum, PDO::PARAM_INT);
$results = $stmt->fetchAll();
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
foreach($results as $readrow) { ?>
<td><?php echo $results['order_num']; ?></td>
<td><?php echo $results['order_date']; ?></td>
<td><?php echo $results['order_status']; ?></td>
<td><?php echo $results['order_total']; ?></td>
<a href="order_details.php?oid=<?php echo $results['order_num']; ?>" class="btn btn-warning btn-xs" role="button">Details</a>
<?php }
$conn = null;?>


You're trying to execute your statement before you bind the parameter. execute() is the last thing you do, after preparing and binding.

$stmt->execute(); // Remove this first execute
$stmt->bindParam(':custnum', $custnum, PDO::PARAM_INT);

Examples here: