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 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp