douping6871 2019-02-28 22:25
浏览 74
已采纳

php我的CRUD更新有问题

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>
  • 写回答

1条回答 默认 最新

  • douchenhui5569 2019-02-28 23:25
    关注

    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));
          }
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 设计一个温度闭环控制系统
  • ¥100 关于加载卡的问题有能知道这个要怎么处理吗?
  • ¥100 rtmpose姿态评估
  • ¥15 java 通过反射找路径下的类,打包后就找不到
  • ¥15 通联支付网上收银统一下单接口
  • ¥15 angular有偿编写,
  • ¥15 centos7系统下abinit安装时make出错
  • ¥15 hbuildex运行微信小程序报错
  • ¥15 关于#python#的问题:我知道这个问题对你们来说肯定so easy
  • ¥15 wpf datagrid如何实现多层表头