SQL Updates: Updates Multiple rows rather than Single rows - php

I am writing a code for MySQL to fetch the 1st row with status is "inactive" and make them "active", but whenever I tried to update the column and make it "active" my query updates multiple rows rather than the single row.
date_default_timezone_set('Asia/Kolkata');
$d = time ();
$date = date("Y-m-d", $d);
$customer_id="1470831854";
$member_details="SELECT * FROM login_update WHERE customer_id ='$customer_id' AND status='inactive' ORDER BY id ASC LIMIT 1 ";
$member = mysql_query($member_details);
while($list = mysql_fetch_array($member)){
$status = $list['status'];
$id = (int)$list['id'];
}
$date_update = "UPDATE login_update SET status='active' WHERE id = '$id'";
$enter_date = mysql_query($date_update);

I think your code should be change as follow
while($list = mysql_fetch_array($member)){
$status = $list['status'];
$customerId = (int)$list['customer_id'];
}
$date_update = "UPDATE login_update SET status='active' WHERE id = '$customerId'";
$enter_date = mysql_query($date_update);
becasue if you get $list['id'] it is always return only 1 unique value from the database then update only one record. I assumed id is your primary key.

Related

I want in this code please to increase the quantity of same item when insert without add new record to the table

I want in this code please to increase the quantity of the same item when inserting
without adding a new record to the table.
the insert working is done, but please I need when inserting to the table that
contains a unique number of the invoice to increase the number of items without
duplicate records.
<?php
session_start();
include('../connect.php');
$a = $_POST['invoice'];
$b = $_POST['product_id'];
$c = $_POST['qty'];
$saleproduct = $_POST['saleproduct'];
$w = $_POST['pt'];
$amount = $_POST['amount'];
$dateok = $_POST['dateok'];
$result = $db->prepare("SELECT * FROM products WHERE product_id= :userid");
$result->bindParam(':userid', $b);
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
$sellprice=$row['price'];
$prodcode=$row['prod_code'];
$code=$row['product_code'];
$color=$row['color_name'];
$size=$row['size_name'];
$sizenum=$row['note'];
$name=$row['product_name'];
}
$sql = "UPDATE products
SET qty=qty-?
WHERE product_id=?";
$q = $db->prepare($sql);
$q->execute(array($c,$b));
$grandtotal = $sellprice- $saleproduct ;
$d=$grandtotal*$c;
$sql = "INSERT INTO sales_order
(invoice_number,prod_code,product_id,qty,amount,saleproduct,name,color_name,size_name,note,price,product_code,date,dateok) VALUES
(:a,:prodcode,:b,:c,:d,:saleproduct,:e,:ee,:eee,:sizenum,:sellp,:i,:k,:kok)";
$q = $db->prepare($sql);
$q->execute(array(':a'=>$a,':prodcode'=>$prodcode,':b'=>$b,':c'=>$c,':d'=>$d,':saleproduct'=>$saleproduct,':e'=>$name,':ee'=>$color,':eee'=>$size,':sizenum'=>$sizenum,':sellp'=>$sellprice,':i'=>$code,':k'=>$date,':kok'=>$dateok));
?>
You'll need to run UPDATE query to update quantity of the existing record i.e.:
UPDATE `sales_order` SET `qty` = `qty` + 1 WHERE `id` = ?
You will obviously have to bind id of the desired record within sales_order table.

Need to figure out total hours between check-in and check-out times

I am trying to create a check-in/check-out table in my database. My check-in form works without issue, inserting the time into my database. The problem occurs when I try to check out. Everything is good on the first entry...
But when I try to check in and check out again, this happens...
So far so good, but when I check out...
Currently, my code updates the out column and totalTime column of all matching child_id's.
Here is my code:
// Select the correct child from the database
$sql_childID = "SELECT id FROM child
WHERE firstName = '$childFirstName'
AND lastName = '$childLastName'";
$result = $pdo->query($sql_childID);
$row = $result->fetch();
$var = $row['id'];
// Insert the check out time for the child
$query = "UPDATE checkinout
SET `out` = :nowTime
WHERE child_id = $var
AND `in` IS NOT NULL";
$statement = $pdo->prepare($query);
$statement->bindValue(':nowTime', date("YmjHis"));
$statement->execute();
// Select check in time for specified child
$sql_inTime = "SELECT `in` FROM checkinout
WHERE child_id = $var";
$inResult = $pdo->query($sql_inTime);
$inRow = $inResult->fetch();
$inTime = strtotime($inRow['in']);
// Select the check out time for specified child
$sql_outTime = "SELECT `out` FROM checkinout
WHERE child_id = $var";
$outResult = $pdo->query($sql_outTime);
$outRow = $outResult->fetch();
$outTime = strtotime($outRow['out']);
// Find total hours
$totalTime = abs($outTime - $inTime)/(60*60);
// Update totalHours column for specified child
$queryTotalTime = "UPDATE checkinout
SET totalTime = :totalTime
WHERE child_id = $var
AND 'out' IS NOT NULL";
$statement = $pdo->prepare($queryTotalTime);
$statement->bindValue(':totalTime', $totalTime);
$statement->execute();
I think you could do all of this in your first update statement using TIMESTAMPDIFF rather than figuring the total time with PHP:
UPDATE checkinout
SET
out = NOW(),
totalTime = TIMESTAMPDIFF(SECOND, `in`, NOW()) / 3600
WHERE
child_id = $var
AND out IS NULL
The criteria WHERE out IS NULL will only update rows that do not have a value in the out column yet.
IF you have MySQL Db THEN sql will be
SELECT TIMESTAMPDIFF(HOUR,in,out) from checkinout;

