使用PHP一次更新数据库中的所有字段

斯图比

我有一个来自表中链接的表格,该表格应该只更新数据库中的一条记录。当我更改表中的一些详细信息并按下提交按钮时,它更改了数据库中的所有字段,而不仅仅是我要更改的字段。以下是我的表单代码以及正在编辑的表格。

编辑用户代码

<?php

 // since this form is used multiple times in this file, I have made it a function that is easily reusable
 function renderForm($userID, $username, $password, $telephone, $address1, $town, $postcode, $forename, $surname, $email, $error)
 {
 ?>
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
 <html>
 <head>
 <title>Edit User</title>
 </head>
 <body>
 <?php 
 // if there are any errors, display them
 if ($error != '')
 {
 echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
 }
 ?> 
 
 <form action="" method="post">
 <input type="hidden" name="userID" value="<?php echo $userID; ?>"/>
 <div>
 <p><strong>ID:</strong> <?php echo $userID; ?></p>
 <strong>Username: </strong> <input type="text" name="username" value="<?php echo $username; ?>"/><br/>
 <strong>Password: </strong> <input type="text" name="password" value="<?php echo $password; ?>"/><br/>
 <strong>Telephone: </strong> <input type="text" name="telephone" value="<?php echo $telephone; ?>"/><br/>
 <strong>Address: </strong> <input type="text" name="address1" value="<?php echo $address1; ?>"/><br/>
 <strong>Town: </strong> <input type="text" name="town" value="<?php echo $town; ?>"/><br/>
 <strong>Postcode: </strong> <input type="text" name="postcode" value="<?php echo $postcode; ?>"/><br/>
 <strong>Forename: </strong> <input type="text" name="forename" value="<?php echo $forename; ?>"/><br/>
 <strong>Surname: </strong> <input type="text" name="surname" value="<?php echo $surname; ?>"/><br/>
 <strong>Email: </strong> <input type="text" name="email" value="<?php echo $email; ?>"/><br/>

 <input type="submit" name="submit" value="Edit details">
 </div>
 </form> 
 </body>
 </html> 
 <?php
 }



 // connect to the database
 include "config.php";
 
 // check if the form has been submitted. If it has, process the form and save it to the database
 if (isset($_POST['submit']))
 { 
 // confirm that the 'id' value is a valid integer before getting the form data
 if (is_numeric($_POST['userID']))
 {
 // get form data, making sure it is valid
 $userID = $_POST['userID'];
 $username = $_POST['username'];
 $password = $_POST['password'];
 $telephone = $_POST['telephone'];
 $address1 = $_POST['address1'];
 $town = $_POST['town'];
 $postcode = $_POST['postcode'];
 $forename = $_POST['forename'];
 $surname = $_POST['surname'];
 $email = $_POST['email'];
 
 // check that firstname/lastname fields are both filled in
 if ($username == '' || $password == '' || $telephone == '' || $address1 == '' || $town == '' || $postcode == '' || $forename == '' || $surname == '' || $email == '' )
 {
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';
 
 //error, display form
 renderForm($userID, $username, $password, $telephone, $address1, $town, $postcode, $forename, $surname, $email, $error);
 }
 else
 {
 // save the data to the database
 	$query = $db->prepare("UPDATE user SET username='$username', password='$password', telephone='$telephone', address1='$address1', town='$town', postcode='$postcode', forename='$forename', surname='$surname', email='$email' ");
	$query->execute();
 
 // once saved, redirect back to the view page
 header("Location: view_user.php"); 
 }
 }
 else
 {
 // if the 'id' isn't valid, display an error
 echo 'Error!';
 }
 }
 else
 // if the form hasn't been submitted, get the data from the db and display the form
 {
 
 // get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)
 if (isset($_GET['userID']) && is_numeric($_GET['userID']) && $_GET['userID'] > 0)
 {
 // query db
 $userID = $_GET['userID'];
 $query = $db->prepare("SELECT * FROM user WHERE userID=$userID");
 $query->execute();
 $dbRow = $query->fetch(PDO::FETCH_ASSOC);
 
 // check that the 'id' matches up with a row in the databse
 if($dbRow)
 {
 
 // get data from db
 $username = $dbRow['username'];
 $password = $dbRow['password'];
 $telephone = $dbRow['telephone'];
 $address1 = $dbRow['address1'];
 $town = $dbRow['town'];
 $postcode = $dbRow['postcode'];
 $forename = $dbRow['forename'];
 $surname = $dbRow['surname'];
 $email = $dbRow['email'];
 
 
 // show form
 renderForm($userID, $username, $password, $telephone, $address1, $town, $postcode, $forename, $surname, $email, '');
 }
 else
 // if no match, display result
 {
 echo "No results!";
 }
 }
 else
 // if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error
 {
 echo 'Error!';
 }
 }
