所以这是一个学校项目。我们正在建立一个网站,我正在使用PHP和MySQL以及phpMyAdmin
我的问题是,当我更新产品而不是更改字段时,它会删除除ProductID之外的行的所有数据,这是主键。
***编辑:我将在这里解决这个问题:这是一个学校项目,因此安全对我来说不是问题
我现在只需更新产品。擦除问题已经解决
**结束编辑**
码:
update-page.php - 其中包含实际的表单
<'?'php
session_start();
require_once 'db_connect.php';
$query = "SELECT * FROM Products";
$result = mysql_query($query);
if (!$result) {
die("Database query failed: " . mysql_error());
}
$num = mysql_num_rows($result);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>AppleRetail - Update Product</title>
<!-- Bootstrap Core CSS -->
<link href="css/bootstrap.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="css/update-custom.css" rel="stylesheet">
</head>
<body>
<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html">AppleRetail.com</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>
<a href="products-page.php">Products</a>
</li>
<li>
<a href="add-page.php">Add Product</a>
</li>
<li>
<a href="delete-page.php">Delete Product</a>
</li>
<li>
<a href="update-page.php">Update Price</a>
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container -->
</nav>
<!-- Page Content -->
<div class="container">
<!-- Jumbotron Header -->
<header class="jumbotron hero-spacer">
<h1>Update Product</h1>
<p>Change the information below and select "Update" to update the product information.</p>
</header>
<hr>
<form action="update.php" method="POST">
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Product ID</th>
<th>Name</th>
<th>Description</th>
<th>Price</th>
<th>Image URL</th>
</tr>
</thead>
<tbody>
<?php while($row = mysql_fetch_array($result)) { ?>
<tr>
<input type="hidden" name="id" value="<?php echo $row['ProductID']; ?>"/>
<td><?php echo $row['ProductID']; ?></td>
<td><input type="text" name="name" value="<?php echo $row['Name']; ?>"/></td>
<td><input type="text" name="description" value="<?php echo $row['Description']; ?>"/></td>
<td><input type="text" name="price" value="<?php echo $row['Price']; ?>"/></td>
<td><input type="text" name="image" value="<?php echo $row['Image']; ?>"/></td>
<td>
<input type="submit" name="update" value="Update">
</td>
</tr>
<?php }?>
</tbody>
</table>
</form>
<!-- Footer -->
<footer>
<div class="row">
<div class="col-lg-12">
<p>Copyright © AppleRetail.com</p>
</div>
</div>
</footer>
</div>
<!-- /.container -->
<!-- jQuery -->
<script src="js/jquery.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="js/bootstrap.min.js"></script>
然后update.php中有查询。
<?php
session_start();
require_once 'db_connect.php';
// Grab the posted data and send to variable
$ProductID = $_POST['ProductID'];
$Name = $_POST['Name'];
$Description = $_POST['Description'];
$Price = $_POST['Price'];
$Image = $_POST['Image'];
$_SESSION['ProductId1'] =$ProductId;
$_SESSION['Name1'] = $Name;
$_SESSION['Description1'] = $Description;
$_SESSION['Price1'] = $Price;
$_SESSION['Image1'] = $Image;
$query = "UPDATE Products SET Name='$Name', Description='$Description', Price='$Price', Image='$Image' WHERE ProductID='$ProductID'";
$result = mysql_query($query);
if (!$result) {
die("Database query failed: " . mysql_error());
}
if($result =='true'){echo "<p>Post is add</p>";}
else{ echo "<p>Post is not add</p>"; }
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>AppleRetail Update Page</title>
</head>
<body>
<?php header("Location: updateFinal.php"); ?>
</body>
</html>
感谢任何帮助!谢谢! 请询问是否需要更多信息!
答案 0 :(得分:1)
input type="text" name="name"
input type="text" name="description"
将其与
进行比较$_POST['Name'];
^
$_POST['Description'];
^
发现错误;)