PHP MYSQL and updating data in a table - php

Good day, all!
I am trying to figure out how to get this chunk of code to do what I want but it eludes me.
What this script is supposed to do is to look through a database of consignment items, pick out the ones that have sold='n' and list them by item number. It displays all of the items in a list with a "Sell" link at the end of each item row. Once the Buyer Number and Amount are entered, the Sell link is clicked, the UPDATE updates the item and changes sold from 'n' to 'y' and once that happens, the item disappears from the list. It works exactly as I expect, except for one little issue that has eluded me. Each time the page is loaded, it doesn't change the amount and buyernumber, but it does change the sold from 'n' to 'y' for the first item only and after that it works as expected, adding the buyernumber and amount to all subsequent items. Until you browse away from it and then it won't do the first item correctly the next time you come back to the page. I have tried all kinds of things to make it work and this is the closest I have to what I want. Any help would be appreciated!
<?php
/**
* Sell an Item
*/
require "../config.php";
require "../common.php";
$success = null;
if (isset($_POST["submit"])) {
if (!hash_equals($_SESSION['csrf'], $_POST['csrf'])) die();
try {
$connection = new PDO($dsn, $username, $password, $options);
$itemnumber = $_POST["submit"];
$buyernumber = $_POST["buyernumber"];
$amount = $_POST["amount"];
$sql = "UPDATE consignitem SET sold='y', amount='$amount', buyernumber='$buyernumber' WHERE sold='n' AND itemnumber = $itemnumber";
$statement = $connection->prepare($sql);
$statement->bindValue(':itemnumber', $itemnumber);
$statement->bindValue(':buyernumber', $buyernuumber);
$statement->bindValue(':amount', $amount);
$statement->bindValue(':sold', $sold);
$statement->execute();
$success = "Item successfully updated";
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
}
try {
$connection = new PDO($dsn, $username, $password, $options);
$sql = "SELECT * FROM consignitem WHERE sold='n'";
$statement = $connection->prepare($sql);
$statement->execute();
$result = $statement->fetchAll();
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
?>
<?php require "templates/header.php"; ?>
<h2>Mark Items as Sold</h2>
<?php if ($success) echo $success; ?>
<style>
table, th, td {
border: 1px solid black;
}
</style>
<form method="post">
<input name="csrf" type="hidden" value="<?php echo escape($_SESSION['csrf']); ?>">
<table>
<thead>
<tr>
<th>Sale Number</th>
<th>Item Number</th>
<th>Lot Number</th>
<th>Category</th>
<th>Item Description</th>
<th>Reserve</th>
<th>Seller Number</th>
<th>Amount</th>
<th>Buyer Number</th>
<th>Sold</th>
<th>Paid</th>
<th>Date</th>
<th>Mark</th>
</tr>
</thead>
<tbody>
<?php foreach ($result as $row) : ?>
<tr>
<td><?php echo escape($row["salenumber"]); ?></td>
<td><?php echo escape($row["itemnumber"]); ?></td>
<td><?php echo escape($row["lotnumber"]); ?></td>
<td><?php echo escape($row["category"]); ?></td>
<td><?php echo escape($row["itemdescription"]); ?></td>
<td><?php echo escape($row["reserve"]); ?></td>
<td><?php echo escape($row["sellernumber"]); ?></td>
<td><?php echo escape($row["amount"]); ?><br><input type="text" name="amount" id="amount" size="8"></td>
<td><?php echo escape($row["buyernumber"]); ?><br><input type="text" name="buyernumber" id="buyernumber" size="12"></td>
<td><?php echo escape($row["sold"]); ?></td>
<td><?php echo escape($row["paid"]); ?></td>
<td><?php echo escape($row["date"]); ?> </td>
<td><button type="submit" name="submit" value="<?php echo escape($row["itemnumber"]); ?>">Sell</button></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</form>
<br>
<?php require "templates/footer.php"; ?>

You're repeating the same input names in all thr rows of the form. When the form is submitted, the $_POST variables will just contain the value from the last row of the form, not the row that the user clicked the submit button for.
You can give the inputs array-style names that are indexed by itemnumber. $_POST['buyernumber'] and $_POST['amount'] will then be arrays, and you can use $_POST['submit'] to get the appropriate array index.
You also need to use placeholders in the SQL that match the placeholders in bindValue(), rather than substituting the variables directly in the query.
<?php
if (isset($_POST["submit"])) {
if (!hash_equals($_SESSION['csrf'], $_POST['csrf'])) die();
try {
$connection = new PDO($dsn, $username, $password, $options);
$itemnumber = $_POST["submit"];
$buyernumber = $_POST["buyernumber"][$itemnumber];
$amount = $_POST["amount"][$itemnumber];
$sql = "UPDATE consignitem SET sold='y', amount= :amount, buyernumber= :buyernumber WHERE sold='n' AND itemnumber = :itemnumber";
$statement = $connection->prepare($sql);
$statement->bindValue(':itemnumber', $itemnumber);
$statement->bindValue(':buyernumber', $buyernuumber);
$statement->bindValue(':amount', $amount);
$statement->execute();
$success = "Item successfully updated";
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
}
try {
$connection = new PDO($dsn, $username, $password, $options);
$sql = "SELECT * FROM consignitem WHERE sold='n'";
$statement = $connection->prepare($sql);
$statement->execute();
$result = $statement->fetchAll();
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
?>
<?php require "templates/header.php"; ?>
<h2>Mark Items as Sold</h2>
<?php if ($success) echo $success; ?>
<style>
table, th, td {
border: 1px solid black;
}
</style>
<form method="post">
<input name="csrf" type="hidden" value="<?php echo escape($_SESSION['csrf']); ?>">
<table>
<thead>
<tr>
<th>Sale Number</th>
<th>Item Number</th>
<th>Lot Number</th>
<th>Category</th>
<th>Item Description</th>
<th>Reserve</th>
<th>Seller Number</th>
<th>Amount</th>
<th>Buyer Number</th>
<th>Sold</th>
<th>Paid</th>
<th>Date</th>
<th>Mark</th>
</tr>
</thead>
<tbody>
<?php foreach ($result as $row) : ?>
<tr>
<td><?php echo escape($row["salenumber"]); ?></td>
<td><?php echo escape($row["itemnumber"]); ?></td>
<td><?php echo escape($row["lotnumber"]); ?></td>
<td><?php echo escape($row["category"]); ?></td>
<td><?php echo escape($row["itemdescription"]); ?></td>
<td><?php echo escape($row["reserve"]); ?></td>
<td><?php echo escape($row["sellernumber"]); ?></td>
<td><?php echo escape($row["amount"]); ?><br><input type="text" name="amount[<?php echo escape($row["itemnumber"]); ?>]" size="8"></td>
<td><?php echo escape($row["buyernumber"]); ?><br><input type="text" name="buyernumber[<?php echo escape($row["itemnumber"]); ?>]" size="12"></td>
<td><?php echo escape($row["sold"]); ?></td>
<td><?php echo escape($row["paid"]); ?></td>
<td><?php echo escape($row["date"]); ?> </td>
<td><button type="submit" name="submit" value="<?php echo escape($row["itemnumber"]); ?>">Sell</button></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</form>
<br>
<?php require "templates/footer.php"; ?>

Related

Database data appears in single line

I'm only getting started with PHP and I have this problem that the data that I wish to display inside a html table from my phpmyadmin database appears in a single row rather than in multiple rows
attached screenshot here
Here's the code:
try {
$conn = new PDO("mysql:host=127.0.0.1;port=3306;dbname=iwp_proj", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
$query = "SELECT * FROM `blog`";
$sql = $conn->prepare($query) ;
$sql->execute();
$q = $sql->fetchAll(PDO::FETCH_ASSOC);
?>
<div align='center' style="background-color: white; opacity: 0.9">
<h1>REVIEWS</h1>
<table align='center' border='2' style=" border-collapse: collapse; bordercolor=#272121">
<thead align='center'>
<tr align='center'>
<th>Destination</th>
<th>Rating</th>
<th>Date of Journey</th>
<th>Comments</th>
</tr>
</thead>
<tbody>
<tr>
<?php foreach($q as $row){ ?>
<td><?php echo ($row['dest']); ?></td>
<td><?php echo ($row['rating']); ?></td>
<td><?php echo ($row['bdate']); ?></td>
<td><?php echo ($row['content']); ?></td>
<?php } ?>
</tr>
<br>
</tbody>
</table>
</div>
If I use the following code then nothing appears at all
<?php while($row = $sql->fetch(PDO::FETCH_ASSOC)) { ?>
<?php foreach($q as $row){ ?>
<td><?php echo ($row['dest']); ?></td>
<td><?php echo ($row['rating']); ?></td>
<td><?php echo ($row['bdate']); ?></td>
<td><?php echo ($row['content']); ?></td>
<?php } ?>
<?php } ?>
What am I doing wrong?
What happens is that you're iterating over "td" elements only, instead of doing it on the "tr" elements. The line which contains the forEach method should be extracted from its current scope. An example follows:
<?php foreach($q as $row){ ?>
<tr>
<td><?php echo ($row['dest']); ?></td>
<td><?php echo ($row['rating']); ?></td>
<td><?php echo ($row['bdate']); ?></td>
<td><?php echo ($row['content']); ?></td>
</tr>
<?php } ?>

Using PHP to get data from MSQL and need a button in the foreach table

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.

PHP how to delete a specific row from database with html table button

I am trying to delete a specific entry from the database with a button. I know this has already been asked several times, unfortunately the solutions don't really work for me. The goal would be, if I click on the button in the 3rd row, that this line is deleted. I have the problem that I always only delete the last ids or all ids at once.
Maybe someone can help me, thank you.
admin.php
<?php
include('connection.php');
include('read.php');
?>
<form action="admin.php" method="post">
<div class="table-wrapper">
<div class="table-scroll">
<table id="myTable">
<tr>
<th>ID</th>
<th>Kartentyp</th>
<th>Absender</th>
<th>Empfänger</th>
<th>Sendedatum</th>
<th id="smallCol">Verschickt</th>
<th id="smallCol">Bestätigung</th>
<th>Edit</th>
</tr>
<?php
foreach ($result as $row) {
if ($row['Dispatched'] == 0) {
$dispatched = 'Pending';
} else {
$dispatched = 'Versendet';
}
?>
<tr class="Alle <?php echo $row['Category']; ?> <?php echo $row['Dispatched']; ?>">
<td><?php echo $row['ID']; ?></td>
<td><?php echo $row['Category']; ?></td>
<td><?php echo $row['Sender']; ?></td>
<td><?php echo $row['Receiver']; ?></td>
<td><?php echo $row['SendDate']; ?></td>
<td><?php echo $dispatched; ?></td>
<td>Placeholder</td>
<td><input type="submit" name="delete" value="delete" >
<button data-target="modal1" class="modal-trigger">Modal</button>
</td>
</tr>
<?php
}
if (isset($_POST['delete'])) {
echo $row['ID'];
$deleteQuery = "DELETE FROM card WHERE id = " . $row['ID'];
$statement = $pdo->prepare($deleteQuery);
$statement->execute();
}
?>
</table>
</div>
</div>
</form>
read.php
<?php
include('connection.php');
$statement = $pdo->prepare("SELECT * FROM card ORDER BY ID ASC");
$statement->execute();
$result = $statement->fetchAll();
if ($statement->rowCount() > 0) {
foreach ($statement->fetchAll() as $row) {
$id = $row['ID'];
$imagePath = $row["ImagePath"];
$sender = $row["Sender"];
$senderEmail = $row["SenderEmail"];
$receiver = $row["Receiver"];
$receiverEmail = $row["ReceiverEmail"];
$subject = $row["Subject"];
$text = $row["Text"];
$sendDate = $row["SendDate"];
$dispatched = $row["Dispatched"];
$category = $row['Category'];
}
}
?>
You can archive this by using get request without posting whole data to server.
if( !empty($_GET['id']) ){
$deleteQuery = "DELETE FROM card WHERE id = " . $id_to_delete;
$statement = $pdo->prepare($deleteQuery);
$statement->execute();
header('location:youfilename.php');
exit;
}
?>
<div class="table-wrapper">
<div class="table-scroll">
<table id="myTable">
<tr>
<th>ID</th>
<th>Kartentyp</th>
<th>Absender</th>
<th>Empfänger</th>
<th>Sendedatum</th>
<th id="smallCol">Verschickt</th>
<th id="smallCol">Bestätigung</th>
<th>Edit</th>
</tr>
<?php
foreach ($result as $row) {
if ($row['Dispatched'] == 0) {
$dispatched = 'Pending';
} else {
$dispatched = 'Versendet';
}
?>
<tr class="Alle <?php echo $row['Category']; ?> <?php echo $row['Dispatched']; ?>">
<td><?php echo $row['ID']; ?></td>
<td><?php echo $row['Category']; ?></td>
<td><?php echo $row['Sender']; ?></td>
<td><?php echo $row['Receiver']; ?></td>
<td><?php echo $row['SendDate']; ?></td>
<td><?php echo $dispatched; ?></td>
<td>Placeholder</td>
<td>Delete
<button data-target="modal1" class="modal-trigger">Modal</button>
</td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</form>
You can modify your code like that :
<?php
include('connection.php');
include('read.php');
if( !empty($_POST) ){
foreach( $_POST as $key_post => $value_post ){
if (preg_match('/delete_/i',$key_post) ) {
$id_to_delete = (integer) str_replace('delete_','', $key_post);
$deleteQuery = "DELETE FROM card WHERE id = " . $id_to_delete;
$statement = $pdo->prepare($deleteQuery);
$statement->execute();
}
}
}
?>
<form action="admin.php" method="post">
<div class="table-wrapper">
<div class="table-scroll">
<table id="myTable">
<tr>
<th>ID</th>
<th>Kartentyp</th>
<th>Absender</th>
<th>Empfänger</th>
<th>Sendedatum</th>
<th id="smallCol">Verschickt</th>
<th id="smallCol">Bestätigung</th>
<th>Edit</th>
</tr>
<?php
foreach ($result as $row) {
if ($row['Dispatched'] == 0) {
$dispatched = 'Pending';
} else {
$dispatched = 'Versendet';
}
?>
<tr class="Alle <?php echo $row['Category']; ?> <?php echo $row['Dispatched']; ?>">
<td><?php echo $row['ID']; ?></td>
<td><?php echo $row['Category']; ?></td>
<td><?php echo $row['Sender']; ?></td>
<td><?php echo $row['Receiver']; ?></td>
<td><?php echo $row['SendDate']; ?></td>
<td><?php echo $dispatched; ?></td>
<td>Placeholder</td>
<td><input type="submit" name="delete_<?php echo $row['ID']; ?>" value="delete" >
<button data-target="modal1" class="modal-trigger">Modal</button>
</td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</form>
Your error was you don't used the POST value to delete the row but the last ID store in the row variable that come from your query reading.
Becarfull too you make a wrong usage of the prepare function in PDO.

php search table by id

Im trying to display all the information from a single row in my database table by the id.
When I click search it shows the entire table and the search really doesnt do anything..
Any help would be appreciated. Thank you :)
im using php/mysql
<?php
session_start();
include('connect.php');
if(isset($_POST['search']))
{
$q = $_POST['srch_query'];
?>
<form method="post" action="">
<input type="text" name="srch_query" value="<?php echo $q ?>" required>
<input type="submit" name="search" value="Search">
</form>
<?php
$search = $db->prepare("SELECT species, tree_desc, age, city, state, location FROM tree_info");
$search->execute();
if($search->rowcount()==0){ echo "No product found!"; }
else
{
echo "Search Result:</br>";?>
<table border="1" cellspacing="0" cellpadding="4">
<thead>
<tr>
<th>Species</th>
<th>Description</th>
<th>Age</th>
<th>City</th>
<th>State</th>
<th>Location</th>
</tr>
</thead>
<tbody>
<?php foreach($search as $s)
{ ?>
<tr class="record">
<td><?php echo $s['species']; ?></td>
<td><?php echo $s['tree_desc']; ?></td>
<td><?php echo $s['age']; ?></td>
<td><?php echo $s['city']; ?></td>
<td><?php echo $s['state']; ?></td>
<td><?php echo $s['location']; ?></td>
</tr>
<?php }
}
} ?>
</tbody>
</table>
You should use $q in your query to filter your resultset. Currently you only send the var to the server, without doing anything with it.
$search = $db->prepare("SELECT species, tree_desc, age, city, state, location FROM tree_info WHERE tree_id=:id");
$search->execute([':id' => (int) $q]);
Your $search variable is a PDOStatement object. You need to fetch result.
Do it this way :
<?php
$results = $search->fetchAll();
foreach($results as $s)
{ ?>
<tr class="record">
<td><?php echo $s['species']; ?></td>
<td><?php echo $s['tree_desc']; ?></td>
<td><?php echo $s['age']; ?></td>
<td><?php echo $s['city']; ?></td>
<td><?php echo $s['state']; ?></td>
<td><?php echo $s['location']; ?></td>
</tr>
<?php } ?>
And check the doc for more informations on the PDOStatement if needed.
http://php.net/manual/fr/class.pdostatement.php
EDIT : my bad, I was focusing on the wrong problem.
You must add your $q to your query if it exists :
if ($q) {
$search = $db->prepare("SELECT species, tree_desc, age, city, state, location FROM tree_info WHERE id = :id");
$search->bindParam('id', $q, PDO::PARAM_INT);
} else {
$search = $db->prepare("SELECT species, tree_desc, age, city, state, location FROM tree_info");
}

how do i create html table like on the picture

i have a 3 table and im doing inner join in the output below
how can i achive something like this
so far i already have code for expanding the row. the real problem here is how do i get all the signatoryname for each tracknum. im using php and html
this is my code
<table class="table table-bordered ">
<thead>
<tr>
<th>Track Number</th>
<th>Document Title</th>
<th>Document Type</th>
<th>Date Filled</th>
<th> </th>
</tr>
</thead>
<?php while ($r = $q->fetch()): ?>
<tr>
<td><?php echo $r['tracknum'] ?></td>
<td><?php echo $r['doctitle'] ?></td>
<td><?php echo $r['doctype'] ?></td>
<td><?php echo $r['datefilled'] ?></td>
<td>
<span class="btnshow glyphicon glyphicon-plus-sign"></span>
</td>
</tr>
<tr><td colspan="5"><p><?php echo $r['signatoryname'] ?></p>
</td></tr>
<?php endwhile; ?>
</table>
for the table to expand
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$(function() {
$("td[colspan=5]").find("p").hide();
$("table").click(function(event) {
event.stopPropagation();
var $target = $(event.target);
if ( $target.closest("td").attr("colspan") > 1 ) {
$target.slideUp();
} else {
$target.closest("tr").next().find("p").slideToggle();
}
});
});
});//]]>
</script>
this is the output of the code
i need to group the data below by tracknum but i need the signatoryname
i want the html row to be expandable and list the signatoryname of that tracknum bellow it. thanks.
update: so far this is my code
UPDATE: below is the correct code:
<?php
require_once 'dbconfig.php';
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname",
$username, $password);
// execute the stored procedure
$sql = 'CALL sp_trasactionsignatory()';
$q = $conn->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
} catch (PDOException $pe) {
die("Error occurred:" . $pe->getMessage());
}
?>
<table class="table table-bordered ">
<thead>
<tr>
<th>Track Number</th>
<th>Document Title</th>
<th>Document Type</th>
<th>Date Filled</th>
<th> </th>
</tr>
</thead>
<?php while ($r = $q->fetch()): ?>
<tr>
<td><?php echo $r['tracknum'] ?></td>
<td><?php echo $r['doctitle'] ?></td>
<td><?php echo $r['doctype'] ?></td>
<td><?php echo $r['datefilled'] ?></td>
<td>
<span class="btnshow glyphicon glyphicon-plus-sign"></span>
</td>
</tr>
<tr><td colspan="5">
<?php
require_once 'dbconfig.php';
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname",
$username, $password);
$_tempp1 = $r['tracknum'];
$stmt = $conn->prepare("CALL sp_gettransactsignatory(?)");
$stmt->bindParam(1, $_tempp1, PDO::PARAM_STR, 30);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
echo "<p>" . $row['signatoryname'] . "</p>";
}
} catch (PDOException $pe) {
die("Error occurred:" . $pe->getMessage());
}
?>
</td></tr>
<?php endwhile; ?>
</table>
Problem:
How do I get all the signatoryname for each tracknum?
Solution:
Your very first initial query would be like this,
(Since you didn't provide the table name, change the table name from the below queries)
$q = $connection->query("SELECT tracknum, doctitle, doctype, datefilled FROM tablename GROUP BY tracknum");
Then comes to your table,
<table class="table table-bordered ">
<thead>
<tr>
<th>Track Number</th>
<th>Document Title</th>
<th>Document Type</th>
<th>Date Filled</th>
<th> </th>
</tr>
</thead>
<?php while ($r = $q->fetch_assoc()): ?>
<tr>
<td><?php echo $r['tracknum']; ?></td>
<td><?php echo $r['doctitle'] ?></td>
<td><?php echo $r['doctype'] ?></td>
<td><?php echo $r['datefilled'] ?></td>
<td>
<span class="btnshow glyphicon glyphicon-plus-sign"></span>
</td>
</tr>
<tr><td colspan="5">
<?php
$result_set = $connection->query("SELECT signatoryname FROM tablename WHERE tracknum = {$r['tracknum']}");
while ($row = $result_set->fetch_assoc()){
echo "<p>" . $row['signatoryname'] . "</p>";
}
?>
</td></tr>
<?php endwhile; ?>
</table>
Don't forget to change the tablename in both the queries.
Edited:
If you using PDO extensions to execute your queries then you can do something like this,
<?php
$_tempp1 = $r['tracknum'];
$stmt = $connection->prepare("SELECT signatoryname FROM tablename WHERE tracknum = :tracknum");
$stmt->bindParam(':tracknum', $_tempp1, PDO::PARAM_STR, 30);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
echo "<p>" . $row['signatoryname'] . "</p>";
}
?>

Categories