将变量值与PHP中的查询结果进行比较[重复]

I'm trying to pull all the "totalsDate" values from a table "totals" and then either INSERT a new record or UPDATE an existing one based on a variable $date.

//getting all the dates from the totals table and assigning to row
    $sqlDate = "SELECT totalsDate FROM totals";
    $query = mysqli_query($dbCon, $sqlDate);
    //$row = mysqli_fetch_array($query);

    while($row = mysqli_fetch_array($query)){
      $rowDate = $row['totalsDate'];

       //if statement to either update the totals table or create a new record
      if($rowDate = $date){

      $sqlThree = "UPDATE totals SET lodgements = '$lodgementsAfter' WHERE branch_name = '$branchTest' AND totalsDate = '$date'";
      $query = mysqli_query($dbCon, $sqlThree);

      }

      else {
      $sqlFour = "INSERT INTO totals VALUES(NULL, '$branchTest', 0, '$amount', 0,  '$date')";
      $query = mysqli_query($dbCon, $sqlFour);
    }



    }

The update part works, however my else statement will never be executed and a new record cannot be entered. I also get this error:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in

I'm a bit confused on using the mysqli_fetch_array and how to actually use the data?

Thanks for any help.

</div>

your db query is not correct. You are not getting a good result back from your query. The error is saying it expects the result to be an array and its not. its returning a true of false.

read this mysqli_query

for more help.

do you have access to the db command line? what does the query return. Are you certain your db connection info is correct?

i found it.

 $rowDate = $date   should be $rowDate == $date or $rowDate === $date

your sql result is an array

$row[0] = "1st result"
$row[1] = "2nd result"
 ..etc

so theortically if you have more then 1 result, it should be doing multiple updates/inserts. are you seeing that? if its just one result then clearly 2014-02-13, 2014-02-26 are not the same.

 $sqlDate = "SELECT totalsDate FROM totals where totalsDate='" . $date . "'";

this will return results, with only records that have $date.

 $rowcount=mysqli_num_rows($query);
if ($rowcount) < 1))
{
   insert;
}
else
{
   update;
}

One of the problem is that $query gets overwritten inside the loop; see here:

while($row = mysqli_fetch_array($query)){

    // ...
    $query = mysqli_query($dbCon, $sqlThree);
    // ...
}

You should either a) don't store the result of mysqli_query() at all, or b) choose a different variable name, e.g. $update_res = mysqli_query(...);.

Better yet, use a single query to do both:

INSERT INTO totals VALUES (NULL, :branch, 0, :amount, 0, :date) 
ON DUPLICATE KEY UPDATE lodgements = :lodgements

Just make sure the proper unique constraints are defined on the table.