jeudi 29 décembre 2016

If statement condition not running

I am checking the for the user_id (it is held in a session) - this is working. Then I am running a SELECT query for that user for the database table click_count. I am checking to see if that user has any records within it, ie: $page_count. If not, I want my INSERT statement to run to add that user to the database table along with other data.

The part I do not understand is it seems that my UPDATE query is always running. For example no matter which user I login with my query only updates the only user in the database table. IE: Bob is the only user in the click_count table, if I log in with Pete, Bob's record is being updated.

I have tested the value for $page_count and it equals 0, so my INSERT should be running. I have also tried if ($page_count === 0) {

Does anyone see anything I am missing?

$curPage = $_SERVER['PHP_SELF'];
$clicks = 0;
$setup = 0;
$page_total_count = 0;
var_dump($user_id);
    $click_sql = "
    SELECT *
    FROM click_count
    WHERE user_id = ?
    AND page_url = ?
    ";
    $click_stmt = $con->prepare($click_sql);
    $click_stmt->execute(array($user_id, $curPage));
    $click_stmt_rows = $click_stmt->fetchAll(PDO::FETCH_ASSOC);
    $page_count = $click_stmt->rowCount();
    foreach ($click_stmt_rows as $click_stmt_row) {
        $setup_status = $click_stmt_row['setup'];
        $page_total_count = $click_stmt_row['page_count'];
    }
    if ($page_count == 0) {
        $click_insert_sql = "
            INSERT INTO click_count
            (user_id, page_url, page_count, setup)
            VALUES(?, ?, ?, ?)
            ON DUPLICATE KEY UPDATE page_count=page_count+1;
        ";
        $click_insert_stmt = $con->prepare($click_insert_sql);
        $click_insert_stmt->execute(array($user_id, $curPage, 1, $setup));
    }
    else {      
        $click_update_sql = "
            UPDATE click_count
            SET page_count=page_count+1
            WHERE user_id = ?
            AND page_url = ?
        ";
        $click_update_stmt = $con->prepare($click_update_sql);
        $click_update_stmt->execute(array($user_id, $curPage));
    }

Aucun commentaire:

Enregistrer un commentaire