doushi5913 2018-08-14 10:08
浏览 146
已采纳

使用php批量更新记录

I'm aware this question has been asked severally but i am unable to find the solution I'm looking for. I have managed to upload CSV files to my database and managed to update them, however there is a limit to number of records i get to update which is 90 rows at most. I've tried uploading records with 400 records but it is unable to update this records. Size wise in the database it has consumed 180kb.

If the query is the issue please guide as any assistance will be appreciated

  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>
        ";
    }
    }
 }
}
  • 写回答

1条回答 默认 最新

  • doubianyu7844 2018-08-14 11:00
    关注

    You can just skip the first steps to check if there's a back account that hasn't had it's switches set, because your update statement will just update everything in the where in array.

    I added the where status='0' statement to your update query to limit the update to the rows that actually need the update.

    // 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();
    

    And this would work the same way for the cash query.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要