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
{
}
}
}
}
}
}
?>
Related
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);
}
}
$a_forms = array("a_GG", "a_FF");
$sql = "SELECT name, field FROM categories WHERE enabled = '1' ";
$result = mysqli_query($con,$sql);
$Others = array();
while($row = mysqli_fetch_array($result)) {
$Other_names[] = $row['name'];
$Other_fields[] = $row['db_field'];
}
for ($i=0;$i<count($a_forms);$i++) {
if ($a_forms[$i] == "a_FF") {
$dforms_sql = "SELECT *
FROM a_FF
where id=".$id;
$dforms_result = mysqli_query($con,$dforms_sql);
while ($forms_row = mysqli_fetch_array($dforms_result)) {
for ($i=0; $i<count($Other_fields); $i++) {
echo "<tr><td colspan='3'>".$Other_names[$i]."</td></tr>";
}
}
} elseif ($a_forms[$i] == "a_GG") {
$dforms_sql = "SELECT *
FROM a_GG where id=".$id;
$dforms_result = mysqli_query($con,$dforms_sql);
while($forms_row = mysqli_fetch_array($dforms_result)) {
echo 'Other cool stuff';
}
}
} //END (first) For Loop
So, for some reason, if that second for loop, $Other_field is there it will only show the IF statement for a_FF. But if the array = a_FF and a_GG and the for loop for $other_field is NOT there it displays them both. So it's obviously that for loop that is breaking something, I have no idea what though. Anyone have any thoughts?
I would like to import a single CSV with 7 columns into 2 tables in MySQL.
Columns 1, 2 and 3 go into a single row in table1. Columns 4 and 5 go as a row in table2. Columns 6 and 7 go as a row again in same table2.
How can this be done using PHP or mysql directly?
First fetch all values from csv and store it in an array. Then maintain your array as per your requirement and then start looping and inserting.
<?php
$efected = 0;
$file_temp = $_FILES["file"]["tmp_name"];
$handle = fopen($file_temp, "r"); // opening CSV file for reading
if ($handle) { // if file successfully opened
$i=0;
while (($CSVrecord = fgets($handle, 4096)) !== false) { // iterating through each line of our CSV
if($i>0)
{
list($name, $gender, $website, $category, $email, $subcat) = explode(',', $CSVrecord); // exploding CSV record (line) to the variables (fields)
//print_r($subcat); sub_cat_ids
if($email!='')
{
$chk_sql = "select * from employees where employee_email='".$email."'";
$chk_qry = mysqli_query($conn, $chk_sql);
$chk_num_row = mysqli_num_rows($chk_qry);
$cat_array = array(trim($category));
$subcat_array = explode( ';', $subcat );
if(count($subcat_array)>0)
{
$subcat_array_new = array();
foreach($subcat_array as $key => $val)
{
$subcat_array_new[] = trim($val);
}
}
$cat_sub_cat_merge = array_merge($cat_array, $subcat_array_new);
$cat_subcat_comma = implode( "','", $cat_sub_cat_merge );
foreach($cat_array as $cat_key => $cat_val)
{
$chk_cat_sql = "select * from employee_cats where cats_name = '".$cat_val."'";
$chk_cat_qry = mysqli_query($conn, $chk_cat_sql);
$chk_cat_num_row = mysqli_num_rows($chk_cat_qry);
//$all_cat_array = array();
if($chk_cat_num_row == 0)
{
$new_cat_ins = "insert into employee_cats set cats_name = '".$cat_val."', parent_cat_id = '0' ";
$new_cat_ins_qry = mysqli_query($conn, $new_cat_ins);
}
}
foreach($subcat_array_new as $subcat_key => $subcat_val)
{
$chk_subcat_sql = "select * from employee_cats where cats_name = '".$subcat_val."'";
$chk_subcat_qry = mysqli_query($conn, $chk_subcat_sql);
$chk_subcat_num_row = mysqli_num_rows($chk_subcat_qry);
//$all_cat_array = array();
if($chk_subcat_num_row == 0 && trim($subcat_val)!='')
{
//$category
$get_catid_sql = "select * from employee_cats where cats_name = '".trim($category)."'";
$chk_catid_qry = mysqli_query($conn, $get_catid_sql);
$fetch_cat_info = mysqli_fetch_array($chk_catid_qry);
$fetch_cat_id = $fetch_cat_info['cats_id'];
$new_subcat_ins = "insert into employee_cats set cats_name = '".$subcat_val."', parent_cat_id = '".$fetch_cat_id."' ";
$new_subcat_ins_qry = mysqli_query($conn, $new_subcat_ins);
}
}
$get_cat_sql = "select * from employee_cats where cats_name in ('".$cat_subcat_comma."')";
$get_cat_qry = mysqli_query($conn, $get_cat_sql);
$get_cat_num_row = mysqli_num_rows($get_cat_qry);
$sub_category_ids = array();
if($get_cat_num_row>0)
{
while($fetch_cat_id = mysqli_fetch_array($get_cat_qry))
{
if($fetch_cat_id['parent_cat_id']==0)
{
$category_id = $fetch_cat_id['cats_id'];
}
else
{
$sub_category_ids[] = $fetch_cat_id['cats_id'];
}
}
$sub_cat_id_vals_comma = implode(",", $sub_category_ids);
}
else
{
$category_id = 0;
$sub_cat_id_vals_comma = "";
}
if($chk_num_row>0)
{
// and here you can easily compose SQL queries and map you data to the tables you need using simple variables
$update_sql = "update employees set
employee_name='".$name."',
employee_gender='".$gender."',
employees_website='".$website."',
employees_cat_id='".$category_id."',
sub_cat_ids='".$sub_cat_id_vals_comma."'
where employee_email='".$email."'";
$mysqli_qry = mysqli_query($conn, $update_sql);
}
else
{
// and here you can easily compose SQL queries and map you data to the tables you need using simple variables
$insert_sql = "insert into employees set
employee_name='".$name."',
employee_gender='".$gender."',
employees_website='".$website."',
employees_cat_id='".$category_id."',
sub_cat_ids='".$sub_cat_id_vals_comma."',
employee_email='".$email."'";
$mysqli_qry = mysqli_query($conn, $insert_sql);
}
$efected = 1;
}
}
$i++;
}
fclose($handle); // closing file handler
}
if($efected==1)
{
header('location:'.$site_url.'?import=success');
}
else
{
header('location:'.$site_url.'?import=failed');
}
?>
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
}
I have a script that checks the submitgame table and if both approve1 and approve 2 are not blank it inserts data into clanstats. There is no mysql_error it simply redirects to the header without inserting anything into the clanstats table, so I have no idea what is going on. Below is the code.
<?php
include("user.php");
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM submitgame WHERE id='$id'") or die(mysql_error());
$playerclan = $row['playerclan'];
$opponentclan = $row['opponentclan'];
$win = $row['win'];
$approve1 = $row['approve1'];
$approve2 = $row['approve2'];
if($win == "won") {
$win = 1;
$points = 2;
$win2 = 0;
$points2 = 1;
}
else {
$win = 0;
$points = 1;
$win2 = 1;
$points2 = 2;
}
if($approve1 != "" && $approve2 != "") {
$query=mysql_query("INSERT INTO clanstats (clan, points, wins) VALUES ('$playerclan', '$points', '$win')");
$query=mysql_query("INSERT INTO clanstats (clan, points, wins) VALUES ('$opponentclan', '$points2', '$win2')");
echo mysql_error($query);
}
else {
header("location:../approvegames.php");
}
mysql_close($con);
header("location:../approvegames.php");
?>
<?php
//first off are you connecting, ill presume so
include("user.php");
//sql injection!!!
$id = mysql_real_escape_string($_GET['id']);
$result = mysql_query("SELECT * FROM submitgame WHERE id='$id' limit 1") or die(mysql_error());
//you were missing this
$row=mysql_fetch_array($result);
$playerclan = $row['playerclan'];
$opponentclan = $row['opponentclan'];
$win = $row['win'];
$approve1 = $row['approve1'];
$approve2 = $row['approve2'];
if($win == "won") {
$win = 1;
$points = 2;
$win2 = 0;
$points2 = 1;
}else{
$win = 0;
$points = 1;
$win2 = 1;
$points2 = 2;
}
if($approve1 != "" && $approve2 != "") {
//you can have multiple inserts
$query=mysql_query("INSERT INTO clanstats (clan, points, wins) VALUES
('$playerclan', '$points', '$win'),
('$opponentclan', '$points2', '$win2')");
header("location:../approvegames.php");
//adding die after the header will make sure nothing else gets executed
die();
}else{
header("location:../approvegames.php");
die();
}
//no need to kill the connection as it will close when the script exits
?>
I think you are missing a line. Perhaps something like:
$row = mysql_fetch_row($result)