problem when performing update mysqli php

0

I have a problem with the update with php and mysql, it does not work, and it does not update the values of the bd table.

The code is as follows:

html:

<form action="actualizar.php" method="POST">
  <h2><em>Actualizacion de Institucion</em></h2>  

      <label for="cod_institucion">Codigo Institucion: </label>
      <input type="text" name="cod_institucion" class="form-input" required/>

      <label for="nombre_institucion">Nombre Institucion: </label>
      <input type="text" name="nombre_institucion" class="form-input" required/> 

      <label for="sector_cod_sector">Codigo Sector: </label>
      <input type="text" name="sector_cod_sector" class="form-input" required/>

      <center> <input class="form-btn" name="submit" type="submit" value="Actualizar Institucion" /></center>
</form>

php:

<?php
$host = "localhost";
$user = "root";
$pw = "";
$db = "proyecto";

$con = mysqli_connect($host,$user,$pw, $db) or die("no se pudo conectar");

$cod_institucion = $_POST['cod_institucion'];
$nombre_institucion = $_POST['nombre_institucion'];
$sector_cod_sector = $_POST['sector_cod_sector'];

$actualizar =  "UPDATE institucion set nombre_institucion = '$nombre_institucion', sector_cod_sector = '$sector_cod_sector' where cod_institucion = '$cod_institucion'";

$resultado =mysqli_query($con, $actualizar) or die("error al actualizar datos");
?>

update: the only error that the browser launches when executing the code is: Notice: Undefined index: code_institution in C: \ wamp64 \ www \ projects \ administrator \ update.php on line 9 I get that same error on lines 10 and 11

update: I made the print_r ($ _ POST); who recommended me, and throws me the following: Array ([cod_inst] = = pacos [cod_sector] = > 2 [submit] = > Update Institution)

The rest I do not understand, I've been trying to repair this for hours, with the data they gave me below and I can not make it work, I'm new to what is php with mysql.

    
asked by Tondrax 24.07.2017 в 13:34
source

2 answers

1

You yourself responded.

In the update you put where cod_institucion = '$cod_institucion' but when initializing this variable you are saying that you are not sending it by POST and that it can not be initialized.

You are directly reading the variables POST and you are placing it in the query, this is a agujero de seguridad , since they may try to manipulate the query in that request. Check this link to escape the quotes: link .

PS: Check that you are not sending by GET instead of POST , maybe that is your problem.

    
answered by 24.07.2017 в 16:00
0
  

Notice: Undefined index: code_institution

this only thing that means is that it is not defined in the position you say ... Look if the array does not start like this:

replace

$_POST['cod_institucion'];

for

$_POST['cod_inst'];

At least I see that you say that that tells you the print_r according to your comment. And do it with each one.

and if it was your person I would do it like this:

$actualizar =  "UPDATE institucion set nombre_institucion = 
'{$nombre_institucion}', sector_cod_sector = '{$sector_cod_sector}' where 
cod_institucion = '{$cod_institucion}'";

And although it's a bad practice really. It is understood you are a beginner I recommend you after solving this find documentation about these topics.

    
answered by 30.07.2018 в 04:15