INSERT, DELETE AND UPDATE in Crud does not help me

0

Good morning I'm doing an exercise in which they ask me to make an ABM form and I did it in the following way

this would be the file edit.php where is the update button that leads to another page to edit the data and then update them in the index

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Documento sin título</title>
<link rel="stylesheet" type="text/css" href="hoja.css">
</head>

<body>

<h1>ACTUALIZAR</h1>

<?php

include("conexion.php");

if(!isset($_POST["bot_actualizar"])){

$Id=$_GET["IdCliente"];

$Nombre=$_GET["Nombre"];

$Apellido=$_GET["Apellido"];

$Direccion=$_GET["Direccion"];

}else{
$Id=$_POST["IdCliente"];

$Nombre=$_POST["Nombre"];

$Apellido=$_POST["Apellido"];

$Direccion=$_POST["Direccion"];

$consulta="UPDATE cliente SET Nombre=:clinom, Apellido=:cliape, Direccion=:clidir WHERE IdCliente=:cliid";

$resultado=$conexion->prepare($consulta);

$resultado->execute(array(":cliid"=>$Id, ":clinom"=>$Nombre, ":cliape"=>$Apellido, ":clidir"=>$Direccion));

header("location:index.php");




}

?>

<p>

</p>
<p>&nbsp;</p>
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  <table width="25%" border="0" align="center">
    <tr>
      <td></td>
      <td><label for="IdCliente"></label>
      <input type="hidden" name="IdCliente" id="IdCliente"></td>
    </tr>
    <tr>
      <td>Nombre</td>
      <td><label for="Nombre"></label>
      <input type="text" name="Nombre" id="Nombre" value="<?php echo $Nombre?>"></td>
    </tr>
    <tr>
      <td>Apellido</td>
      <td><label for="Apellido"></label>
      <input type="text" name="Apellido" id="Apellido" value="<?php echo $Apellido?>"></td>
    </tr>
    <tr>
      <td>Dirección</td>
      <td><label for="Direccion"></label>
      <input type="text" name="Direccion" id="Direccion" value="<?php echo $Direccion?>"></td>
    </tr>
    <tr>
      <td colspan="2"><input type="submit" name="bot_actualizar" id="bot_actualizar" value="Actualizar"></td>
    </tr>
  </table>
</form>
<p>&nbsp;</p>
</body>
</html>

index.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>ABM</title>
<link rel="stylesheet" type="text/css" href="hoja.css">


</head>

<body>

<?php


include("conexion.php");

$base=$conexion->query("SELECT IdCliente,Nombre,Apellido,Direccion FROM cliente");

$registros=$base->fetchALL(PDO::FETCH_OBJ);

if(isset($_POST["cr"])){

$nombre=$_POST["Nombre"];

$apellido=$_POST["Apellido"];

$direccion=$_POST["Apellido"];


$consulta="Insert INTO cliente (Nombre, Apellido, Direccion) VALUES (:nom. :ape, :dir)";

$resultado=$conexion->prepare($consulta);

$resultado->execute(array(":nom"=>$nombre, ":ape"=>$apellido, ":dir"=>$direccion));

header("location:index.php");


}


//DOS FORMAS------------------------------------------------------------------------

//$registros=$conexion->query("SELECT IdCliente,Nombre,Apellido,Direccion FROM cliente")->fetchALL(PDO::FETCH_OBJ);


?>


<h1>Formulario ABM cliente_factura <span class="subtitulo"> Hecho por Santi Corso</span></h1>
<form action"<?php echo $_SERVER['PHP_SELF'];?>" method="post"></form>
  <table width="50%" border="0" align="center">
    <tr >
      <td class="primera_fila">IdCliente</td>
      <td class="primera_fila">Nombre</td>
      <td class="primera_fila">Apellido</td>
      <td class="primera_fila">Direccion</td>
      <td class="sin">&nbsp;</td>
      <td class="sin">&nbsp;</td>
      <td class="sin">&nbsp;</td>
    </tr> 


    <?php
      foreach($registros as $clientes): ?>
     <tr>
      <td><?php echo $clientes->IdCliente?> </td>
      <td><?php echo $clientes->Nombre?></td>
      <td><?php echo $clientes->Apellido?></td>
      <td><?php echo $clientes->Direccion?></td>

      <td class="bot"><a href="borrar.php?Direc=<?php echo $clientes->Direccion?>"><input type='button' name='del' id='del' value='Borrar'></a></td>
      <td class='bot'><a href="editar.php?IdCliente=<?php echo $clientes->IdCliente?> & Nombre=<?php echo $clientes->Nombre?> & Apellido=<?php echo $clientes->Apellido?> & Direccion=<?php echo $clientes->Direccion?>"><input type='button' name='up' id='up' value='Actualizar'></a></td>
    </tr>    

    <?php

    endforeach;

    ?>
    <tr>
    <td>&nbsp;</td>
      <td><input type='text' name='Nombre' size='10' class='centrado'></td>
      <td><input type='text' name='Apellido' size='10' class='centrado'></td>
      <td><input type='text' name='Direccion' size='10' class='centrado'></td>
      <td><span class="bot">
        <input type='submit' name='cr' id='cr' value='Insertar'>
      </span></td>
    <td class='bot'>&nbsp;</td></tr>    
  </table>
    </form>
<p>&nbsp;</p>
</body>
</html>

conexion.php

<?php


try{
$conexion=new PDO('mysql:host=localhost; dbname=cliente_factura', 'root', '');

$conexion->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$conexion->exec("Set character set utf8");  


}catch (Exception $e){

die('Error' . $conexion->getMessage());
echo "Linea de error" . $conexion->getLine();   


}



?>

and finally the delete.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Documento sin título</title>
</head>

<body>
<?php


include("conexion.php");

$Direc=$_GET["Direc"];

$conexion->query("DELETE FROM cliente WHERE Direccion='$Direc'");

header("location:index.php");


?>


</body>
</html>

And doing all this still does not update, neither inserting (via the page, since from the database if the row is added), nor deleting data. I do not know what the error is

    
asked by Santi Corso 16.10.2017 в 07:53
source

1 answer

0

I would like to know if the error I found would correct all the code or there is something more hidden. Since the variables are well defined (I repeat them everywhere so that they can be written) and the fields, tables, etc also. I do not find it wrong

    
answered by 16.10.2017 в 13:27