【Foreword】
This article mainly summarizes the function of editing user information in PHP. The principle is very simple, but the syntax requirements must be correct. The author encountered a lot of problems in the writing process, many of which were syntax parsing errors caused by symbols. record here
【Step on the pit】
(1) Connect the string , because the field types such as name are char string types, so when parsing, add ' ' quotation marks outside.
So it should be written as name=' "$_POST['name']" ', so that it can be parsed into the form of name='name'
$sql = "update demo set name='".$_POST['username']."',age='".$_POST['userage']."' where id=".$_POST['userid'];
(2) The mysqli_fetch_assoc() function obtains a row from the result set as an associative array and takes a record
(3) The input box of the hidden ID is often used to pass values, <input type='hidden' name='id'>
【Code】
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <?php //connect to the user database include 'config.php'; $connect = mysqli_connect(DB_HOST,DB_USER,DB_PWD,DB_NAME); if(!$connect){ die("could not connect:".mysqli_error()); }else{ echo "Connection succeeded<br>"; //search result // The mysqli_query() function executes a MySQL query $result = mysqli_query($connect,"select* from user"); //mysqli_num_rows The number of rows in the result set, which is obtained from the call to mysql_query() $count = mysqli_num_rows($result); //If you pass a page, change it to an integer $page = isset($_GET['page'])?(int)$_GET['page']:1; //Number of displays per page $num = 5 ; //total pages $total = ceil($count / $num); if ($page<1) { $page = 1; } if ($page>$total) { $page = $total; } //Offset $offset = ($page-1)*$num; $sql = "select id,name,age from demo order by id asc limit $offset,$num"; $result = mysqli_query($connect,$sql); if ($result&&mysqli_num_rows($result)) { echo "<table border=1 cellspacing=0 class='demo'>"; while($row = mysqli_fetch_array($result)){ echo "<tr>"; echo "<td>"."<input type='checkbox'>".$row['id']."</td>"; echo "<td>".$row['name']."</td>"; echo "<td>".$row['age']."</td>"; echo "<td><a href='delete.php?id=".$row['id']."'>删除用户</a></td>"; echo "<td><a href='edit.php?id=".$row['id']."'>编辑用户</a></td>"; echo "<tr/>"; } echo'<tr> <td colspan="5"> <a href="index.php?page=1">首页</a> <a href="index.php?page=' .($page-1). '">上一页</a> <a href="index.php?page=' .($page+1). '">下一页</a> <a href="index.php?page=' .$total. '">尾页</a> The current page is '.$page.' of '.$total.' </td> </tr>'; echo "</table>"; echo "<button onclick=''>删除</button>"; }else{ echo "No data"; } } //close the connection $close = mysqli_close($connect); if(!$close){ die("Failed to close the database"); }else{ echo "Close successfully"; } ?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <?php //connect to the user database include 'config.php'; $connect = mysqli_connect(DB_HOST,DB_USER,DB_PWD,DB_NAME); if(!$connect){ die("could not connect:".mysqli_error()); }else{ //edit user list if (is_numeric($_GET['id'])) { $id = (int) $_GET['id']; } $sql = "select id,name,age from demo where id =".$id; $result = mysqli_query($connect,$sql); $data = mysqli_fetch_assoc($result); } //close the connection $close = mysqli_close($connect); if(!$close){ die("Failed to close the database"); }else{ echo "Close successfully"; } ?> <form action="change.php" method="post"> 用户名:<input type="text" name="username" value="<?php echo $data['name'];?>"><br> 年龄:<input type="text" name="userage" value="<?php echo $data['age'] ?>"><br> <input type="hidden" name="userid" value="<?php echo $data['id']?>"> <input type="submit"> </form>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <?php //connect to the user database include 'config.php'; $connect = mysqli_connect(DB_HOST,DB_USER,DB_PWD,DB_NAME); if(!$connect){ die("could not connect:".mysqli_error()); }else{ // echo $_POST['userid']; $id = (int) $_POST['userid']; if (trim($_POST['username'])&&trim($_POST['userage'])) { $sql = "update demo set name='".$_POST['username']."',age='".$_POST['userage']."' where id=".$_POST['userid']; $result = mysqli_query($connect,$sql); if($result){ echo "Modified successfully <br>"; }else{ echo "Modification failed<br>"; } } } //close the connection $close = mysqli_close($connect); if(!$close){ die("Failed to close the database"); }else{ echo "Close successfully"; } ?>
.