首页 > 解决方案 > 使用php批量更新记录

问题描述

我知道这个问题已被多次提出,但我无法找到我正在寻找的解决方案。我已设法将 CSV 文件上传到我的数据库并设法更新它们,但是我要更新的记录数有限制,最多为 90 行。我已尝试上传包含 400 条记录的记录,但无法更新此记录。在数据库的大小方面,它消耗了 180kb。

如果查询是问题,请指导,因为任何帮助将不胜感激

  echo  $query="SELECT  bankstatement.referenceno,bankstatement.debit,bankstatement.credit,bankstatement.status,cashbook.referenceno,
            cashbook.debit,cashbook.credit,cashbook.status
                FROM   bankstatement CROSS JOIN
                             cashbook
                            where '$bank' = '$cash' and cashbook.credit = bankstatement.debit and cashbook.debit = bankstatement.credit and bankstatement.status = '0' and cashbook.status = '0' ";

     echo   $res= mysqli_query($db,$query) or trigger_error("Query Failed! SQL: $query - Error: ".mysqli_error(),E_USER_ERROR);


        if($res){
        echo $change = "update bankstatement set status='1' where referenceno in($bank)";
        echo $change1 = "update cashbook set status='1' where referenceno in($cash)";


        echo mysqli_query($db,$change);
        echo mysqli_query($db,$change1);

echo "<script>
        alert('Success in Reconciling Process!!!');
        window.location.href='viewreconcile.php';
        </script>
        ";
    }else{
        echo "<script>
        alert('Error in Reconciling Process!!!');
        window.location.href='managereconcile.php';
        </script>
        ";
    }
    }
 }
}

标签: phpmysql

解决方案


您可以跳过第一步来检查是否有一个未设置开关的后退帐户,因为您的更新语句只会更新数组中的所有内容。

我将该where status='0'语句添加到您的更新查询中,以将更新限制为实际需要更新的行。

// The back account numbers to update
$bank_accounts= [000001, 0000002, 000003];
// adding fix from https://stackoverflow.com/questions/5527202/mysqli-stmtbind-paramnumber-of-elements-in-type-definition-string-doesnt-ma
$types = implode('', array_fill(0, count($bank_accounts), 's'));
$bank_accounts = array_merge([$types], $bank_accounts);

$to_prepare = [];
foreach($bank_accounts as $key => $value) {
   $to_prepare[$key] = &$bank_accounts[$key];
}

// Preparing the parameters for the prepared statement
$clause = implode(',', array_fill(0, count($bank_accounts), '?'));


// Preparing the statement
$stmt = $db->prepare("update bankstatement 
                     set status='1' 
                     where 
                        status='0' 
                     and 
                        referenceno in($clause)
                     ");

// always check whether the prepare() succeeded 
if ($stmt === false) {
  trigger_error($db->error, E_USER_ERROR);
  return;
}

// bind the bank accounts to the parameters
call_user_func_array(array($stmt, 'bind_param'), $to_prepare );

// Update all the rows
$stmt->execute();

这对于现金查询也是一样的。


推荐阅读