Going to try to keep it short. I have a while loop in grid.php file to fill up a table as such...
<?php while($product = $products->fetch_assoc()) { ?> <tr> <td><?php echo $product['cd_id']?></td> <td><?php echo $product['cd_title']?></td> <td><?php echo $product['cd_musician_fname']?></td> <td><?php echo $product['cd_musician_lname']?></td> <td><?php echo $product['cd_price']?></td> <td><a href="edit.php?id=<?echo $product['cd_id'];?>" type="button" class="btn btn-primary">Edit</a></td> <td><a href="delete.php?id=<?echo $product['cd_id'];?>" type="button" class="btn btn-danger">Delete</a></td> </tr> <?php } ?> If I click the first anchor tag takes me to a edit.php file and here is the head code for that file.
<?php include '_includes/db.php'; $cd_id = trim($_GET['id']); $message = ''; include '_includes/connection.php'; if($db->connect_error){ $message = $db->connect_error; }else{ $sql = "SELECT * FROM CD WHERE cd_id = $cd_id"; $result = $db->query($sql); $row = $result->fetch_assoc(); if($db->error){ $message = $db->error; } } ?> Now I will show the html of edit.php
<!-- Product Musician last name--> <fieldset class="form-group"> <label for="cd_musician_lname">Musician's lirst name</label> <input type="text" class="form-control" id="cd_musician_lname" name="cd_musician_lname" value="<?php echo $row['cd_musician_lname'];?>"> </fieldset> <!-- End of Musician last name--> <!-- Product price--> <fieldset class="form-group"> <label for="cd_price">Product price</label> <input type="text" class="form-control" id="cd_price" name="cd_price" value="<?php echo $row['cd_price'];?>"> </fieldset> <!-- End of Product price--> <!-- Form submit button--> <a href="edit_confirm.php?id=<?echo $row['cd_id'];?>" type="submit" class="btn btn-primary">Update Record</a> <a class="btn btn-primary" href="index.php" role="button">Go Back Home</a> I have the edit.php page working just fine but if I make changes in the fields and click the submit anchor tag I get all the fields of the row empty but the PK. Here is the code for the final edit_confirm.php file
<?php include '_includes/db.php'; $cd_id = trim($_GET['id']); $cd_title = $_POST['cd_title']; $cd_musician_fname = $_POST['cd_musician_fname']; $cd_musician_lname = $_POST['cd_musician_lname']; $cd_price = $_POST['cd_price']; $message = ''; include '_includes/connection.php'; if($db->connect_error){ die("Connection failed: ".$db->connect_error); } else { $sql = "UPDATE CD SET cd_title='".$cd_title."', cd_musician_fname='". $cd_musician_fname."', cd_musician_lname='". $cd_musician_lname."', cd_price='".$cd_price."' WHERE cd_id = $cd_id "; $db->query($sql); var_dump($sql); } ?> <!DOCTYPE html> <html lang="en"> <head> <?php include '_includes/main-head.php';?> </head> <body> <?php include '_includes/main-navbar.php';?> <div class="container"> <hr> <?php if($db->query($sql) === TRUE){ ?> <h1>Record updated successfully.</h1> <?php echo $cd_title; ?> <?php echo $record->affected_rows ?> <p> record was updated in the database.</p></br> <?php } else { ?> <p>Error updating the record: </p> <?php $db->error; ?> <?php }; ?> <hr> <a class="btn btn-primary" href="index.php" role="button">Go Back Home</a> </div> <?php include '_includes/main-script.php';?> </body> </html> If you notice in the edit_confirm.php I did a var_dump to see what are the values in the variables and it shows empty.
I need help with this. Thank you in advance.
<?echo $product['cd_id'];?>(Having not used short tags ever I'm not sure the echo can run into that even). You also shouldn't just pass user input direct to SQL.