How to update a value by 1 if the new value inserted into the database clashes with value in the database?

I want to update the database of the sort order column to increase its value by one if the the new value inserted into the database clashes with the value that is already in the database. May I know how should I go about doing it? Please help! Thanks!
Below is my code (I am not sure whether am I on the right track):
$result = mysql_query("SELECT sortorder FROM information ORDER BY id ASC;");
if($result >= 1 ){
$i=1;
while ($initialorder = mysql_fetch_assoc($result))
{
$initialorder = $initialorder["sortorder"];
if ($sortorder == $initialorder ){
$result6 = mysql_query("SELECT * FROM information
WHERE `sortorder` = '$sortorder'");
$row6 = mysql_fetch_array($result6);
$removethis1 = $row6['id'];
$result7 = mysql_query("UPDATE information
SET `sortorder`= ((SELECT `sortorder`
FROM (SELECT MAX(`sortorder`) AS
'$initialorder' FROM information) AS '$initialorder') + 1)
WHERE id='$removethis1'");
}
$query = "INSERT INTO `information`
(`id`,`page`,`description`,`status`,`sortorder`,`keyword`,`date_added`)
VALUES
('$id','$title','$description','$status',
'$sortorder','$keyword','$date_added')";
$result = mysql_query($query, $conn);
header('Location: index.php?status=1&title='.$title);
$i++; }
}
You can do this:
INSERT INTO ON `information`
...
DUPLICATE KEY UPDATE
sortorder = '".$sortorder + 1." '

PHP: MySQL: Update is Updating More than One Row

So I am trying to update update my 2 tables: Invoice and Order_Table. I want it to update the new quantity and the new price in each table but only for the specific OrderID. I came up with this:
$UpdateQuant = "UPDATE Order_Table SET Quantity = '$NewQuant' WHERE OrderID = '$OrderID' ";
$UpdateQuant = mysql_query($UpdateQuant);
$UpdatePrice = "UPDATE Order_Table SET TotalCost = '$NewPrice' WHERE OrderID = '$OrderID' ";
$UpdatePrice = mysql_query($UpdatePrice);
//Update Invoice Table
$UpdateQuant = "UPDATE Invoice SET Quantity = '$NewQuant' WHERE OrderID = '$OrderID' ";
$UpdateQuant = mysql_query($UpdateQuant);
$UpdatePrice = "UPDATE Invoice SET TotalCost = '$NewPrice' WHERE OrderID = '$OrderID' ";
$UpdatePrice = mysql_query($UpdatePrice);
However, when I execute this, it updates every single row. I don't see why this would even happen since I'm using WHERE OrderID = '$OrderID'
Echo out the SQL just before you execute it, maybe something isn't in the variables as you expect. Otherwise, it would have to be that each record updated has the same orderID.
Also, as pointed out int he comments, only strings should be encapsulated by quotes, numeric types shouldn't have them:
You can also combine the queries you run into a single one like this:
$UpdateQuant = "UPDATE Order_Table SET Quantity = $NewQuant, TotalCost = $NewPrice WHERE OrderID = $OrderID ";
$UpdateQuant = mysql_query($UpdateQuant);
Which has combined the top two queries together and you can do likewise for the bottom two.

MySQL output CSV and update from CSV

I have a query below where I select a single ID from my database, then update a field for that ID with a 1 to indicate that this record has been processed. I now need to perform the same process but select 50 ID's and output them in CSV format, and again update each record with a 1 to indicate that these records have been processed. Any help is appreciated, I'm not sure on the most efficient method to do this.
$result = mysql_query("SELECT `id` FROM `t_ids` WHERE `f_fetched` IS null LIMIT 1");
while($row = mysql_fetch_array($result))
$f_id = $row['id'];
mysql_query("UPDATE t_ids SET f_fetched = '1' WHERE id = '$f_id'");
You could use something like this
$result = mysql_query("SELECT `id` FROM `t_ids` WHERE `f_fetched` IS null LIMIT 50");
$processed_ids = array();
while($row = mysql_fetch_array($result))
{
//do whatever processing you need to with that id
//add the id to the $processed_ids array
$processed_ids[] = $row['id'];
}
$ids = implode(",", $processed_ids); //create a comma-delimited string of ids.
//update all rows in 1 query
mysql_query("UPDATE t_ids SET f_fetched = '1' WHERE id IN ($ids)");
why not just use:
mysql_query("UPDATE t_ids SET f_fetched = '1' WHERE `f_fetched` IS null");

Categories