Home > Net >  How would I make a php if statement update my sql column
How would I make a php if statement update my sql column

Time:02-28

My Problem


I'm trying to give persons over 16 special options on the website I am creating, but I'm struggling to get the PHP to check the age and then change the column to 1 for Yes or 0 for No.

What I've Tried

if ($account['age']>=16) {
        echo 'True';
        $sql = 'UPDATE accounts SET o16 = 1 WHERE id = ?';
} else {
        echo 'False';
        $sql = 'UPDATE accounts SET o16 = 0 WHERE id = ?';
};?>    

I'm using MariaDB SQL at this moment. It returns the values true or false but it does not change the value of the column. I would be grateful if someone could help. I don't know if I have to do something different or if there's another way I could do this.

CodePudding user response:

But what are you doing about the SQL? You are just doing an update but you don't execute it. Try somethings like this.

<?php
    
    // $user_id =  you put users id here, if you want to do it over session or something.
    if ($account['age']>=16) {
        echo 'True';
        $sql = 'UPDATE accounts SET o16 = 1 WHERE id = ?';
        $sql->bind_param('i',//$user_id);
        $sql->execute();
    } else {
        echo 'False';
        $sql = 'UPDATE accounts SET o16 = 0 WHERE id = ?';
        $sql->bind_param('i',//$user_id);
        $sql->execute();
    }   

Hope this helps you in some way, i think this could be right, but correct me if im wrong :)

CodePudding user response:

use print_r($this->db->last_query()); to see the query and check under SQL execution in database server. You can check where you went wrong.

  • Related