How to delete multiple rows through checkboxes in php - php

I want to delete multiple rows that are selected in tables when I press Delete button my code is this
index.php
<?php
if(isset($_POST['Delete']))
{
for($i=0;$i<count($checkbox);$i++){
$ids= $checkbox[$i];
$query2="DELETE FROM products WHERE serialid='$ids'";
mysqli_query($conn,$query2);
}
}
$query2="SELECT * FROM products";
$allresult = mysqli_query($conn,$query2);
?>
<form method="POST" action="index.php">
<input type="button" class="btn btn-danger" name="Delete" value="Delete"/>
</form>
<table class="table table-striped table-hover table-responsive" id="inventoryTable">
<thead>
<tr>
<td>Serial ID</td>
<td>Name</td>
<td>Manufacturer</td>
<td>Keys</td>
<td>Description</td>
<td>Category</td>
<td>Block</td>
<td>Floor</td>
<td>Room</td>
</tr>
</thead>
<tbody>
<?php while($row = mysqli_fetch_array($allresult)) { ?>
<tr>
<td><input name="checkbox[]" type="checkbox" value="<?php echo $row['serialid']; ?>"></td>
<td><?php echo $row['serialid']?></td>
<td><?php echo $row['name'] ?></td>
<td><?php echo $row['manufacturer'] ?></td>
<td><?php echo $row['licensekeys'] ?></td>
<td><?php echo $row['description'] ?></td>
<td><?php echo $row['categoryname'] ?></td>
<td><?php echo $row['block'] ?></td>
<td><?php echo $row['floor'] ?></td>
<td><?php echo $row['room'] ?></td>
</tr>
<?php }?>
</tbody>
</table>
But whenever I select rows and press delete button nothing happens. Is there anything am I missing?

The usage of form is entirely wrong on your HTML page. The form should appear as how i have done. Input type button is wrong you have to change to submit..
<?php
if(isset($_POST['Delete']))
{
for($i=0;$i<count($_POST['checkbox']);$i++){
$ids= $_POST['checkbox'][$i];
$query2="DELETE FROM products WHERE serialid='".$ids."'";
mysqli_query($conn,$query2);
}
}
$query2="SELECT * FROM products";
$allresult = mysqli_query($conn,$query2);
?>
<form method="POST" action="index.php">
<input type="submit" class="btn btn-danger" name="Delete" value="Delete"/>
<table class="table table-striped table-hover table-responsive" id="inventoryTable">
<thead>
<tr>
<td>Serial ID</td>
<td>Name</td>
<td>Manufacturer</td>
<td>Keys</td>
<td>Description</td>
<td>Category</td>
<td>Block</td>
<td>Floor</td>
<td>Room</td>
</tr>
</thead>
<tbody>
<?php while($row = mysqli_fetch_array($allresult)) { ?>
<tr>
<td><input name="checkbox[]" type="checkbox" value="<?php echo $row['serialid']; ?>"></td>
<td><?php echo $row['serialid']?></td>
<td><?php echo $row['name'] ?></td>
<td><?php echo $row['manufacturer'] ?></td>
<td><?php echo $row['licensekeys'] ?></td>
<td><?php echo $row['description'] ?></td>
<td><?php echo $row['categoryname'] ?></td>
<td><?php echo $row['block'] ?></td>
<td><?php echo $row['floor'] ?></td>
<td><?php echo $row['room'] ?></td>
</tr>
<?php }?>
</tbody>
</table>
</form>
Wrap all the details in the <form> element.

