Jb1128
Jb1128

Reputation: 97

Why is my php "if statement" not affecting my mysql query?

First of all, I know mysql is deprecated. Will change to mysqli as soon as I figure out the issue at hand. My query continues to update all my rows even if the data is not set in the 'stripetoken' column. Why is this happening?

Code snippet:

$token_query = 'SELECT * FROM jobsubmission';
$token_res = mysql_query($token_query);
$token_row = mysql_fetch_array($token_res);


if(isset($token_row['stripetoken'])) {
    $updqry = 'UPDATE jobsubmission SET assigned=1 WHERE ID="'.$book_ids[$jcount].'"';
    $update = mysql_query($updqry);
    $bookdate = date("d-m-Y");

Upvotes: 1

Views: 34

Answers (2)

Jb1128
Jb1128

Reputation: 97

So while @JohnConde was absolutely correct in saying I needed to use the empty function over the isset, my solution layed elsewhere. Here is how I managed to get the query to work to my specifications:

  • instead of searching for empty, I made the 'stripetoken' column NULL by default.
  • This allowed me to use the following code:

    $token_query = 'SELECT * FROM jobsubmission WHERE ID="'.$book_ids
    [$jcount].'" and stripetoken is not null';
    $token_res = mysql_query($token_query);
    $token_row = mysql_fetch_object($token_res);
    
    if(!$token_row->stripetoken == NULL) {
    

Upvotes: 0

John Conde
John Conde

Reputation: 219804

Because $token_row['stripetoken'] is always set because it is a column in your database and it will be available in $token_row as a result. Now whether it has a value or not is a different story. You should be using empty() instead (assuming you don't want it to be true for falsy values).

if(!empty($token_row['stripetoken'])) {

Upvotes: 3

Related Questions