vendredi 14 juin 2019

Error in using basic update query in laravel

I'm trying to run the following code which is in innoDB server to update value of a table using basic query form. but it's kept showing error.


        if(isset($_POST['col_opt']) &&
            isset($_POST['val1']) &&
            isset($_POST['where_opt']) &&
            isset($_POST['val2']) 
        ){
            $col_name = $_POST['col_opt'];
            $nvalue = $_POST['val1'];
            $cond = $_POST['where_opt'];
            $condval = $_POST['val2'];

            DB::update("UPDATE animes SET ? = ? Where ? = ?",
                [$col_name, $nvalue, $cond, $condval]);

The sql is getting value of all variables. But it still showing error:

SQLSTATE[42000]: Syntax error or access violation: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '? = ? Where ? = ?' at line 1 (SQL: UPDATE animes SET genre_id = 2 Where anm_id = 4)

it's working when column names are manually given.

Shouldn't it also work in case of variables?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire