PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, February 22, 2022

[FIXED] php I'm having trouble with the update on my CRUD

 February 22, 2022     mysql, php, phpmyadmin, sql, wamp     No comments   

Issue

I tried disabling strict mode, changing precio to int, tried to convert it to double but so far nothing works. Can anyone recommend me a good book or a good youtube channel on web development?. The CRUD works so far as insert and deleting goes. But update doesn't work. When I disabled strict mode I stopped getting the warning #1265 but it didn't update anything. I'm using WAMP, phpmyadmin and Notepad++. I've also changed the $_GET to POST on my update an it didnt work

<?php
   include("conexion.php");
   $registros=$base->query("select * from articulo")- 
   >fetchAll(PDO::FETCH_OBJ);

if(isset($_POST['cr']))
{
    $nombre=$_POST["Nom"];
    $cantidad=$_POST["Can"];
    $precio=$_POST["Pre"];
    $costo=$_POST["Cos"];
    $sql="INSERT INTO articulo (nombre,cantidad,precio,costo) VALUES(:nom, :can, :pre, :cos)";
    $resultado=$base->prepare($sql);
    $resultado->execute(array(":nom"=>$nombre,":can"=>$cantidad,":pre"=>$precio,":cos"=>$costo ));
    header("Location:index.php");
}
?>

<h1>CRUD<span class="subtitulo">Create Read Update Delete</span></h1>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"> 
<table width="50%" border="0" align="center">
<tr >
  <td class="primera_fila">Id</td>
  <td class="primera_fila">Nombre</td>
  <td class="primera_fila">Cantidad</td>
  <td class="primera_fila">Precio</td>
  <td class="primera_fila">Costo</td>
  <td class="sin">&nbsp;</td>
  <td class="sin">&nbsp;</td>
  <td class="sin">&nbsp;</td>
  <td class="sin">&nbsp;</td>
</tr> 

<?php
    foreach($registros as $articulo):?> 
<tr>
  <td> <?php echo $articulo->Id_campo?> </td>
  <td> <?php echo $articulo->nombre?> </td>
  <td> <?php echo $articulo->cantidad?></td>
  <td> <?php echo $articulo->precio?> </td> 
  <td> <?php echo $articulo->costo?> </td> 
  <td class="bot"><a href="borrar.php?Id=<?php echo $articulo->Id_campo?>"><input type='button' name='del' id='del' value='Borrar'></a></td>      
  <td class='bot'><a href="editar.php?Id=<?php echo $articulo->Id_campo?> & nom=<?php echo $articulo->nombre?> & can=<?php echo $articulo->cantidad?> & pre=<?php echo $articulo->precio?> & cos=<?php echo $articulo->costo?>"><input type='button' name='up' id='up' value='Actualizar'></a></td>

</tr>       
<?php
    endforeach;
?>

And this is the code for the update php

  <h1>ACTUALIZAR</h1>
<?php

include("conexion.php");
if (!isset($_POST["bot_actualizar"]))
{
    $Id=$_GET["Id"];
    $nom=$_GET["nom"];
    $can=$_GET["can"];
    $pre=$_GET["pre"];
    $cos=$_GET["cos"];  
}
else
{
    $Id=$_GET["Id"];
    $nom=$_GET["nom"];
    $can=$_GET["can"];
    $pre=$_GET["pre"];
    $cos=$_GET["cos"];
    $sql="UPDATE articulo SET nombre=:miNom, cantidad=:miCan, precio=:miPre, 
    costo=:miCos WHERE Id_campo=:miId";
    $resultado=$base->prepare($sql);
    $resultado->execute(array(":miId"=>$Id, ":miNom"=>$nom, ":miCan"=>$can, 
    ":miPre"=>$pre, ":miCos"=>$cos));
    header("Location:index.php");
}
?>

<p>

</p>
<p>&nbsp;</p>
<form name="form1" method="post" action="<?php $_SERVER['PHP_SELF'];?>">
 <table width="25%" border="0" align="center">
   <tr>
     <td></td>
     <td><label for="id"></label>
  <input type="hidden" name="id" id="id" value="<?php echo $Id?>"></td>
 </tr>
 <tr>
  <td>Nombre</td>
  <td><label for="nom"></label>
  <input type="text" name="nom" id="nom" value="<?php echo $nom?>"></td>
 </tr>
 <tr>
  <td>Cantidad</td>
  <td><label for="can"></label>
  <input type="text" name="can" id="can" value="<?php echo $can?>"></td> 
 </tr>
 <tr>
  <td>Precio</td>
  <td><label for="pre"></label>
  <input type="text" name="pre" id="pre" value="<?php echo $pre?>"></td>
 </tr>
 <tr>
  <td>Costo</td>
  <td><label for="cos"></label>
  <input type="text" name="cos" id="cos" value="<?php echo $cos?>"></td>
  </tr>
  <tr>
    <td colspan="2"><input type="submit" name="bot_actualizar" 
   id="bot_actualizar" value="Actualizar"></td>
    </tr>
   </table>
  </form>
 <p>&nbsp;</p>

Solution

2 things.

  1. Your form is using POST method and for your update you are looking for GET request.
  2. The header sent at the end of the update is useless as you already start diplaying the page with the H1....

        include("conexion.php");
        if (!isset($_POST["bot_actualizar"]))
        {
            $Id=$_POST["Id"];
            $nom=$_POST["nom"];
            $can=$_POST["can"];
            $pre=$_POST["pre"];
            $cos=$_POST["cos"];  
        }
        else
        {
            $Id=$_POST["Id"];
            $nom=$_POST["nom"];
            $can=$_POST["can"];
            $pre=$_POST["pre"];
            $cos=$_POST["cos"];
            $sql="UPDATE articulo SET nombre=:miNom, cantidad=:miCan, precio=:miPre, 
            costo=:miCos WHERE Id_campo=:miId";
            $resultado=$base->prepare($sql);
            $resultado->execute(array(":miId"=>$Id, ":miNom"=>$nom, ":miCan"=>$can, 
            ":miPre"=>$pre, ":miCos"=>$cos));
        }
    


Answered By - Tuckbros
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing