I'm trying to delete the selected rows from the list I have in mysql so I won't have to delete the rows in my table one by one. When I press delete, my page refreshes without any php error but I don't get the result desired either. here is what I have:
<?php $product_set = find_all_products(); ?>
<body>
<form action="manage_products.php" method="delete">
<div class="page">
<article>
<div id="page">
<?php echo message(); ?>
<h2>Manage Products</h2>
<table>
<tr>
<th style="text-align: left; width: 125px;">Location</th>
<th style="text-align: left; width: 250px;">URL to the Product</th>
<th style="text-align: left; width: 100px;">First Name</th>
<th style="text-align: left; width: 100px;">Last Name</th>
<th style="text-align: left; width: 100px;">Size</th>
<th style="text-align: left; width: 100px;">Status</th>
<th style="text-align: left; width: 100px;">Checked</th>
<th colspan="2" style="text-align: left;">Actions</th>
</tr>
<?php while($product = mysqli_fetch_assoc($product_set)){ ?>
<tr>
<td><?php echo htmlentities($product["location"]);?></td>
<td><?php echo htmlentities($product["productURL"]); ?></td>
<td><?php echo htmlentities($product["fname"]); ?></td>
<td><?php echo htmlentities($product["lname"]); ?></td>
<td><?php echo htmlentities($product["size"]); ?></td>
<td><?php echo htmlentities($product["status"]); ?></td>
<td><input name="checkbox" type="checkbox" id="checkbox[]" value="<?php echo $product['id']; ?>"></td>
<td>Edit Order</td>
<td>Delete Order</td>
</tr>
<?php } ?>
</table>
<?php
// Check if delete button active, start this
if(isset($_GET['delete']))
{
$checkbox = $_GET['checkbox'];
for($i=0;$i<count($checkbox);$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM product WHERE link_id='$del_id'";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if($result){
$_SESSION["message"] = "Product deletion failed.";
redirect_to("created_products.php"); }
}
//mysqli_close($dbc);
?>
<br />
</div>
</div>
<br>
<input type="submit" name="submit" value="Delete" onclick="return val();"/>
</form>
<div class="clear-all"></div>
</article>
</div>
</body>
Also place all the code which is for deleting the rows in the starting of your file. Thank you #Traveller for this.
Instead of for loop, use WHERE IN
for($i=0;$i<count($checkbox);$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM product WHERE link_id='$del_id'";
$result = mysql_query($sql);
}
Replace that with
$ids = implode(",", $_POST['checkbox']);
$sql = "DELETE FROM product WHERE link_id in ($ids)";
Related
I want to display the invoice bill for each customer when admin clicks on view bill button for that customer. Also I want to display all the newspaper details in a single invoice when customer name is same.I have included the relevant code and database snapshots.
c_details.php
<?php
session_start();
include_once("connect.php");
$query="SELECT * FROM sub_details";
$result=mysqli_query($conn,$query);
?>
<table align="center">
<h3 style="color: black; font-weight: bold; text-align: center;">
Subscription details</h3><br>
<tr>
<th style="text-align: center;">Id</th>
<th style="text-align: center;">Name</th>
<th style="text-align: center; width: 900px">Newspaper</th>
<th style="text-align: center;">Duration</th>
<th style="text-align: center;">Price</th>
<th style="text-align: center; width: 800px">Subscription date</th>
<th style="text-align: center;">Remaining Days</th>
<th style="text-align: center;">Bill</th>
</tr>
<?php while($rows=mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php echo $rows['id']; ?></td>
<td><?php echo $rows['name']; ?></td>
<td><?php echo $rows['newspaper']; ?></td>
<td><?php echo $rows['duration']; ?></td>
<td><?php echo $rows['price']; ?></td>
<td><?php echo $rows['sdate']; ?></td>
<?php echo var_dump($result->fetch_all(MYSQLI_ASSOC));
$date1=date_create();
$date2=date_create($rows['edate']);
$interval=date_diff($date2,$date1)->format("%a days");
?>
<td><?php echo $interval; ?></td>
<td>
<form action="invoice.php" method="GET">
<?php $invoiceId = $rows['name']; ?>
<button type="submit" onclick= "window.open('invoice.php?name= '<?php echo
$rows['name']; ?>)" class="btn btn-primary btn-lg" value="submit" >
View bill</button></td>
</form>
</tr>
<?php } ?>
</table>
invoice.php
<?php
include_once("connect.php");
$invoiceId = isset($_GET['name']) ? $_GET['name'] : null;
if($invoiceId) {
$query = "SELECT * FROM sub_details WHERE name = ? limit 1";
$result = mysqli_query($conn,$query);
?>
<?php while($rows=mysqli_fetch_assoc($result)) {
?>
<form style="text-align: left;margin-left: 30px" class="register-form" id="register-form">
<div class="form-group"> Bill no: <input type="text" name="id" value="<?php echo $rows['id']; ?>"></div><br>
<div class="form-group">Name: <input type="text" name="name" value="<?php echo $rows['name']; ?>"></div><br>
<div class="form-group">Address: <input type="text" name="address" value="<?php echo $rows['address']; ?>"></div><br><br>
</form>
</th>
</tr>
<td>
<table cellpadding="5px" cellspacing="6px" style="width: 75%; border-radius:20px;">
<tr>
<th>Newspaper</th>
<th >Duration</th>
<th >Price</th>
</tr>
<tr>
<td ><?php echo $rows['newspaper']; ?></td>
<td><?php echo $rows['duration']; ?></td>
<td><?php echo $rows['price']; ?></td>
</tr>
<tr>
<td>DELIVERY CHARGES</td>
<td colspan="3" style="padding-right:60px;text-align: right;">50</td>
</tr>
<tr>
<th>Total</th>
<?php
$t_price=$rows['price']+ 50; ?>
<th colspan="3" style="text-align: right;padding-right: 55px"><?php echo $t_price; ?></th>
</tr>
<tr>
<th colspan="4" >
<br><br><br><br><br><br><br>
</th>
</tr>
</table>
</td>
<tr>
<th colspan="4" style="border-top-color: #ffff4d">
<p style="text-align: left;">Note: Clients are requested to pay the bill before 5th of every month.</p>
</th>
</tr>
<?php } ?>
</table>
Database screenshots
First, you should pass the invoice ID for each record in c_details.php so that you can identify them later:
<button type="submit" onclick= "window.open('invoice.php?id='<?php echo $rows['id']; ?>)" class="btn btn-primary btn-lg" value="submit" >View bill</button>
It will produce URLs like invoice.php?id=<id>, where <id> is the ID for each record in the database. So, for example, a record with the ID 102 will be invoice.php?id=102.
On invoice.php, you can retrieve the ID with $_GET['id']. You should adjust your query to fetch the data for the given invoice:
$invoiceId = isset($_GET['id']) ? $_GET['id'] : null;
if($invoiceId) {
$query = $conn->prepare("SELECT * FROM sub_details, signup_c WHERE id = ? limit 1";
$query->bind_param('s', $invoiceId);
$query->execute();
$result = $query->get_result();
}
If you're using PHP 7.0 or later, you can simplify your code using the null coalescing operator:
$invoiceId = $_GET['id'] ?? null;
I strongly recommend you to learn about how to prevent SQL injections in PHP.
I have a problem calculating the sum of the data from the database and from user input, can Y'all help me?
this is the PHP code
1st value (stok di database) is data from database, and 2nd value (stok fisik) is data from user input, and selisih is 2nd value - 1st value.
<div class="box-content nopadding" style="overflow: auto;">
<table width="400px" class="table table-hover table-nomargin table-bordered">
<thead>
<tr>
<th style="text-align: center; vertical-align:middle;" rowspan="2">No</th>
<th style="text-align: center; vertical-align:middle;" rowspan="2">Nama Barang</th>
<th style="text-align: center; vertical-align:middle;" colspan="2">Stok</th>
<th style="text-align: center; vertical-align:middle;" rowspan="2">Selisih</th>
</tr>
<tr>
<th style="text-align: center; vertical-align:middle;">Stok di Database</th>
<th style="text-align: center; vertical-align:middle;">Stok Fisik</th>
</tr>
</thead>
<tbody>
<?php
$data = $db->query("select id, nama_barang, stock from tbl_atk group by nama_barang");
for ($i = 0; $i < count($data); $i++) {
$no = $i + 1;
if ($no % 2 ==0) $bg ='#FBFBFB';
else $bg = '#FFFFFF';
// $total = 'result' - $data[$i]['stock'];
$tot_masuk = $tot_masuk + "stock_op";
$tot_keluar = $tot_keluar + $data[$i]['stock'];
$grand_tot = $tot_masuk - $tot_keluar;
?>
<tr>
<td style="text-align: center; vertical-align:middle;"><?php echo $no; ?></td>
<td><?php echo $data[$i]['nama_barang']?></td>
<td style="text-align: right;" class="so"><?php echo $data[$i]['stock']; ?></td>
<td><input type="number" class="form-control so" id="stock_op" name="stock_op" style="text-align: left"></td>
<td style="text-align: right; font-weight:bold;"><output class="result"></output></td>
</tr>
<?php
}
?>
<script src="../js/jquery.min.js"></script>
<script>
$(document).on('input','.so',function(){
var totalSum = 0;
var currentRow = $(this).closest('tr');
currentRow.find('.so').each(function(){
var inputVal = ($(this).is('input')) ? $(this).val() : parseInt($(this).html());
console.log(inputVal);
if($.isNumeric(inputVal)){
totalSum += parseFloat(inputVal);
}
});
currentRow.find('.result').val(totalSum);
});
</script>
<tr style="background-color:#eaeaea; font-weight:bold;">
<td style="text-align: center; vertical-align:middle;"></td>
<td style="text-align: right;">Selisih</td>
<td style="text-align: right;"><?php echo $tot_keluar; ?></td>
<td style="text-align: right;"><?php echo $tot_masuk; ?></td>
<td style="text-align: right;"><?php echo $grand_tot; ?></td>
</tr>
</tbody>
</table>
</div>
and this is the User Interface
Change the .so to so in the below input fields.
<td style="text-align: right;" class="form-control .so"><?php echo $data[$i]['stock']; ?></td>
<td><input type="number" class="form-control .so" id="stock_op" name="stock_op" style="text-align: left"></td>
Change id result to class result in below line
<td style="text-align: right; font-weight:bold;"><output id="result"></output></td>
New Update
Give a specific class to your stok fisik cell
<tr style="background-color:#eaeaea; font-weight:bold;">
<td style="text-align: center; vertical-align:middle;"></td>
<td style="text-align: right;">Selisih</td>
<td style="text-align: right;"><?php echo $tot_keluar; ?></td>
<td class="grandResult" style="text-align: right;"><?php echo $tot_masuk; ?></td>
<td style="text-align: right;"><?php echo $grand_tot; ?></td>
</tr>
change your jquery code to this
$(document).on('input','.so',function(){
var grandSum = 0;
var totalSum = 0;
var currentRow = $(this).closest('tr');
currentRow.find('.so').each(function(){
var inputVal = ($(this).is('input')) ? $(this).val() : parseInt($(this).html());
if($.isNumeric(inputVal)){
totalSum += parseFloat(inputVal);
}
});
currentRow.find('.result').val(totalSum);
$('input.so').each(function(){
var cVal = ($(this).is('input')) ? $(this).val() : parseInt($(this).html());
if($.isNumeric(cVal)){
grandSum += parseFloat(cVal);
}
});
$('.grandResult').val(grandSum);
});
Hello Fauzio, I have created a sample table with 3 columns,
1. t_id (ie. id )
2. t_name ( ie name barang)
3. t_value (ie stock di database)
I created a table and displayed the values of each rows in the table. all values from column t_value is added in java script array. then as user enters the value for that particular row.
On click of the button , the value for that particular row is picked from db_column and user column and the result is printed in the result column
<?php
$sql = "SELECT t_id,t_name, t_value FROM test_table";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
$main_array = array();
$count=0;
while($row = $result->fetch_assoc()) {?>
<tr>
<td class="no">
<?php echo $count;?>
</td>
<td class="name">
<?php echo $row["t_name"]."<br>";?>
</td>
<td class="db_value">
<?php
array_push($main_array,$row["t_value"]);
echo $row["t_value"]."<br>";?>
</td>
<td class="user_value">
<form method="post">
<input type="number" value="">
<button id="cal" type="button" class="<?php echo $count;?>"> Calculate </button>
</form>
</td>
<td class="result">
<input type="tel" name="" value="" id="result_<?php echo $count;?>">
</td>
</tr>
<?php $count++;}?>
</tbody>
</table>
</div>
<script type="text/javascript">
jQuery(document).ready(function() {
var main_array = new Array();
<?php foreach($main_array as $key => $val){ ?>
main_array.push('<?php echo $val; ?>');
<?php } ?>
jQuery('#cal').on('click', function() {
id = jQuery(this).attr('class');
value_1st = main_array[id];
value_2nd = jQuery(jQuery(this).closest('.user_value')).find('input[type="number"]').val();
result = value_1st + value_2nd;
input_field = jQuery(jQuery(jQuery(this).closest('.user_value')).next()).find('#result_' + id);
input_field.val(result);
});
});
</script>
I want to pass a specific row from database to another database by clicking the add button. my dbconnection is included at the top of it.
Here is the my table
Here is my Code
<table style="margin-left: 28%;">
<tr>
<th><h2>Product Code</h2></th>
<th><h2>Products and Services</h2></th>
<th><h2>Amount</h2></th>
<th><h2>Action</h2></th>
</tr>
<tr>
<?php
$select_query = "SELECT * FROM products_and_services";
$select_result = mysqli_query($connection,$select_query);
?>
<?php while ($rows = mysqli_fetch_row($select_result)):;
$id = $rows[0];
$prod_code = $rows[1];
$service = $rows[2];
$amount = $rows[3];
?>
<td style="text-align: center;"><?php echo $rows[1]; ?></td>
<td style="text-align: center;"><?php echo $rows[2]; ?></td>
<td style="text-align: center;"><?php echo $rows[3]; ?></td>
<td style="text-align: center;"><a href="" name="add">Add</td>
<?php
if(isset($_POST['add'])){
$sql = "INSERT INTO temp_table (Product_name,Product_price,Product_ID)
VALUES ('$service','$amount','$id')";
mysqli_query($db,$sql);
}
?>
</tr>
<?php endwhile ?>
How to split my page into 4 sections with data from database?
I'm displaying data from the database and they appear one by one on the table. And I want to split them into 4 parts and each of them will show another result from the database. In addition, I would like to make line in the middle, which will keep them separated.
That's my code, to show results from DB:
EDIT: Ok, I edited like u said, and now page is splitted on 2 section, but I got on them the same results. How to change this, that on the left side will be shown one record, and on the right side another.
$result = $wpdb->get_results("SELECT company_wojewodztwo, company_powiat, company_miasto, company_name, telefon, company_nip, company_opis FROM test WHERE company_wojewodztwo = '$woj' AND company_powiat = '$pow' AND company_miasto = '$nazwa' ORDER BY RAND()");
foreach ( $result as $k => $v ) {
$c_name = stripslashes($v->company_name);
$c_opis = stripslashes($v->company_opis);
$c_mob_nr = stripslashes($v->telefon);
$c_nip = stripslashes($v->company_nip);
?>
<table>
<tbody>
<tr style="height: 50%;">
<td style="width: 50%;"><?php echo $c_name; ?></td>
<td style="width: 50%;"><?php echo $c_name; ?></td>
</tr>
<tr style="height: 50%;">
<td style="width: 50%;"><?php echo $c_nip ?></td>
<td style="width: 50%;"><?php echo $c_nip ?></td>
</tr>
<tr style="height: 50%;">
<td style="width: 50%;"><?php echo $c_mob_nr ?></td>
<td style="width: 50%;"><?php echo $c_mob_nr ?></td>
</tr>
<tr style="height: 50%;">
<td style="width: 50%;"><?php echo $c_opis ?></td>
<td style="width: 50%;"><?php echo $c_opis ?></td>
</tr>
</tbody>
</table>
<?php if(count($result) != $k+1){ ?>
<p>___________________________________________________</p>
<?php } ?>
<?php
}
?>
do something like this:
<table>
<tbody>
<tr style="height: 50%;">
<td style="width: 50%;"><?php echo 'data 1'; ?></td>
<td style="width: 50%;"><?php echo 'data 2'; ?></td>
</tr>
<tr style="height: 50%;">
<td style="width: 50%;"><?php echo 'data 1'; ?></td>
<td style="width: 50%;"><?php echo 'data 2'; ?></td>
</tr>
</tbody>
</table>
I put styles inline, You try to put them in a css file.
Update your code...
<?php
global $wpdb;
$result = $wpdb->get_results("SELECT company_province, company_district, company_city, company_name, telefon, company_nip, company_opis FROM test WHERE company_province = '$woj' AND company_district = '$pow' AND company_city = '$nazwa' ORDER BY RAND()");
foreach ( $result as $k => $v ) {
$c_name = stripslashes($v['company_name']);
$c_opis = stripslashes($v['company_opis']);
$c_mob_nr = stripslashes($v['telefon']);
$c_nip = stripslashes($v['company_nip']);
?>
<div class="parent">
<table>
<tr><th><p><b> Name: </b></th><td>
<?php echo $c_name;?></p></td></tr>
<tr><th><p><b> NIP: </b></th><td>
<?php echo $c_nip;?></p></td></tr>
<tr><th><b> Number tel: </b></th><td>
<?php echo '+48 '.$c_mob_nr;?><br></td></tr>
<tr><th><b> Opis Firmy: </b></th><td>
<span class="more"> <?php echo $c_opis;?></span></td></tr>
</table>
<?php if(count($result) != $k+1){ ?>
<p>___________________________________________________</p>
<?php } ?>
</div>
<?php
}
?>
here it showing all records from database but it shows all empty div first then below it shows all records .i want to show each record to be shown in single div in rows.
<div id="winnercontainer">
<h2 style="background-color:#9966FF; width:850px; height:30px; font:bold; font- size:22px; color:#FFFFFF; padding-left:20px;">Winners</h2>
<?php
include('pagination/ps_pagination.php');
$conn = mysql_connect('localhost','root','');
if(!$conn) die("Failed to connect to database!");
$status = mysql_select_db('gunjanbid', $conn);
if(!$status) die("Failed to select database!");
$sql = 'SELECT * FROM winners';
$pager = new PS_Pagination($conn, $sql,10, 10);
//The paginate() function returns a mysql result set for the current page
$rs = $pager->paginate();
?>
<table>
<?php
while($row=mysql_fetch_array($rs)){
?>
<div id="winner">
<tr>
<td ><img src="memberpic/myphoto.jpg" width="150" height="150" alt="001" /></td>
<td > </td>
<td ><table >
<tr>
<td >Auction Item : <?php echo $row['Items']; ?></td>
</tr>
<tr>
<td>Rs. <?php echo $row['Rs']; ?></td>
</tr>
<tr>
<td>Winning Bid Value : Rs. <?php echo $row['WinningBidValue']; ?></td>
</tr>
<tr>
<td>MRP : Rs. <?php echo $row['MRP']; ?></td>
</tr>
<tr>
<td>Auction closed on : <?php echo $row['enddate']; ?></td>
</tr>
<tr>
<td>Winner : <?php echo $row['Winner']; ?></td>
</tr>
<tr>
<td>City: <?php echo $row['City']; ?></td>
</tr>
<tr>
<td >Delivery Status: <?php echo $row['DeliveryStatus']; ?></td>
</tr>
</table></td>
<td > </td>
<td id=save><font color=black>SAVED:</font> Rs.<?php echo $row['MRP']- $row['WinningBidValue']; ?></td>
<td > </td>
<td ><img src=productimage/1/1.1.jpg width="200" height="193" alt="001" /></td>
</tr>
</div>
<?php
}
?>
</table>
<?php
//Display the navigation
//echo $pager->renderFullNav();
echo '<div style="text-align:center">'.$pager->renderFullNav().'</div>';
?>
</div>
abd my stylesheet s are
#winnercontainer
{
width:870px;
height:auto;
background-color:#CCCCCC;
border:solid #9966FF;
border-radius:10px;
margin:auto;
clear:both;
margin:10px;
position:relative;
}
#winner
{
width:840px;
height:250px;
background-color: #999999;
border: solid #9966FF;
border-radius:10px;
margin:auto;
clear:both;
margin-top: 10px;
margin-right: 10px;
margin-bottom: 10px;
margin-left: 15px;
position:relative;
}
i want all my records to be place in div means each div have one record to be display.plz help me ,i am new here.
I'll give you a start with some old code I have that reads a table from a MySQL database and prints it out on a screen. You can modify it as you see fit. It's a php file that resides on the server.
<?php
echo "<head><title>Read list</title></head>";
$host = "xxxxxxxxxxx";
$user = "xxxxxxxxxxx";
$password = "xxxxxxxxxxx";
$dbname = "xxxxxxxxxxx";
$cxn = mysqli_connect($host,$user,$password,$dbname);
if (mysqli_connect_errno()) {echo "No connection" . mysqli_connect_error();}
$query = " select * from list where uniqueid >= 0 ";
$result = mysqli_query($cxn, $query) or die ("could not query database");
echo "<table cellpadding=1px border=1>";
while ($row = mysqli_fetch_array($result))
{
$uniqueid = $row['uniqueid'];
$localid = $row['localid'];
$mdid = $row['mid'];
$type = $row['type'];
echo "<tr><td>".$uniqueid."</td><td>".$localid."</td><td>".$mdd."</td><td>".$type."</td></tr>";
}
echo "</table>";
?>