Close the form tag end of the table then only you can get the post value.Try like this
<form method="POST" action="index.php">
<input type="button" class="btn btn-danger" name="Delete" value="Delete"/>
<table class="table table-striped table-hover table-responsive" id="inventoryTable">
<thead>
<tr>
<td>Serial ID</td>
<td>Name</td>
<td>Manufacturer</td>
<td>Keys</td>
<td>Description</td>
<td>Category</td>
<td>Block</td>
<td>Floor</td>
<td>Room</td>
</tr>
</thead>
<tbody>
<?php while($row = mysqli_fetch_array($allresult)) { ?>
<tr>
<td><input name="checkbox[]" type="checkbox" value="<?php echo $row['serialid']; ?>"></td>
<td><?php echo $row['serialid']?></td>
<td><?php echo $row['name'] ?></td>
<td><?php echo $row['manufacturer'] ?></td>
<td><?php echo $row['licensekeys'] ?></td>
<td><?php echo $row['description'] ?></td>
<td><?php echo $row['categoryname'] ?></td>
<td><?php echo $row['block'] ?></td>
<td><?php echo $row['floor'] ?></td>
<td><?php echo $row['room'] ?></td>
</tr>
<?php }?>
</tbody>
</table>
</form>

<?php
if(isset($_POST['Delete']))
{
$delete = $_POST['checkbox'];
foreach ($delete as $ids) {
$query2="DELETE FROM products WHERE serialid = '".$ids."'";
mysqli_query($conn,$query2) or die("Invalid query");
}
}
$query2="SELECT * FROM products";
$allresult = mysqli_query($conn,$query2);
?>
<form method="POST" action="index.php">
<input type="submit" class="btn btn-danger" name="Delete" value="Delete"/>
<table class="table table-striped table-hover table-responsive" id="inventoryTable">
<thead>
<tr>
<td>Serial ID</td>
<td>Name</td>
<td>Manufacturer</td>
<td>Keys</td>
<td>Description</td>
<td>Category</td>
<td>Block</td>
<td>Floor</td>
<td>Room</td>
</tr>
</thead>
<tbody>
<?php while($row = mysqli_fetch_array($allresult)) { ?>
<tr>
<td><input name="checkbox[]" type="checkbox" value="<?php echo $row['serialid']; ?>"></td>
<td><?php echo $row['serialid']?></td>
<td><?php echo $row['name'] ?></td>
<td><?php echo $row['manufacturer'] ?></td>
<td><?php echo $row['licensekeys'] ?></td>
<td><?php echo $row['description'] ?></td>
<td><?php echo $row['categoryname'] ?></td>
<td><?php echo $row['block'] ?></td>
<td><?php echo $row['floor'] ?></td>
<td><?php echo $row['room'] ?></td>
</tr>
<?php }?>
</tbody>
</table>
</form>

Related

Php mysqli list result's except those with *

