php 我的 CRUD 更新有问题
php I'm having trouble with the update on my CRUD
我尝试禁用严格模式,将 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"> </td>
<td class="sin"> </td>
<td class="sin"> </td>
<td class="sin"> </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> </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> </p>
2 件事。
- 您的表单正在使用
POST
方法,您正在寻找 GET
请求进行更新。
更新结束时发送的 header 没有用,因为您已经开始显示带有 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));
}
我尝试禁用严格模式,将 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"> </td>
<td class="sin"> </td>
<td class="sin"> </td>
<td class="sin"> </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> </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> </p>
2 件事。
- 您的表单正在使用
POST
方法,您正在寻找GET
请求进行更新。 更新结束时发送的 header 没有用,因为您已经开始显示带有
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)); }