How to UPDATE MySQL for each row from this code? - php

I'm trying to update rows 'tracking', 'carrier', 'status' where is status 'Waiting for tracking'
$sql = "SELECT * FROM orders WHERE status = 'Waiting for tracking'";
$result = $mysqli->query($sql);
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$id = $row['id'];
$tracking = $row['tracking'];
$carrier = $row['carrier'];
$link = $row['link'];
// get DOM from URL or file
$html = file_get_html($link);
// find td tags with data-qa=shipment-tracking-number
foreach($html->find('td[data-qa=shipment-tracking-number]') as $tracking)
$tracking = $tracking->plaintext;
// find td tags with data-qa=shipment-carrier
foreach($html->find('td[data-qa=shipment-carrier]') as $carrier)
$carrier = $carrier->plaintext;
if(isset($tracking)) {
$tracking = "UPDATE orders SET tracking = '$tracking' WHERE id = '$id'";
$tracking_result = $mysqli->query($tracking);
if($mysqli->query($tracking_result) === true) {
$pendnig = "UPDATE orders SET status = 'Pending' WHERE id = '$id'";
$pending_result = $mysqli->query($pending);
}
}
if(isset($carrier)) {
$carrier = "UPDATE orders SET carrier = '$carrier' WHERE id = '$id'";
$carrier_result = $mysqli->query($carrier);
}
}
}
When execute file, update me only 'tracking' and 'carrier' not update status as 'Pending' and only for first row, not for other.

You can check if update query get succeed as below:
if($tracking_result) {
$pendnig = "UPDATE orders SET status = 'Pending' WHERE id = '$id'";
$pending_result = $mysqli->query($pending);
}
Hope it helps you.

I have fixed it with new code.
$sql = "SELECT * FROM orders WHERE status = 'Waiting for tracking'";
$result = $mysqli->query($sql);
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$id = $row['id'];
$tracking = $row['tracking'];
$carrier = $row['carrier'];
$link = $row['link'];
// get DOM from URL or file
$html = file_get_html($link);
// find td tags with data-qa=shipment-tracking-number
foreach($html->find('td[data-qa=shipment-tracking-number]') as $tracking)
$tracking = $tracking->plaintext;
// find td tags with data-qa=shipment-carrier
foreach($html->find('td[data-qa=shipment-carrier]') as $carrier)
$carrier = $carrier->plaintext;
if(isset($tracking)) {
$tracking = "UPDATE orders SET tracking = '$tracking' WHERE id = '$id'";
$tracking_result = $mysqli->query($tracking);
if($tracking_result === true) {
$pendnig = "UPDATE orders SET status = 'Pending' WHERE id = '$id'";
$pending_result = $mysqli->query($pendnig);
}
}
if(isset($carrier)) {
$carrier = "UPDATE orders SET carrier = '$carrier' WHERE id = '$id'";
$carrier_result = $mysqli->query($carrier);
}
// clean up memory
$html->clear();
unset($html);
}
}

Related

How to check mysql fetched rows in while loop are same

how to check resulted row values from while loop are same,I need to update when status is completed from all results...
$result = $db->query("SELECT * FROM fy_working_staf_cstm WHERE task_id_c='".$t_id."'");
while($row = $db->fetchRow($result)){
++$tas;
$staff_id=$row['id_c'];
$result1 = $db->query("SELECT `status` FROM `fy_working_staf` WHERE id='".$staff_id."' AND `status`='Completed'");
$staf = $db->fetchByAssoc($result1);
$status = $staf['status'];
if($stat=='Completed')
{
++$tas1;
//$comple_staus='Closed_Closed';
}
}
if(($tas == $tas1) && ($tas1 !=0) )
{
$q = $db->query("UPDATE `tasks` SET `status`='Completed' WHERE id='".$t_id."' ");
$st = $db->fetchByAssoc($q);
}
Instead i can use break condition in this as :
while($row = $db->fetchRow($result)){
$staff_id=$row['id_c'];
$result1 = $db->query("SELECT `status` FROM `fy_working_staf` WHERE id='".$staff_id."' AND `status`='Completed'");
$staf = $db->fetchByAssoc($result1);
$status = $staf['status'];
if($stat!='Completed')
{
$comple_staus='Closed_Closed';
break;// will exit the loop when the row is not same
}
}

delete not working properly in while loop

