I want to select the income amount from income table, update the total income and subtract the income from the total income. Then delete the row.
public function delete($id)
{
$this->db->get('income');
$this->db->select('income_amount');
$this->db->query('update total_income set amount=amount-'.$this->db->select('income_amount'));
$this->db->where('income_id', $id);
$this->db->delete('income');
}
Unless I have misunderstood your question, you want to do the following :
Select income_amount from the income table :
$this->db->select('income_amount');
$query = $this->db->get('income');
$row = $query->row();
if (isset($row)) {
$income_amount = $row->income_amount;
}
Update the total_income table by substracting $income_amount from all the amount values of the table :
$this->db->set('amount', 'amount-'.$income_amount);
$this->db->update('total_income');
Delete the row identified by $id from the income table :
$this->db->delete('income', array('income_id' => $id));
Related
I have a function that queries a table of student scores and return their total score, grade, registration number and score rank(position). I want to write an Sql join that will use the registration number returned from the scores table to get student names from the student table but the names are not returned and i am not getting any error.
public function get_subject_result(){
$subjectID = $this->input->get('subjectID');
$termID = $this->input->get('termID');
$sessionID = $this->input->get('sessionID');
//This Query which gave me headache actually ranks the total score and return positions
//$this->db->query("JOIN student ON main.stud_reg = student.reg");
$query = $this->db->query("SELECT (SELECT COUNT(*) + 1
FROM result ref
WHERE ref.total > main.total AND subject_id='".$subjectID."' AND term_id='".$termID."' AND session_id='".$sessionID."') as rank, ca1, ca2, exam,
total, remark, stud_reg, grade, subject_id
FROM result main JOIN student ON main.stud_reg = student.reg WHERE subject_id='".$subjectID."' AND term_id='".$termID."' AND session_id='".$sessionID."'
ORDER BY total DESC");
if ($query->num_rows() > 0) {
return $query->result();
}else{
return false;
}
}
I want to add the product quantity based on customer name.Here I fetch the values based on customer name and store into array ,now I want to add the quantity values.In select query ,the condition customer having 2+2= 4 qty in separate of two rows How can I add the qty values.
$selectproduct = "SELECT * FROM purchase_item WHERE custname = '$customername'";
$resultselectproduct = $conn->query($selectproduct);
if ( $resultselectproduct ->num_rows >0 )
{
while($rowselectproduct = $resultselectproduct->fetch_assoc())
{
$array[] = $rowselectproduct;
}
}
My database structure:
custname product qty
A ProA 2
A ProB 2
When I run the query based on 'A' customer I got the value as qty value 4
Just change your query and use the SUM() function. Like so:
$query = "SELECT
SUM(qty) AS total
FROM `purchase_item`
WHERE `custname` = '$customername'";
$stmt = $conn->query($query);
$result = $stmt->fetch_assoc();
echo $result['total'];
So, Im fetching the last record on the table "energy"
function find_all_energy() {
global $db;
$sql = "SELECT * FROM energy ORDER BY energyID DESC";
$result = mysqli_query($db, $sql);
$energy = mysqli_fetch_assoc($result);
mysqli_free_result($result);
return $energy;
}
The last record returns "1000", but what if there are new inserted record(s) like "2000", I want to return it as "3000" because of ( += ) and new inserted record(s) again like "5000" then it will fetch it as "8000" and so on. Thank you!
What you what is the last line of your table ?
I would suggest to do it with a MAX(id) on a WHERE requirement, like it is suggested in this post :
SELECT row
FROM table
WHERE id=(
SELECT max(id) FROM table
)
I want to update three invoice data that belongs to a customer like below:
function updateInvoice($dbh, $id, $amount, $bal, $payer){
$q = $dbh->query("SELECT * FROM `zinvoice` WHERE `id`='$id'
AND `status` IN ('Balance','Not Paid')");
$nrow = count($q->fetchAll());
$remain = 0;
if($nrow==1){
//update the amount
if($amount>$bal){
$remain = $amount-$bal;
$dbh->query("UPDATE zinvoice SET paidamount=(paidamount+$bal),
balanceamount='0', status='Paid'
WHERE id='$id'");
//payment history update
}else if($amount==$bal){
$remain = 0;
$dbh->query("UPDATE zinvoice SET paidamount=(paidamount+$bal),
balanceamount='0', status='Paid'
WHERE id='$id'");
//payment history update
}else{
$newbal = $bal-$amount;
$dbh->query("UPDATE zinvoice SET paidamount=(paidamount+$amount),
balanceamount='$newbal', status='Balance'
WHERE id='$id'");
$remain = 0;
//payment history update
}
}else{
//Nothing to update Because there is no Invoice with balance or not paid
}
return $remain;
}
Now I want to update multiple invoices by a customer id=2. Customer with id=2 has three invoices with total amount of 1500, invoice id=1 has amount=500, invoice id=2 has amount=700, invoice id=3 has amount=300
//Loop to update
$q = $dbh->query("SELECT * FROM zinvoice WHERE customerid='$cid'");
while($row =$q->fetch(PDO::FETCH_OBJ)):
$remain = updateInvoice($dbh, $row->id, $amount, $row->balanceamount, $payer);
if($remain>=1){
updateInvoice($dbh, $row->id, $remain, $row->balanceamount, $payer);
}else{
//nothing
}
endwhile;
$amount would come from a form//amount = 800
$cid would also come from a form//cid = 2
$payer would also come from a form//payer = john
The problem is that this code updated all the invoices to status=Paid. I want this code to update Invoice id=2 which has balance amount=700 and as there would be 100 amount remaining, it will continue to update the other invoices, say Invoice id=3. But Invoice id=3 has a balance amount=300, which is more than 100 amount, it will subtract 100from it and Invoice id=3 would have 200 balance amount. And there are no more amount left and would stop updating.
How would I achieve my desired output?
Note: I am not using prepared statement to avoid more codes here. And this code would be vulnerable to sql injection.
I have this code to get the total of the price column though i want it to only sum the total of those who are in the status of "Claimed"
public function getAllSales()
{
$stmt = $this->conn->prepare("Select sum(Price) AS total From tbl_orderlist");
$stmt->execute();
$result = $stmt->fetch();
return $result;
}
ClaimedList
Answered almost the same question earlier... LOL
Select COUNT(*) as total From tbl_orderlist WHERE status = 'Claimed';