-1

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>
        ";
    }
    }
 }
}
SamuelJak
  • 3
  • 4

1 Answers1

0

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.

Tschallacka
  • 27,901
  • 14
  • 88
  • 133
  • The array would hold the values that needed to be updated its just that after a certain number of values updating wouldn't occur. Thanks for the above solution – SamuelJak Aug 14 '18 at 12:18
  • @SamuelJak still, your update value doesn't hold any values from your first select. Only the same parameters you used to filter the first result sets in the query. – Tschallacka Aug 14 '18 at 13:59
  • I'm rather unfamiliar with prepared statements but I've managed to work with the above code but I'm getting the following error *Warning: Parameter 2 to mysqli_stmt::bind_param() expected to be a reference, value given in C:\wamp\www\BankRecon\recon.php on line 62`* How do i sort this issue – SamuelJak Aug 15 '18 at 07:34
  • @SamuelJak I forgot about that, yea you need to compile a second array that contains the references. I updated my answer. For extra info read https://stackoverflow.com/questions/16120822/mysqli-bind-param-expected-to-be-a-reference-value-given – Tschallacka Aug 15 '18 at 08:53
  • I'm getting more errors now especially in this line of code below, i dont what i'm doing wrong as I've also followed the link you provided. Could it be something i did earlier `call_user_func_array(array($stmt, 'bind_param'), $to_prepare );` – SamuelJak Aug 15 '18 at 11:22
  • This is the error I'm now getting,kindly help in sorting this issue *Warning: mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables in C:\wamp\www\BankRecon\recon1.php on line 67* – SamuelJak Aug 16 '18 at 10:09
  • can you provide a `var_dump($clause, $bank_accounts(anonymised), $to_prepare(anonymised))` output, somewhere in a pastebin? also your query text you use in `$db->prepare` – Tschallacka Aug 16 '18 at 10:12
  • The `$db->prepare` ive been using is the one you provided above and as for the query output is below: [var_dump output]: https://pasteboard.co/HzqMQuv.png – SamuelJak Aug 16 '18 at 11:52
  • @SamuelJak and the dump of the of the $bank_accounts and $to_prepare? – Tschallacka Aug 16 '18 at 12:00
  • [bank_account dump]: https://pasteboard.co/HzrtPcAs.png [to_prepare dump]: https://pasteboard.co/HzruRx8.png – SamuelJak Aug 16 '18 at 13:31
  • @SamuelJak I updated my code, see line 3 and 4 and the question I linked in the comment – Tschallacka Aug 16 '18 at 13:43
  • Hi sorry to bother but its still giving me this same error *Warning: mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables in C:\wamp\www\BankRecon\recon1.php on line 73* [New_vardump]: https://pasteboard.co/HzyzszI.png – SamuelJak Aug 17 '18 at 07:32
  • Hmm, i followed the example, then I'm not sure what to do now. Maybe try using PDO. – Tschallacka Aug 17 '18 at 07:36
  • I'll try out PDO. This is the whole code i'm using to compare from two tables with checkbox arrays, if there is a problem with it i'd really appreciate since you've helped me out last few days. [comparison_code]: https://pastebin.com/rpFN6xgy – SamuelJak Aug 17 '18 at 08:48
  • @SamuelJak only thing I can think of is that you do `$clause = implode(',', array_fill(0, count($bank_accounts), '?')` before merging the types into the $bank_accounts. but that's it really... – Tschallacka Aug 17 '18 at 09:41
  • 1
    Hey i got the code to work out over the weekend, thanks for the help – SamuelJak Aug 20 '18 at 06:24