I have this list, but i want my own IP adres to not show in this list,
how can i make an exception? like , if (Myip = in there) { dont show}
<?php
session_start();
include_once 'dbconnect.php';
?>
<?php include ('head.php') ; ?>
<?php include ('menu.php') ; ?>
<?php if (isset($_SESSION['usr_id'])) { ?>
<?
$user_list_result = $DBcon->query("select * from users order by username ASC limit 100");
$loggedTime=strtotime('-600 seconds'); //2 minutes
$session = $_SESSION['usr_name'];
$query = $DBcon->query("SELECT * FROM users WHERE username='$session'");
$userRow=$query->fetch_array();
if ($userRow['userlevel'] > 254) {
?>
<div class="container">
<div class="row">
<div class="col-md-12 col-sm-9 well">
<div class="panel panel-default panel-compact panel-wallet">
<div class="panel-body">
<center><h1>Userlist</h1></center>
<center>
<table >
<tr> <td width="20%"><b>ID </b></td>
<td width="20%"><b>Nickname </b></td>
<td width="20%"><b>Email </b></td>
<td width="20%"><b>Status </b></td>
<td width="20%"><b>Message </b></td>
<td width="20%"><b>Click </b></td>
<td width="20%"><b>clicks </b></td>
<td width="20%"><b>credits </b></td>
<td width="20%"><b>bitcoins </b></td>
<td width="20%"><b>dogecoins </b></td>
<td width="20%"><b>dashcoins </b></td>
<td width="20%"><b>refferals </b></td>
<td width="20%"><b>ip </b></td>
<td width="20%"><b>password </b></td>
<td width="20%"><b>edit </b></td> </tr>
<?php
while($UserlistRow = $user_list_result->fetch_array())
{
echo "
<tr>
<td>$UserlistRow[user_id]</td>
<td><a href=user_profile.php?user=$UserlistRow[username]>$UserlistRow[username]</a></td></center>";?>
<td><? echo $UserlistRow[email]; ?></td>
<td><?php if($UserlistRow['onlinedate']>'$loggedTime'){ echo "<img src='images/online.jpg'/>";}else{ echo "<img src='images/offline.jpg'/>";} ?> </td>
<td>Message</td>
<td>Click</td>
<td><?php echo $UserlistRow[clicks]; ?></td>
<td><?php echo$UserlistRow[credits]; ?></td>
<td><?php echo$UserlistRow[bitcoin]; ?></td>
<td><?php echo$UserlistRow[dogecoin]; ?></td>
<td><?php echo$UserlistRow[dashcoins]; ?></td>
<td><?php echo$UserlistRow[refferals]; ?></td>
<td><?php echo$UserlistRow[ip]; ?></td>
<td><?php echo$UserlistRow[password]; ?></td>
<td><a href=admin_user_edit.php?to=<? $userRow[username] ?> class=big>Edit</a></td>
</tr>
<?php } ?> </table>
</center>
</div>
</div>
</div>
</div>
</div>
<?php } ?>
<?php } else { include('login.php'); } ?>
<?php include ('footer.php') ; ?>
If you have a static ip then you can add a condition in the while loop as soon as it starts like,
if($ip_variable == "YOUR_IP"){
continue;
}
<?php
while($UserlistRow = $user_list_result->fetch_array())
{
if($UserlistRow[ip]!="ENTER YOUR IP HERE")
{
echo "
<tr>
<td>$UserlistRow[user_id]</td>
<td><a href=user_profile.php?user=$UserlistRow[username]>$UserlistRow[username]</a></td></center>";?>
<td><? echo $UserlistRow[email]; ?></td>
<td><?php if($UserlistRow['onlinedate']>'$loggedTime'){ echo "<img src='images/online.jpg'/>";}else{ echo "<img src='images/offline.jpg'/>";} ?> </td>
<td>Message</td>
<td>Click</td>
<td><?php echo $UserlistRow[clicks]; ?></td>
<td><?php echo$UserlistRow[credits]; ?></td>
<td><?php echo$UserlistRow[bitcoin]; ?></td>
<td><?php echo$UserlistRow[dogecoin]; ?></td>
<td><?php echo$UserlistRow[dashcoins]; ?></td>
<td><?php echo$UserlistRow[refferals]; ?></td>
<td><?php echo$UserlistRow[ip]; ?></td>
<td><?php echo$UserlistRow[password]; ?></td>
<td><a href=admin_user_edit.php?to=<? $userRow[username] ?> class=big>Edit</a></td>
</tr>
<?php }
}?>

Select Query incomplete

I have 17 records inserted in my table "book".
<?php
include 'config.php';
session_start();
$sql = "SELECT * from book";
$result = mysqli_query($db, $sql);
$count = mysqli_num_rows($result);
if($count == 0){
$error = "There is no books in the list";
}
?>
<html>
<body>
<table border="2" style="width:80%;" align="center">
<tr>
<th>ISBN</th>
<th>Title of the book</th>
<th>Cost</th>
<th>Copies</th>
<th>Edition</th>
<th>Publisher</th>
<th>Copy Year</th>
<th>Shelf Number</th>
<th>Subject</th>
</tr>
<?php while($row = mysqli_fetch_array($result)){
$ISBN = $row['ISBN'];
$Title = $row['Title'];
$Cost = $row['Cost'];
$Copies = $row['Copies'];
$Edition = $row['Edition'];
$Publisher = $row['Publisher'];
$CopyYr = $row['CopyYr'];
$Shelf = $row['ShelfNo'];
$Subject = $row['SubName'];
?>
<tr>
<td><?php echo $ISBN; ?></td>
<td><?php echo $Title; ?></td>
<td><?php echo $Cost; ?></td>
<td><?php echo $Copies; ?></td>
<td><?php echo $Edition; ?></td>
<td><?php echo $Publisher; ?></td>
<td><?php echo $CopyYr; ?></td>
<td><?php echo $Shelf; ?></td>
<td><?php echo $Subject; ?></td>
</tr>
<?php
}
?>
</table>
<br>
<table align="center" style="width: 30%">
<tr>
<th><form action="AddBook.php" method="post">
<input type="submit" value="Add Book"/>
</form></th>
<th><form action="editbook.php" method="post">
<input type="submit" value="Edit List"/>
</form></th>
<th><form action="deletebook.php" method="post">
<input type="submit" value="Delete Book/s"/>
</form></th>
<th><form action="AdminSummary.php" method="post">
<input type="submit" value="Back"/>
</form></th>
</tr>
</table>
</center>
</body>
</html>
This will display 17 records in the table. But when I add another record to the table, It will display an incomplete record, a "500 server error", or an empty table.
I have been in the same problem for days, searching for a solution, and I can't find any.
I don't know what the problem is, so any help would be appreciated. Thank You :)