here i am trying to delete from detail table here is one problem with my code if detail table have three records than only delete one record from detail table.and also it does not effect other two records in stock table..
Only one first record this code work properly after that it does not work in for delete query from detail table and not effected in stock table...
<?php
include("include/config.inc.php");
$purchaseMasterId = isset($_REQUEST['purchaseMasterId']) ? $_REQUEST['purchaseMasterId'] : 0;
if($purchaseMasterId > 0) {
$k = 0;
$selectMaster = "SELECT purchasedetail.purchaseMasterId, colorId,
totalkg, purchaseDetailId, partyId
FROM purchasedetail
JOIN purchasemaster ON
purchaseMaster.purchaseMasterId = purchasedetail.purchaseMasterId
WHERE purchasedetail.purchaseMasterId = ".$_REQUEST['purchaseMasterId'];
$selectMasterRes = mysql_query($selectMaster);
while($purRow = mysql_fetch_array($selectMasterRes)) {
$purchaseDetailId = $purRow['purchaseDetailId'];
$purchaseMasterId = $purRow['purchaseMasterId'];
$colorId = $purRow['colorId'];
$totalkg = $purRow['totalkg'];
$partyId = $purRow['partyId'];
$select = "SELECT qty
FROM stock
WHERE partyId = ".$partyId."
AND colorId = ".$colorId;
$selectRes = mysql_query($select);
if($stockRow = mysql_fetch_array($selectRes)) {
$current = $stockRow['qty'];
}
$updateStock = "UPDATE stock
SET qty = ".$current." - ".$totalkg."
WHERE partyId = ".$partyId."
AND colorId = ".$colorId;
$updateStockRes = mysql_query($updateStock) or die(mysql_error());
if($updateStockRes) {
$deleteDt = "DELETE FROM purchasedetail
WHERE purchaseDetailId = ".$purchaseDetailId;
$deleteRes = mysql_query($deleteDt);
if($deleteRes){
$deleteMst = "DELETE FROM purchasemaster
WHERE purchaseMasterId = ".$_REQUEST['purchaseMasterId'];
$deleteMstRes = mysql_query($deleteMst) or die(mysql_error());
if(!$deleteMstRes) {
echo "Purchase Master Delete Fail";
} else {
header("Location:purdetail.php");
exit();
}
}
}
}
}
?>
Here is my updated code with perfect code working
<?php
include("include/config.inc.php");
$purchaseMasterId = isset($_REQUEST['purchaseMasterId']) ? $_REQUEST['purchaseMasterId'] : 0;
if($purchaseMasterId > 0)
{
$k = 0;
$selectMaster = "SELECT purchasedetail.purchaseMasterId,colorId,totalkg,purchaseDetailId,partyId
FROM purchasedetail
JOIN purchasemaster ON purchasemaster.purchaseMasterId = purchasedetail.purchaseMasterId
WHERE purchasedetail.purchaseMasterId = ".$_REQUEST['purchaseMasterId'];
$selectMasterRes = mysql_query($selectMaster) or die(mysql_error());
while($purRow = mysql_fetch_array($selectMasterRes))
{
$purchaseDetailId = $purRow['purchaseDetailId'];
$purchaseMasterId = $purRow['purchaseMasterId'];
$colorId = $purRow['colorId'];
$totalkg = $purRow['totalkg'];
$partyId = $purRow['partyId'];
$select = "SELECT qty
FROM stock
WHERE partyId = ".$partyId."
AND colorId = ".$colorId;
$selectRes = mysql_query($select);
if($stockRow = mysql_fetch_array($selectRes))
{
$current = $stockRow['qty'];
}
$updateStock = "UPDATE stock
SET qty = ".$current." - ".$totalkg."
WHERE partyId = ".$partyId."
AND colorId = ".$colorId;
$updateStockRes = mysql_query($updateStock) or die(mysql_error());
if($updateStockRes)
{
$deleteDt = "DELETE FROM purchasedetail
WHERE purchaseDetailId = ".$purchaseDetailId;
$deleteRes = mysql_query($deleteDt);
if($deleteRes)
{
$selectid = "SELECT purchaseDetailId
FROM purchasedetail
WHERE purchaseMasterId = ".$purchaseMasterId;
$selectidRes = mysql_query($selectid);
if(mysql_affected_rows() == 0)
{
$mst = "DELETE FROM purchasemaster
WHERE purchaseMasterId = ".$purchaseMasterId;
$mstRes = mysql_query($mst) or die(mysql_error());;
if($mstRes)
{
header("Location:purdetail.php");
exit();
}
else
{
}
}
}
}
}
}
?>

