Change a value of a specific row of my bd

1

I am trying to access the table of my bd, to change a value "revision" (now it is worth 1 and I want to pass to 2) of a specific row using its id.

Although the error gives me at the time of ending the connection, I think I'm doing the query wrong, but I do not know how to access that row and change the value of one of the cells if it is not this way .

I get the following error:

Warning: mysqli_free_result () expects parameter 1 to be mysqli_result, boolean given in C: \ xampp \ htdocs \ PHP_WEB_MMR \ Platform \ PHP \ Project \ validateProposal.php on line 34

<?php

// Conectamos con la base de datos.
$bd_host = "localhost"; 
$bd_usuario = "root"; 
$bd_password = ""; 
$bd_base = "carrot";


$conexion = mysqli_connect($bd_host, $bd_usuario, $bd_password); 
mysqli_select_db($conexion,$bd_base);

$idValidacion = $_POST['idValidar'];
//    $idValidacion = mysqli_real_escape_string($conexion, $_POST['idValidar']);

$revision = 2;

$consulta = "
    INSERT INTO propuesta(revision)
    VALUES ('$revision')
    WHERE id = $idValidacion
";

$resultado = mysqli_query($conexion, $consulta);

// Liberamos y cerramos conexión.
mysqli_free_result($resultado);
mysqli_close($conexion);
?>
    
asked by NEA 14.08.2018 в 14:06
source

1 answer

0

INSERT INTO is to insert a new record, if you want to update an existing record, you must use UPDATE , the structure according to the MySQL documentation is:

UPDATE [LOW_PRIORITY] [IGNORE] table_reference
    SET assignment_list
    [WHERE where_condition]
    [ORDER BY ...]
    [LIMIT row_count]

value:
    {expr | DEFAULT}

assignment:
    col_name = value

assignment_list:
    assignment [, assignment] ...

For your specific case, the query would look something like this:

UPDATE propuesta SET revision='$revision' WHERE id = $idValidacion

In any case indicate that concatenating values in queries is a very bad practice since it gives rise to sql injection, instead you should use prepared statements.

Obviously the query you have gives an error because it is not valid, because the syntax of INSERT does not support the clause WHERE . to avoid this type of errors you can wrap mysqli_query() within in if to determine if the query was executed successfully, example:

if(!$resultado = mysqli_query($conexion, $consulta)) {
    echo 'La consulta falló, error:'.mysqli_error($conexion);
}
    
answered by 14.08.2018 в 14:32