Reputation: 181
I have a HTML Table with Dynamic Columns which increase or decrease as per the user's selection. I need to Insert this HTML Table in a MySQL Database table after taking inputs from the users in the front end.
The columns from Category to Product Name are constant post that the location column changes depending upon the user requirement.
Once the input is made and clicked on save i need to insert this data into production table in the below format:
The code that i have completed so far kindly guide me with a proper direction:
<?php
if(isset($_POST['for_post_market'])){ $market = $_POST['for_post_market']; }
if(isset($_POST['for_post_prod_date'])){ $date_prod = $_POST['for_post_prod_date']; }
if(isset($_POST['for_post_sale_date'])){ $date_sale = $_POST['for_post_sale_date']; }
$query = 'SELECT * FROM product WHERE prod_status="Active"';
$stmt = $DB_con->prepare($query);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$result[$row['prod_cat_name']][] = $row['prod_id'];
}
?>
<form method="post">
<table id="invoices" border="1" class="table table-striped table-bordered">
<thead>
<col width="65">
<col width="65">
<th>Category</th>
<th>Product ID</th>
<th>Product Name</th>
<th hidden="true">Production Date</th>
<th hidden="true">Sales Date</th>
<?php
$a=count($market);
for($i=0;$i<$a;$i++) {
echo '<th><input type="hidden" value="'. $market[$i] . '">'. $market[$i] .'</th>';
}
?>
</thead>
<tbody>
<?php
foreach($result as $id => $invoices) {
echo '<tr>';
echo '<td rowspan='. count($invoices) . '>' . $id . '</td>';
$count = 0;
foreach ($invoices as $invoice) {
if ($count != 0) {
echo '<tr>';
}
$count++;
echo '<td>' . $invoice . '</td>';
?>
<?php
$psql = $DB_con->prepare("SELECT * FROM product WHERE prod_id='$invoice'");
$psql->execute();
$resultpro = $psql->fetchall(PDO::FETCH_ASSOC);
foreach($resultpro as $line) {
}
?>
<td><?php echo $line['prod_name']; ?></td>
<?php
echo '<td hidden="true">' . $date_prod . '</td>';
echo '<td hidden="true">' . $date_sale . '</td>';
$a=count($market);
for($j=0;$j<$a;$j++) {
echo '<td><input type="text" name="'. $market[$j] .' "class="form-control" maxlength="6" size="4"></td>';
}
}
}
echo '</tbody>';
echo '</table>';
?>
<button type="submit" class="btn btn-default waves-effect waves-light" name="btn-saveforcast" id="btn-saveforcast">Save</button>
</form>
<?php
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
else
{
?>
Complete Code Link
Upvotes: 0
Views: 3428
Reputation: 783
Your attribute name
have the same value for all <input>
fields
For example "97th Street" etc.
Use market ID instead with some prefix.
Check this first, and try to avoid spaces in names.
Each <input>
value with identical names will be overwritten.
Upvotes: 1
Reputation: 783
Put empty square brackets in the end of "name" attribute like
<input type="text" name="'. $market[$j][] . '
and You will gain what You want!!!
Array
(
[79th_Street] = Array
(
[0] = one
[1] = two
[2] = three
[3] = four
)
[btn-saveforcast] = ;
)
Additionally You can put "Product ID" to this array and then You can get separete arrays for each ProductID
<input type="text" name="'. $market[$j][$product_id][] . '
and result will be:
Array
(
[79th_Street] = Array
(
[2] = Array
(
[0] = one
)
[3] = Array
(
[0] = two
[1] = three
[2] = four
)
)
[btn-saveforcast] = ;
)
Upvotes: 0
Reputation: 2239
I tried formatting your code to make it somewhat readable and this is what I got:
<?php
if(isset($_POST[ 'for_post_market'])){
$market=$ _POST[ 'for_post_market'];
}
if(isset($_POST[ 'for_post_prod_date'])){
$date_prod=$ _POST[ 'for_post_prod_date'];
}
if(isset($_POST[ 'for_post_sale_date'])){
$date_sale=$ _POST[ 'for_post_sale_date'];
}
$query='SELECT * FROM product WHERE prod_status="Active"' ;
$stmt=$ DB_con->prepare($query);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$result[$row['prod_cat_name']][] = $row['prod_id'];
}
?>
<form method="post">
<table id="invoices" border="1" class="table table-striped table-bordered">
<thead>
<col width="65">
<col width="65">
<th>Category</th>
<th>Product ID</th>
<th>Product Name</th>
<th hidden="true">Production Date</th>
<th hidden="true">Sales Date</th>
<?php
$a=count($market); for($i=0;$i<$a;$i++) { echo '<th><input type="hidden" value="'. $market[$i] . '">'. $market[$i] . '</th>'; }
?>
</thead>
<tbody>
<?php foreach($result as $id=> $invoices) {
echo '<tr>';
echo '<td rowspan='. count($invoices) . '>' . $id . '</td>';
$count = 0;
foreach ($invoices as $invoice) {
if ($count != 0) {
echo '<tr>';
}
$count++;
echo '<td>' . $invoice . '</td>';
?>
<?php
$psql=$ DB_con->prepare("SELECT * FROM product WHERE prod_id='$invoice'");
$psql->execute();
$resultpro = $psql->fetchall(PDO::FETCH_ASSOC);
foreach($resultpro as $line) { }
?>
<td>
<?php
echo $line[ 'prod_name'];
?>
</td>
<?php
echo '<td hidden="true">' . $date_prod . '</td>';
echo '<td hidden="true">' . $date_sale . '</td>';
$a=count($market);
for($j=0;$j<$a;$j++) {
echo '<td><input type="text" name="'. $market[$j] . ' "class="form-control" maxlength="6" size="4"></td>';
}
}
}
echo '</tbody>';
echo '</table>';
?>
<button type="submit" class="btn btn-default waves-effect waves-light" name="btn-saveforcast" id="btn-saveforcast">Save</button>
</form>
<?php
} catch(PDOException $e) {
echo $e->getMessage();
}
} else {
?>
There are a lot of syntax-errors in your code, but that is not the main issue. The problem is that you have totally misunderstood how HTML and PHP work.
HTML is a language that describes the layout of the page to a web-browser (e.g. Google Chrome). The browser reads the HTML code and then displays the page on your computer screen in the way that it thinks is good. That is it. HTML doesn't do any computations; it is simply a way of describing the layout of the page.
PHP on the other hand, is a back-end scripting language. This means that the PHP script is executed by the server, as opposed to HTML and JavaScript, that your browser takes care of.
I am not sure what you have tried to do here, but I see you have inserted PHP-scripts in table-columns in several places. This does not make any sense at all.
HTML is not a scripting-language so it does not have any order of execution (because it has nothing to execute). It really has nothing to do with PHP at all.
If you want to connect PHP to HTML, meaning that you want data from your server (database) to be displayed on the web page, you need to use JavaScript. JavaScript has the ability to alter HTML, and PHP can echo JavaScript-commands. However, keep in mind: PHP is executed by the server (back-end) and JavaScript is executed by the user (front-end). This means that the JavaScript is really not echoed until the whole PHP-script ends, so you cannot dance back and forth between JavaScript and PHP.
What you should do is something like this:
<form action="foo.php" method="post">
...
</form>
I am not exactly sure what you are trying to achieve, so I cannot help you much more than this. If you provide more information you can get better help.
Upvotes: 2
Reputation: 79
Put the results inside an array than loop trough the array using a for loop. Inside the for loop put the mysql query to insert into the database.
Don't forget to put the $_POST['name'][$i];
like this inside the for loop so it will insert proper.
Hope this helped.
Upvotes: 1