php mysql trying to use empty to only add a new record in table if a primary key exists in a different table

I am trying to only allow a submission via the form only if a party_id exists in a table using empty, here is my code at the moment it is still allowing everything through even if there is no party_id.
Any help would be great.
if($_SERVER["REQUEST_METHOD"]== "POST") {
$party_id = (int)$_POST["partyid"];
$name = $_POST["name"];
$date = $_POST["date"];
$length = (int)$_POST["length"];
$sql = "SELECT * FROM `party` WHERE `party_id`='" . $party_id . "'";
$res = mysqli_query($link, $sql);
if(empty($party_id)) { #Were any records found?
print '<p>No Parties with that ID found! please press the back button to select another party</p>';
} else {
$record = mysqli_fetch_assoc($res);
$party_name = $record["party_name"];
$price = $record["price"];
$cost = $price * $length;
$bookable = true;
$sql2 = "SELECT * FROM `reservations`" or die("Unable to connect to database");
A simpler way might be to just check if the query returned any results like this.
if($_SERVER["REQUEST_METHOD"]== "POST") {
$party_id = (int)$_POST["partyid"];
$name = $_POST["name"];
$date = $_POST["date"];
$length = (int)$_POST["length"];
$sql = "SELECT * FROM `party` WHERE `party_id`='$party_id'";
$res = mysqli_query($link, $sql);
if ( mysqli_num_rows($res ) == 0 ) {
print '<p>No Parties with that ID found! please press the back button to select another party</p>';
} else {
$record = mysqli_fetch_assoc($res);
$party_name = $record["party_name"];
$price = $record["price"];
$cost = $price * $length;
$bookable = true;
$sql2 = "SELECT * FROM `reservations`" or die("Unable to connect to database");

PHP MYSQL Multiple If Statements for Multiple UPDATES

Can someone tell me what is wrong with these IF statements?
if(isset($_POST['submit']))
{
$dropship = $unitid['id'];
$jumpship = $_POST['jumpship'];
$dsdest = $_POST['planet'];
$dslz = $_POST['landingzone'];
$dsmission = $_POST['mission'];
$ds1 = mysql_query("SELECT id, ds1 FROM gc3025_game_jumpships WHERE `id`='$jumpship'");
$ds2 = mysql_query("SELECT id, ds2 FROM gc3025_game_jumpships WHERE `id`='$jumpship'");
$ds3 = mysql_query("SELECT id, ds3 FROM gc3025_game_jumpships WHERE `id`='$jumpship'");
$dist_loc_get2 = mysql_query("SELECT * FROM gc3025_dist_game WHERE `planet`='$dsdest' AND `districtid`='$dslz'");
$distloc2 = mysql_fetch_assoc($dist_loc_get2);
$newdist = $distloc2['g_district'];
$ds_name_get = mysql_query("SELECT * FROM gc3025_game_dropships WHERE `id`='$dropship'");
$ds_name = mysql_fetch_assoc($ds_name_get);
$dsname = $ds_name['unit_name'];
$dest_name_get = mysql_query("SELECT gc3025_planets_game.Game, gc3025_planets_game.owners, gc3025_planets_game.g_planet, gc3025_planets_game.Planet_id, gc3025_planets_id.planet_name FROM gc3025_planets_id JOIN gc3025_planets_game ON gc3025_planets_id.id = gc3025_planets_game.Planet_id WHERE `g_planet`='$dsdest'");
$dest_name = mysql_fetch_assoc($dest_name_get);
$destname = $dest_name['planet_name'];
$dsdz_name_get = mysql_query("Select gc3025_dist_game.districtid, gc3025_dist_game.g_district, gc3025_dist_labels.id, gc3025_dist_labels.dist_name FROM gc3025_dist_game JOIN gc3025_dist_labels ON gc3025_dist_game.districtid = gc3025_dist_labels.id WHERE `g_district`='$newdist'");
$dsdz_name = mysql_fetch_assoc($dsdz_name_get);
$dsdzname = $dsdz_name['dist_name'];
$dsmission_name_get = mysql_query("SELECT * FROM gc3025_movement_dropdowns WHERE `id`='$dsmission'");
$dsmission_name = mysql_fetch_assoc($dsmission_name_get);
$dsmissionname = $dsmission_name['mission_type'];
if ($ds1 == 0){
mysql_query ("UPDATE `gc3025_game_jumpships` SET `ds1` = '$dsname', `ds1dest` = '$destname', `ds1dz` = '$dsdzname', `ds1mission` = '$dsmissionname' WHERE `id`='$jumpship'");
}
if ($ds1 == 1){
mysql_query ("UPDATE `gc3025_game_jumpships` SET `ds2` = '$dsname', `ds2dest` = '$destname', `ds2dz` = '$dsdzname', `ds2mission` = '$dsmissionname' WHERE `id`='$jumpship'");
}
if ($ds2 == 1){
mysql_query ("UPDATE `gc3025_game_jumpships` SET `ds3` = '$dsname', `ds3dest` = '$destname', `ds3dz` = '$dsdzname', `ds3mission` = '$dsmissionname' WHERE `id`='$jumpship'");
}
if ($ds3 == 1){
echo "This Jumpship is Full!";
}
echo "<p>$dsname Loaded on $jumpship going to $destname and to complete $dsmissionname In District $dsdzname!</p>";
Hope this is enough.
Basically the table is for a jumpship that carries 3 dropships. I need the if statement to basically If ds1 has a dropship in it then the dropship will be entered to ds2 column. if there is a dropship in ds1 and ds2 then it will update ds3 column. if all three have dropships in them then the echo "this jumpship is full" will post.
Function mysql_query returns resource, and you must use mysql_fetch_assoc for data extraction. Besides, data inside the query should be properly escaped.
$ds_query = mysql_query(sprintf(
"SELECT ds1, ds2, ds3 FROM gc3025_game_jumpships WHERE `id`='%s'",
mysql_real_escape_string($jumpship)
));
$ds_result = mysql_fetch_assoc($ds_query);
if (!$ds_result['ds1'])
{
mysql_query(sprintf(
"UPDATE `gc3025_game_jumpships` " .
"SET `ds1` = '%s', `ds1dest` = '%s', `ds1dz` = '%s', `ds1mission` = '%s' " .
"WHERE `id`='%s'",
mysql_real_escape_string($dsname),
mysql_real_escape_string($destname),
mysql_real_escape_string($dsdzname),
mysql_real_escape_string($dsmissionname),
mysql_real_escape_string($jumpship)
));
}
elseif ($ds_result['ds1'])
{
// and so on
}
elseif ($ds_result['ds2'])
{
// and so on
}

SQL won't work? It doesn't come up with errors either

I have PHP function which checks to see if variables are set and then adds them onto my SQL query. However I am don't seem to be getting any results back?
$where_array = array();
if (array_key_exists("location", $_GET)) {
$location = addslashes($_GET['location']);
$where_array[] = "`mainID` = '".$location."'";
}
if (array_key_exists("gender", $_GET)) {
$gender = addslashes($_GET["gender"]);
$where_array[] = "`gender` = '".$gender."'";
}
if (array_key_exists("hair", $_GET)) {
$hair = addslashes($_GET["hair"]);
$where_array[] = "`hair` = '".$hair."'";
}
if (array_key_exists("area", $_GET)) {
$area = addslashes($_GET["area"]);
$where_array[] = "`locationID` = '".$area."'";
}
$where_expr = '';
if ($where_array) {
$where_expr = "WHERE " . implode(" AND ", $where_array);
}
$sql = "SELECT `postID` FROM `posts` ". $where_expr;
$dbi = new db();
$result = $dbi->query($sql);
$r = mysql_fetch_row($result);
I'm trying to call the data after in a list like so:
$dbi = new db();
$offset = ($currentpage - 1) * $rowsperpage;
// get the info from the db
$sql .= " ORDER BY `time` DESC LIMIT $offset, $rowsperpage";
$result = $dbi->query($sql);
// while there are rows to be fetched...
while ($row = mysql_fetch_object($result)){
// echo data
echo $row['text'];
} // end while
Anyone got any ideas why I am not retrieving any data?
while ($row = mysql_fetch_object($result)){
// echo data
echo $row->text;
} // end while
I forgot it wasn't coming from an array!

Categories