我正在尝试使用一个文件进行更新和插入。以下代码可以插入数据,但是当我尝试更新时它不起作用,但是当我尝试更新数据时,数据也会转到其他部分。
Flow主页 - >插入/更新用户界面 - >插入/更新操作
显示所有数据的主页。有更新和删除链接按钮。现在你知道id已经过去了。
<!DOCTYPE>
<?php
session_start();
/*if(!isset($_SESSION["isLogin"]))
{
header("location:index.php");
}*/
?>
<html>
<head>
<title></title>
<?php
include_once("include.html");
include_once("dbconfig.php");
?>
</head>
<body>
<?php include_once("navbar.html"); ?>
<div class="main mainContentMargin">
<div class="row">
<?php
$sth = $pdo->prepare("SELECT * FROM product_master where isActive='y' order by id desc");
$sth->execute();
?>
<div class="card col s12">
<table class="responsive-table centered striped">
<thead>
<tr>
<th style="width: 15%">Product Name</th>
<th>Description</th>
<th style="width: 15%">Price</th>
<th>Update</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
while ($row = $sth->fetch(PDO::FETCH_ASSOC))
{
?>
<tr>
<td style="width: 15%"><?php echo $row["p_name"] ?></td>
<td ><?php echo $row["description"] ?></td>
<td style="width: 15%"><?php echo $row["price"]." Rs./".$row["unit"] ?></td>
<td style="width:5%"><a href="product.php?id=<?php echo $row["id"] ?>"><i class="material-icons">mode_edit</i></a></td>
<td style="width:5%"><a href="delete.php?id=<?php echo $row["id"] ?>"><i class="material-icons">mode_delete</i></a></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
</div>
<?php include_once("footer.html");?>
</body>
</html>
插入/更新用户界面
<?php
session_start();
/*if(!isset($_SESSION["isLogin"]))
{
header("location:index.php");
}*/
?>
<html>
<head>
<title></title>
<?php
include_once("include.html");
include_once("dbconfig.php");
?>
</head>
<body>
<?php include_once("navbar.html"); ?>
<?php
$product="";
$descritpion="";
$price="";
$unit="";
$ins_up="Insert";
if(isset($_REQUEST["id"]))
{
$sth = $pdo->prepare("SELECT * FROM product_master where id=".$_REQUEST["id"]);
$sth->execute();
while ($row = $sth->fetch(PDO::FETCH_ASSOC))
{
$product=$row["p_name"];
$descritpion=$row["description"];
$price=$row["price"];
$unit=$row["unit"];
$ins_up="Update";
}
}
?>
<div class="main mainContentMargin">
<div class="row">
<form method="post" action="insertProduct.php">
<div class="card col s12">
<div class="card-content">
<div class="input-field">
<input type="text" name="txtProductname" id="txtProductname" value="<?php echo $product ?>">
<label for="txtProductname">Product Name</label>
</div>
<div class="input-field">
<textarea name="txtDesc" id="txtDesc" class="materialize-textarea" value="<?php echo $descritpion ?>"></textarea>
<label for="txtDesc">Description</label>
<script>
$(document).ready(function($) {
$('#txtDesc').val("<?php echo $descritpion ?>");
});
</script>
</div>
<div class="input-field">
<input type="number" name="txtProductprice" id="txtProductprice" value="<?php echo $price ?>">
<label for="txtProductprice">Price</label>
</div>
<div>
<?php
if($unit=="pcs" || $unit=="")
{
?>
<input name="group1" type="radio" id="pcsUnit" value="pcs" checked />
<label for="pcsUnit">Pcs.</label>
<input name="group1" type="radio" id="pcsKg" value="kg" />
<label for="pcsKg">KG.</label>
<?php
}
else
{
?>
<input name="group1" type="radio" id="pcsUnit" value="pcs" />
<label for="pcsUnit">Pcs.</label>
<input name="group1" type="radio" id="pcsKg" value="kg" checked />
<label for="pcsKg">KG.</label>
<?php
}
?>
</div>
</div>
<div class="card-action">
<div class="input-field">
<input type="submit" class="btn" name="btnInsert" id="btnInsert" value="<?php echo $ins_up ?>"></td>
</div>
</div>
</div>
</form>
</div>
</div>
<?php include_once("footer.html");?>
</body>
</html>
插入/更新操作文件
<?php
include("dbconfig.php");
if(isset($_REQUEST["id"]))
$id=$_REQUEST["id"];
$name=$_REQUEST["txtProductname"];
$description=$_REQUEST["txtDesc"];
$price=$_REQUEST["txtProductprice"];
$unit=$_REQUEST["group1"];
if($_REQUEST["btnInsert"]!="Update")
{
$stmt=$pdo->prepare("INSERT INTO product_master (p_name, description, price,unit,isActive)
VALUES (:p_name, :description, :price,:unit,:isActive)");
$isActive='y';
$stmt->bindParam(':isActive', $isActive);
}
else
{
$stmt=$pdo->prepare("update product_master SET p_name=:p_name , description=:description , price=:price , unit=:unit where id=:id");
$stmt->bindParam(":id",$id);
}
$stmt->bindParam(':p_name', $name);
$stmt->bindParam(':description', $description);
$stmt->bindParam(':price', $price);
$stmt->bindParam(':unit', $unit);
$stmt->execute();
if($stmt->rowCount()) {
echo 'success';
} else {
echo 'update failed';
}
//header('Location: home.php');
?>
的DBConfig
<?php
$pdo = new PDO("mysql:host=localhost; dbname=db_inventory;","root","");
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec("set names utf8");
?>
答案 0 :(得分:1)
$id
时未定义 UPDATE
虽然您已经在GUI页面上定义了id
,但是该值不会随着对实际查询数据库的脚本的下一个请求传递。
将以下行添加到表单中:
<input type="hidden" name="id" value="<?php echo htmlentities($_GET['id']); ?>" />
答案 1 :(得分:0)
让用户正确检索您的ID。在您的表单中,我没有 id 的任何输入元素。因此,当您尝试从请求中获取时,它始终是空的
如果您启用了服务器错误日志,则可能会收到错误未定义变量$ id ...