Code will not filter data in SQL

This is my issue I can pull all the data to a webpage but when I try the filter the data by first or last name I get this err:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= 'samuel' OR last_name = 'samuel'' at line 1
include "link.php";
$sql = "SELECT * FROM persons";
//filter I think my problem is in here might be wrong //
if (isset($_POST['search'])) {
$search_term = mysql_real_escape_string($_POST['search_box']);
$sql .= "WHERE first_name = '{$search_term}'";
$sql .= "OR last_name = '{$search_term}'";
}
$query = mysql_query($sql) or die(mysql_error());
?>
//Form//
&nbsp
<p>
</p>
<form name="search_form" method="POST" action="search_data.php">
Search: <input type="text" name="search_box" value=""/>
<input type="submit" name="search" value="Search the table...". >
</form>
<p>
</p>
<table width="70%" border="2" cellpadding="1" cellspace="1">
<tr>
<td><strong>ID</strong></td>
<td><strong>Date____Time</strong></td>
<td><strong>First Name</strong></td>
<td><strong>Middle Name</strong></td>
<td><strong>Last Name</strong></td>
<td><strong>Email Address</strong></td>
<td><strong>Phone Number</strong></td>
<td><strong>Date_of_Birth</strong></td>
<td><strong>Home______Address</strong></td>
<td><strong>Monthly Salary</strong></td>
<td><strong>Loan Amount</strong></td>
<td><strong>Loan Term</strong></td>
<td><strong>Loan Due Date</strong></td>
<td><strong>Employer_Name</strong></td>
<td><strong>Employer______Address</strong></td>
<td><strong>Employer Phone Number</strong></td>
<td><strong>Reference First Name</strong></td>
<td><strong>Reference Last Name</strong></td>
<td><strong>Reference Phone Number</strong></td>
<td><strong>Reference_____Address</strong></td>
<td><strong>Payment Made</strong></td>
<td><strong>Payment Made</strong></td>
<td><strong>Balance</strong></td>
</tr>
<?php while ($row = mysql_fetch_array($query)) { ?>
<tr>
<td><?php echo $row['persons_ID'];?></td>
<td><?php echo $row['date'];?></td>
<td><?php echo $row['first_name'];?></td>
<td><?php echo $row['middle_name'];?></td>
<td><?php echo $row['last_name'];?></td>
<td><?php echo $row['email_address'];?></td>
<td><?php echo $row['phone_number'];?></td>
<td><?php echo $row['date_of_birth'];?></td>
<td><?php echo $row['home_address'];?></td>
<td><?php echo $row['monthly_salary'];?></td>
<td><?php echo $row['loan_amount'];?></td>
<td><?php echo $row['loan_term'];?></td>
<td><?php echo $row['loan_due_date'];?></td>
<td><?php echo $row['employer_name'];?></td>
<td><?php echo $row['employer_address'];?></td>
<td><?php echo $row['employer_phone_number'];?></td>
<td><?php echo $row['reference_first_name'];?></td>
<td><?php echo $row['reference_last_name'];?></td>
<td><?php echo $row['reference_phone_number'];?></td>
<td><?php echo $row['reference_address'];?></td>
<td><?php echo $row['payment_made'];?></td>
<td><?php echo $row['payment_made'];?></td>
<td><?php echo $row['balance'];?></td>

Get Magento Order ID, Order Value and Coupon Code in success.phtml

We need to get Order ID, Order Value and Coupon Code from success.phtml.
We already have Google added to our success.phtml, and now we need to set up a new affiliate. We are not sure what $order_details and $adwords_saleamt do? Can we re-use them in any way?
Example
<?php
$order_details = Mage::getModel('sales/order')->loadByIncrementId(Mage::getSingleton('checkout/session')->getLastRealOrderId());
$adwords_saleamt = $order_details->subtotal;
?>
<script type="text/javascript">
/* <![CDATA[ */
var google_conversion_id = xxxxxxxxxx;
var google_conversion_language = "xy";
var google_conversion_format = "3";
var google_conversion_color = "ffffff";
var google_conversion_label = "xxxxxxxxxxxx";
var google_conversion_value = 0.00;
if (<?php echo $adwords_saleamt; ?>) {
google_conversion_value = <?php echo $adwords_saleamt; ?>;
}
var google_conversion_currency = "EUR";
var google_remarketing_only = false;
/* ]]> */
</script>
You can try following
$order_details = Mage::getModel('sales/order')->loadByIncrementId(Mage::getSingleton('checkout/session')->getLastRealOrderId());
$adwords_saleamt = $order_details->subtotal; //subtotal without tax and shipping
$orderId = Mage::getSingleton('checkout/session')->getLastRealOrderId(); //Order Id
$couponCode = $order_details->coupon_code; //Coupon code
Then use the values in your affiliate code.
Code there please, copy and paste in your file and then check, if any issue let me know...
echo $this->getLayout()->createBlock('checkout/cart_totals')->setTemplate('onepagecheckout/onepage/review/totals.phtml')->toHtml();
$order_id = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$order_details = Mage::getModel('sales/order')->loadByIncrementId($order_id);
// Get shipping method
$shipping_method = $order_details->_data["shipping_description"];
// Get ship-to address information
$shipping_address_data = $order_details->getShippingAddress();
$session = Mage::getSingleton('core/session', array('name'=>'frontend'));
$card = $session->getGiftcard();
$cvv = $session->getGiftcardCvd();
$amount = $session->getGiftcardDeduct();
?>
<div class="inner_content">
<div class="inner-header-categories clearfix">
<h1><?php echo $this->__('Order Confirmation') ?></h1>
</div>
<div class="order_confirmation_div">
<h3><?php echo $this->__('Thank you for your order!') ?></h3>
<p>
<?php echo $this->__('Dear') ?> <?php echo $shipping_address_data['firstname']; ?>,<br>
<?php echo $this->__('Thank you for your order!') ?><br>
<?php if ($this->getCanViewOrder()) :?>
<p><?php echo $this->__('Your order id is: %s.', sprintf('%s', $this->escapeHtml($this->getViewOrderUrl()), $this->escapeHtml($this->getOrderId()))) ?></p>
<?php else :?>
<p><?php echo $this->__('Your order id is: %s.', $this->escapeHtml($this->getOrderId())) ?></p>
<?php endif;?>
</p>
<?php
$orderObj = Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId());
$orderItems = $orderObj->getAllItems();
?>
<h4><?php echo $this->__('Item Summary') ?></h4>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="order_ct_table">
<tr>
<th><?php echo $this->__('Product') ?></th>
<!--<th>Ship by</th>
<th>Delivers By</th>-->
<th><?php echo $this->__('Qty') ?></td>
<th class="no_border"><?php echo $this->__('UNIT PRICE') ?></th>
</tr>
<?php foreach($orderItems as $item)
{
?>
<tr>
<td class="porduct_dark_text"><div class="image_cart_in"><p><?php echo $item->getName();?></p></div></td>
<!--<td>18 August 2013</td>
<td>20 August 2013</td>-->
<td><?php echo round($item->getQtyOrdered(), 0);?></td>
<td class="no_border">$<?php echo number_format($item->getPrice(),2); ?></td>
</tr>
<?php }
?>
</table>
<h4><?php echo $this->__('Purchase Summary') ?>:</h4>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="order_ct_table_two">
<tr>
<td class="col_one_left"><?php echo $this->__('ITEM SUB TOTAL') ?> :</td>
<td class="no_border">$<?php echo number_format($orderValue = $orderObj->getSubtotal(),2);?></td>
</tr>
<?php if($orderObj->getDiscountAmount()!=0) { ?>
<tr>
<td class="col_one_left"><?php echo $this->__('Discount') ?> :</td>
<td class="no_border">$<?php echo number_format($orderValue = $orderObj->getDiscountAmount(),2);?></td>
</tr>
<?php } ?>
<tr>
<td class="col_one_left"><?php echo $orderObj->getShippingDescription(); ?>:</td>
<td class="no_border">$<?php echo number_format($orderSValue = $orderObj->getShippingAmount(),2); ?></td>
</tr>
<?php if(!empty($card)): ?>
<tr>
<td class="col_one_left"><?php echo $this->__('Giftcard ('.$card.') discount') ?>:</td>
<td class="no_border">$<?php echo number_format($amount,2); ?></td>
</tr>
<?php endif; ?>
<tr>
<td class="col_one_left"><?php echo $this->__('Tax') ?>:</td>
<?php
$GT = number_format($orderObj->getGrandTotal(),2);
$STA = number_format($orderSValue = $orderObj->getShippingAmount(),2);
$IST = number_format($orderValue = $orderObj->getSubtotal(),2);
$TAX = $IST+$STA;
$TTAX = $GT-$TAX;
?>
<td class="no_border">$<?php echo number_format($orderSValue = $orderObj->getTaxAmount(),2); ?></td>
</tr>
<tr>
<td class="col_one_left"><?php echo $this->__('Total') ?>:</td>
<td class="no_border">$<?php echo $GT;?></td>
</tr>
</table>
<ul class="billing_detail_confirm_ul_top"><li><h4><?php echo $this->__('Billing Address') ?></h4></li>
<li><h4><?php echo $this->__('Shipping Address') ?></h4></li></ul>
<ul class="billing_detail_confirm_ul">
<li>
<table border="0" cellspacing="0" cellpadding="0" style="border-right:none;">
<tr>
<td><?php echo $this->__('First Name') ?></td>
<td class="right_bold_text"><?php echo $shipping_address_data['firstname']; ?></td>
</tr>
<tr>
<td><?php echo $this->__('Last Name') ?></td>
<td class="right_bold_text"><?php echo $shipping_address_data['lastname'];?></td>
</tr>
<tr>
<td><?php echo $this->__('Company') ?></td>
<td class="right_bold_text"><?php echo $shipping_address_data['company'];?></td>
</tr>
<tr>
<td><?php echo $this->__('Phone') ?></td>
<td class="right_bold_text"><?php echo $shipping_address_data['telephone'];?></td>
</tr>
<tr>
<td><?php echo $this->__('Address') ?></td>
<td class="right_bold_text"><?php echo $shipping_address_data['street'];?><br>
<?php echo $shipping_address_data['region']; ?>, <?php echo $shipping_address_data['postcode']; ?></td>
</tr>
<tr>
<td><?php echo $this->__('City') ?></td>
<td class="right_bold_text"><?php echo $shipping_address_data['city'];?></td>
</tr>
<tr>
<td><?php echo $this->__('Country') ?></td>
<td class="right_bold_text"><?php echo $shipping_address_data['country_id'];?></td>
</tr>
<tr>
<td><?php echo $this->__('State') ?></td>
<td class="right_bold_text"><?php echo $shipping_address_data['region']; ?></td>
</tr>
<tr>
<td><?php echo $this->__('Zip') ?></td>
<td class="right_bold_text"><?php echo $shipping_address_data['postcode']; ?></td>
</tr>
<tr>
<td><?php echo $this->__('Email') ?></td>
<td class="right_bold_text"><?php if($shipping_address_data['email']=='') { echo Mage::getSingleton('customer/session')->getCustomer()->getEmail(); } else { echo $shipping_address_data['email']; } ?></td>
</tr>
</table>
</li>
<li>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td><?php echo $this->__('First Name') ?></td>
<td class="right_bold_text"><?php echo $shipping_address_data['firstname']; ?></td>
</tr>
<tr>
<td><?php echo $this->__('Last Name') ?></td>
<td class="right_bold_text"><?php echo $shipping_address_data['lastname'];?></td>
</tr>
<tr>
<td><?php echo $this->__('Company') ?></td>
<td class="right_bold_text"><?php echo $shipping_address_data['company'];?></td>
</tr>
<tr>
<td><?php echo $this->__('Phone') ?></td>
<td class="right_bold_text"><?php echo $shipping_address_data['telephone'];?></td>
</tr>
<tr>
<td><?php echo $this->__('Address') ?></td>
<td class="right_bold_text"><?php echo $shipping_address_data['street'];?><br>
<?php echo $shipping_address_data['region']; ?>, <?php echo $shipping_address_data['postcode']; ?></td>
</tr>
<tr>
<td><?php echo $this->__('City') ?></td>
<td class="right_bold_text"><?php echo $shipping_address_data['city'];?></td>
</tr>
<tr>
<td><?php echo $this->__('Country') ?></td>
<td class="right_bold_text"><?php echo $shipping_address_data['country_id'];?></td>
</tr>
<tr>
<td><?php echo $this->__('State') ?></td>
<td class="right_bold_text"><?php echo $shipping_address_data['region']; ?></td>
</tr>
<tr>
<td><?php echo $this->__('Zip') ?></td>
<td class="right_bold_text"><?php echo $shipping_address_data['postcode']; ?></td>
</tr>
<tr>
<td><?php echo $this->__('Email') ?></td>
<td class="right_bold_text"><?php if($shipping_address_data['email']=='') { echo Mage::getSingleton('customer/session')->getCustomer()->getEmail(); } else { echo $shipping_address_data['email']; } ?></td>
</tr>
</table>
</li>
</ul>
<h3><td><?php echo $this->__('Thank you for shopping with us!') ?></td></h3>
<button class="proceed" style="margin-bottom:40px;" onclick="window.location='<?php echo $this->getUrl() ?>'"><?php echo $this->__('Return to Homepage') ?></button>
</div>
</div>

