0

Let me post some code first so I can explain more of what I want to accomplish.

<?php


require "../config.php";
require "../common.php";

if (isset($_POST['submit'])) {
  if (!hash_equals($_SESSION['csrf'], $_POST['csrf'])) die();

  try  {
    $connection = new PDO($dsn, $username, $password, $options);

    $sql = "SELECT * FROM saledata WHERE itemnumber = :itemnumber";

    $itemnumber = $_POST['itemnumber'];
    $statement = $connection->prepare($sql);
    $statement->bindParam(':itemnumber', $itemnumber, PDO::PARAM_STR);
    $statement->execute();

    $result = $statement->fetchAll();
  } catch(PDOException $error) {
      echo $sql . "<br>" . $error->getMessage();
  }
}
?>
<?php require "templates/header.php"; ?>

<?php  
if (isset($_POST['submit'])) {
  if ($result && $statement->rowCount() > 0) { ?>
    <h2>Results</h2>
<style>
table, th, td {
  border: 1px solid black;
}
</style>
    <table>
      <thead>
        <tr>
        <th>Item Number</th>
        <th>Sale Number</th>
        <th>Lot Number</th>
        <th>Item Description</th>
        <th>Seller Number</th>
        <th>Buyer Number</th>
        <th>Sold</th>
        <th>Sold</th>
        <th>Paid</th>
        <th>Date</th>
        </tr>
      </thead>
      <tbody>
      <?php foreach ($result as $row) : ?>
        <tr>
        <td><?php echo escape($row["itemnumber"]); ?></td>
        <td><?php echo escape($row["salenumber"]); ?></td>
        <td><?php echo escape($row["lotnumber"]); ?></td>
        <td><?php echo escape($row["itemdescription"]); ?></td>
        <td><?php echo escape($row["sellernumber"]); ?></td>
        <td><?php echo escape($row["buyernumber"]); ?></td>
        <td><?php echo escape($row["sold"]); ?></td>
        <td><?php echo escape($row["paid"]); ?></td>
        <td><?php echo escape($row["amount"]); ?></td>
        <td><?php echo escape($row["date"]); ?> </td>
        </tr>
      <?php endforeach; ?>
      </tbody>
    </table>
    <?php } else { ?>
      <blockquote>No results found for <?php echo escape($_POST['itemnumber']); ?>.</blockquote>
    <?php } 
} ?> 

<h2>Find item based on Item Number</h2>

<form method="post">
  <input name="csrf" type="hidden" value="<?php echo escape($_SESSION['csrf']); ?>">
  <label for="itemnumber">Item Number</label>
  <input type="text" id="itemnumber" name="itemnumber"><br><br>
  <input type="submit" name="submit" value="View Results">
</form>

<?php require "templates/footer.php"; ?>
(END)

This produces an HTML table containing the data in the database in rows. What I would like is a button at the beginning or end of the rows that will modify a field in that item's row. An example is the column for sold or not sold. What I would like is a button at either the beginning or the end of each row that modifies the "sold" column from N to Y for that item. I have tried to put a block of code in the code here at the end but I can't get it to do anything. I can get the button to show using the HTML tag but can't get it to actually do anything. Here is the concept of the foreach:

        <td><?php echo escape($row["sold"]); ?></td>
        <td><?php echo escape($row["paid"]); ?></td>
        <td><?php echo escape($row["amount"]); ?></td>
        <td><?php echo escape($row["date"]); ?> </td>
        <td>BUTTON and code to change Sold from N to Y</td>
</tr>

Any assistance would be greatly appreciated! I know my stuff is probably elementary and not very efficient, but I have been working on this project for several weeks and never worked with a MYSQL database before.

3
  • I suppose you have two ways to do it. Put the button in a form with the relative information and submit the page for reload. Or you could write some java script to modify the page and make an ajax call to update the database. Commented Sep 24, 2019 at 13:58
  • @jason-k: I have tried to figure out how to do the first method you describe. But that is why I asked on here. . . I can't figure out how to do it. Know even less about javascript than I do about MYSQL. . . one thing at a time. . . Commented Sep 24, 2019 at 17:38
  • I found another article showing some info I might be able to use at: stackoverflow.com/questions/18136280/… I was not submitting the form back to the page for one thing, and I was trying to put the entire process inside the <td></td> and it wouldn't work that way. I will try to figure out how to do it with that example. Thank you everybody! Commented Sep 24, 2019 at 18:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.