?>

查看用户信息代码

<!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="">
    <link rel="icon" href="../../favicon.ico">

    <title>Ballymena Sports</title>

    <!-- Bootstrap core CSS -->
    <link href="bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="home2.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>

  <body>

    <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
      <div class="container">
        <div class="navbar-header">
				<a class="navbar-brand" href="home2_template.html">Ballymena Sports</a>
		</div>
		
		<ul class="nav navbar-nav navbar-right">
		    <li><a href="admin_login.php">Administrator</a></li>
            <li><a href="logout.php">Log out</a></li>
		</ul>
		
	  </div>
    </nav>
	


    <!-- Main part of homepage -->
    <div class="jumbotron">
		<div class="container">
		  <h2>Users</h2>
		  <p>This table shows all registered users of Ballymena Sports:</p>            
			
			<div class="table-responsive"> 
			<tbody>
				<?php 
					include "config.php"; 
					
					$query = $db->prepare("SELECT * FROM user ORDER BY userID asc");
					$query->execute();
		
		
					echo "<table id='user' class='table table-bordered'>
						  <tr>
						  <th>User ID</th>
						  <th>Username</th>
						  <th>Forename</th>
						  <th>Surname</th>
						  <th>Email</th>
						  <th>Address</th>
						  <th>Town</th>
						  <th>Postcode</th>
						  <th>Edit User</th> 
						  <th>Delete User</th>
						  </tr>";
						
					while ($dbRow = $query->fetch(PDO::FETCH_ASSOC)) {
						$userID = $dbRow['userID'];
						$username = $dbRow['username'];
						$forename = $dbRow['forename'];
						$surname = $dbRow['surname'];
						$email = $dbRow['email'];
						$address1 = $dbRow['address1'];
						$town = $dbRow['town'];
						$postcode = $dbRow['postcode'];
						// code to display information
						
				
			   { echo "<tr>
						<td>$userID</td>
						<td>$username</td>
						<td>$forename</td>
						<td>$surname</td>
						<td>$email</td>
						<td>$address1</td>
						<td>$town</td>
						<td>$postcode</td>
						<td><a href='edit_user.php?userID=".$userID."'>Edit</a></td>
						<td><a href='delete_user.php?userID=".$userID."'>Delete</a></td>
					  </tr>";}
				} //while
				?> 

			</tbody>
			</div>
		  </table>
		</div>
    </div>
<?php 


	if(!$_SESSION['admin_username']){
		header('location:admin_login.php'); 
		
		$name = $_SESSION['admin_username'];
	}
	
?> 

      <hr>



    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="../../dist/js/bootstrap.min.js"></script>
    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <script src="../../assets/js/ie10-viewport-bug-workaround.js"></script> 
	<!-- Header and footer later to be used as include statements -->
  </body>
</html>

伊恩·坎普(Ian Kemp)

您的问题是您的update语句未指定where子句:

$query = $db->prepare("UPDATE user SET username='$username', password='$password', telephone='$telephone', address1='$address1', town='$town', postcode='$postcode', forename='$forename', surname='$surname', email='$email' ");

您需要使用用户ID来指定您只想更新该特定用户的行:

$query = $db->prepare("UPDATE user SET username='$username', password='$password', telephone='$telephone', address1='$address1', town='$town', postcode='$postcode', forename='$forename', surname='$surname', email='$email' where userId=$userID");

您还应该研究使用准备好的语句来保护您的代码免受SQL注入攻击。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

一次导入所有mysql数据库

来自分类Dev

PHP一次将数据插入到具有多个ID的数据库中?

来自分类Dev

从onLoadFinished调用中更新数据库一次又一次

来自分类Dev

如何在一次查询中使用计算更新数据库中的多行

来自分类Dev

更新数据库中的所有DATETIME字段

来自分类Dev

更新数据库中的所有DATETIME字段

来自分类Dev

如何使用npm库Telegraf从数据库中的ID列表一次向所有电报bot用户发送消息

来自分类Dev

Propel:如何在一个循环中一次从数据库中检索所有元素?

来自分类Dev

一次将ArrayList字段插入数据库

来自分类Dev

AngularJS + IndexedDB,一次打开数据库供所有控制器使用,还是分别为每个请求打开数据库?

来自分类Dev

一次从数据库更新多个记录

来自分类Dev

在 PHP 中,如何只创建一次数据库连接并从 JavaScript 到 Ajax 多次使用它?

来自分类Dev

根据数据库一次执行php文件

来自分类Dev

通过休眠仅更新选定的字段,而不更新数据库中的所有列

来自分类Dev

更新数据库MySQL的所有日期字段

来自分类Dev

将所有可能每年仅更改一次的静态数据保留在数据库或文件系统中是否很好?

来自分类Dev

如何遍历数据库中employeeID列的所有行并一次检索一个小时的工资率?

来自分类Dev

如何遍历数据库中employeeID列的所有行并一次检索一个小时的工资率?

来自分类Dev

我是否意外地更新了生产数据库中的所有字段?

来自分类Dev

使用数据库中的记录更新哈希数组,在每个现有哈希中添加一个新字段

来自分类Dev

c# 使用 MySQL 数据库中的后台工作程序每 5 秒更新一次数据网格视图

来自分类Dev

在Firebase Realtime数据库中的所有记录中添加一个额外的字段

来自分类Dev

ORA-00955名称已由现有对象使用更新数据库代码第一次迁移ASP.NET MVC时出错

来自分类Dev

使用隐藏字段更新数据库中的数据

来自分类Dev

如何自动更新Noda Time数据库,以及多久更新一次?

来自分类Dev

如果数据库保存现有数据,如何仅在 Activity 中调用一次方法?

来自分类Dev

使用数组将数据一次插入3个表中到Postgresql数据库中

来自分类Dev

如果小工具是由参数组合标识的,那么我是否应该在数据库中的所有位置都拥有所有参数,或者只有一次?

来自分类Dev

如何使用聚合检查MongoDB数据库中的所有文档的特定字段的最新值?

Related 相关文章

  1. 1

    一次导入所有mysql数据库

  2. 2

    PHP一次将数据插入到具有多个ID的数据库中?

  3. 3

    从onLoadFinished调用中更新数据库一次又一次

  4. 4

    如何在一次查询中使用计算更新数据库中的多行

  5. 5

    更新数据库中的所有DATETIME字段

  6. 6

    更新数据库中的所有DATETIME字段

  7. 7

    如何使用npm库Telegraf从数据库中的ID列表一次向所有电报bot用户发送消息

  8. 8

    Propel:如何在一个循环中一次从数据库中检索所有元素?

  9. 9

    一次将ArrayList字段插入数据库

  10. 10

    AngularJS + IndexedDB,一次打开数据库供所有控制器使用,还是分别为每个请求打开数据库?

  11. 11

    一次从数据库更新多个记录

  12. 12

    在 PHP 中,如何只创建一次数据库连接并从 JavaScript 到 Ajax 多次使用它?

  13. 13

    根据数据库一次执行php文件

  14. 14

    通过休眠仅更新选定的字段,而不更新数据库中的所有列

  15. 15

    更新数据库MySQL的所有日期字段

  16. 16

    将所有可能每年仅更改一次的静态数据保留在数据库或文件系统中是否很好?

  17. 17

    如何遍历数据库中employeeID列的所有行并一次检索一个小时的工资率?

  18. 18

    如何遍历数据库中employeeID列的所有行并一次检索一个小时的工资率?

  19. 19

    我是否意外地更新了生产数据库中的所有字段?

  20. 20

    使用数据库中的记录更新哈希数组,在每个现有哈希中添加一个新字段

  21. 21

    c# 使用 MySQL 数据库中的后台工作程序每 5 秒更新一次数据网格视图

  22. 22

    在Firebase Realtime数据库中的所有记录中添加一个额外的字段

  23. 23

    ORA-00955名称已由现有对象使用更新数据库代码第一次迁移ASP.NET MVC时出错

  24. 24

    使用隐藏字段更新数据库中的数据

  25. 25

    如何自动更新Noda Time数据库,以及多久更新一次?

  26. 26

    如果数据库保存现有数据,如何仅在 Activity 中调用一次方法?

  27. 27

    使用数组将数据一次插入3个表中到Postgresql数据库中

  28. 28

    如果小工具是由参数组合标识的,那么我是否应该在数据库中的所有位置都拥有所有参数,或者只有一次?

  29. 29

    如何使用聚合检查MongoDB数据库中的所有文档的特定字段的最新值?

热门标签

归档