Issue with Where Clause

Viewed 20

I have this code:

$sql = "UPDATE prodotti SET Descrizione = $descrizione, Prezzo = $prezzo, Quantita = $quantita WHERE Nome = $nome;";

    if($connessione->query($sql)){
        echo "Successful Update";
    }else{
        echo $connessione->error_log;
    }

The browser gives this error:

Fatal error: Uncaught mysqli_sql_exception: Unknown column 'ciccio' in 'where clause' in C:\xampp\htdocs\InserimentoProdotti\php\modifica.php:15 Stack trace: #0 C:\xampp\htdocs\InserimentoProdotti\php\modifica.php(15): mysqli->query('UPDATE prodotti...') #1 {main} thrown in C:\xampp\htdocs\InserimentoProdotti\php\modifica.php on line 15.

In this moment, my database is like this: Columns: ID, Name, Description, Price, Quantity. (My db is in italian, so I translated).

I need to change all those data a selected record,the user can select a specific product using a select tag in the form.

1 Answers

You are not using single quotes in your SQL statement. Therefor the values are evaluated as columns - which does not exists.

I would assume your code would work if you changed it to:

$sql = "UPDATE prodotti SET Descrizione = '$descrizione', Prezzo = '$prezzo', Quantita = '$quantita' WHERE Nome = '$nome';";

BUT you really have to think about SQL injections if the values come from user input. If they do you need to use placeholders instead.

Related