php我的CRUD更新有问题

问题描述 投票:0回答:1

我尝试禁用严格模式,将precio更改为int,尝试将其转换为double,但到目前为止没有任何作用。任何人都可以在网页开发上推荐一本好书或一个优秀的youtube频道吗?只要插入和删除,CRUD就可以工作。但更新不起作用。当我禁用严格模式时,我停止收到警告#1265,但它没有更新任何内容。我正在使用WAMP,phpmyadmin和Notepad ++。我也在我的更新上将$ _GET更改为POST,但它没有用

<?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;
?>

这是更新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>
php mysql sql phpmyadmin wamp
1个回答
0
投票

2件事。

  1. 您的表单使用POST方法,您正在寻找GET请求的更新。
  2. 在更新结束时发送的标头是无用的,因为您已经开始使用H1...对页面进行diplaying。 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)); }
© www.soinside.com 2019 - 2024. All rights reserved.