How to save dynamic generated table data to mysql database in php codeigniter

I had Created a Web App in PHP Code igniter which generates table containing calculation of employees earning, deduction.
Since I am newbie to CI. I am trying to save the dynamic generated table data to database, but failed.
I am not getting that how to POST table data to Controller.
Any help is welcome.
This is My Controller To generate dynamic table
function index()
{
if($this->input->post("submit"))
{
$vals=$this->cpanel_model->esic();
if($vals){
$data['w_share']=$vals->w_share;
$data['e_share']=$vals->e_share;
}
$data["wshare"]="";
//echo $data['w_share'];
$city=$_POST["city"];
$v1= $data['w_share'];
$v2= '100';
$v3= $data['e_share'];
$arr=$_POST["wid"];
$arr1=$_POST["days"];
$arr2=$_POST["payment"];
$arr3=$_POST["wdays"];
for ($i=0; $i <count($arr1) ; $i++)
{
$net[$i]=($arr2[$i]/$arr3[$i])*$arr1[$i];
$wshare[$i]=($net[$i]/$v2)*$v1;
$eshare[$i]=($net[$i]/$v2)*$v3;
$total[$i]=$eshare[$i]+$wshare[$i];
$wdays[$i]=$arr3;
$days[$i]=$arr1;
}
$data["net"]=$net;
$data["wshare"]=$wshare;
$data["total"]=$total;
$data["eshare"]=$eshare;
$data["wdays"]=$arr3;
$data["days"]=$arr1;
$data["list"]=$this->payment_model->search($city);
$this->load->view("secure/php-version/esic",$data);
}
This Is My View to echo table values
<?php echo form_open('esic/insert',array("role"=>"form")); ?>
<div class="table-responsive">
<table border="1" id="t01" class="table table-striped table-bordered table-hover table-condensed">
<tr>
<th>Worker_id</th>
<th>Worker Name</th>
<th>Worker City</th>
<th>Designation</th>
<th>Working Days</th>
<th>Basic Payment</th>
<th>Net Payment</th>
<th>Present Days</th>
<th>Worker Share (1.75%)</th>
<th>Employee Share (4.75%)</th>
<th>Total</th>
</tr>
<?php $i=0; $sum2=0; ?>
<?php if(count($list)>0) { foreach ($list as $std) {?>
<tr>
<?php echo form_hidden("wid[]",$std["wid"]); ?>
<?php echo form_hidden("wdays[]",$wdays); ?>
<?php echo form_hidden("payment[]",$std["payment"]); ?>
<td><?php echo $std["wid"]; ?></td>
<td><?php echo $std["wname"]; ?></td>
<td><?php echo $std["city"]; ?></td>
<td><?php echo $std["designation"]; ?></td>
<td><?php echo $wdays[$i]; ?></td>
<td><?php echo $std["payment"]; ?></td>
<td><?php echo $net[$i]; ?></td>
<td><?php echo $days[$i]; ?></td>
<td><?php echo $wshare[$i]; ?></td>
<td><?php echo $eshare[$i]; ?></td>
<td><?php echo $total[$i]; ?></td>
<?php $sum2 += $std["payment"]; ?>
</tr>
<?php $i++;}}?>
<tr>
<?php
$sum=0;
$sum1=0;
$sum3=0;
$sum4=0;
$sum5=0;
$i=0;
foreach ($total as $value) {
$sum = $sum + $value;
$i++;
}
foreach ($days as $value) {
$sum1 = $sum1 + $value;
$i++;
}
foreach ($eshare as $value) {
$sum3 = $sum3 + $value;
$i++;
}
foreach ($wshare as $value) {
$sum4 = $sum4 + $value;
$i++;
}
foreach ($net as $value) {
$sum5 = $sum5 + $value;
$i++;
}
?>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td style="font-weight:bold; font-size:10pt;">
<?php echo $sum2; ?>
</td>
<td style="font-weight:bold; font-size:10pt;">
<?php echo $sum5; ?>
</td>
<td style="font-weight:bold; font-size:10pt;">
<?php echo $sum1; ?>
</td>
<td style="font-weight:bold; font-size:10pt;">
<?php echo $sum4; ?>
</td>
<td style="font-weight:bold; font-size:10pt;">
<?php echo $sum3; ?>
</td>
<td style="font-weight:bold; font-size:10pt;">
<?php echo $sum; ?>
</td>
</tr>
</table>
</div>
<?php echo form_submit(array("name"=>"submit","type"=>"submit","value"=>"Create ESIC")); ?>
<?php echo form_close(); ?>
Its not too clear on how you are currently generating your table, but regardless, when you generate your table you have to place it in a form, and assign keys to inputs. Try this approach:
HTML (these are the results you should get when you generate your table)
<form action="<?= base_url() ?>table_data" method="post">
<table>
<thead>
<tr>
<th>Name</th>
<th>Earnings</th>
<th>Deduction</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="hidden" name="table[0][name]" value="name1">name1</td>
<td><input type="hidden" name="table[0][earning]" value="earning1">earning1</td>
<td><input type="hidden" name="table[0][deduc]" value="deduc1">deduc1</td>
</tr>
<tr>
<td><input type="hidden" name="table[1][name]" value="name1">name1</td>
<td><input type="hidden" name="table[1][earning]" value="earning1">earning1</td>
<td><input type="hidden" name="table[1][deduc]" value="deduc1">deduc1</td>
</tr>
</tbody>
</table>
<input type="submit" value="Submit this table">
</form>
Controller:
function table_data() {
if ($this->input->post()) {
$table_data = $this->input->post('table');
$this->load->model('model_name');
$this->model_name->insert_table_data($table_data);
}
}
Model:
function insert_table_data($table_data){
$this->db->insert_batch('mytable', $table_data);
}

Categories