这是我的代码。这是"编辑"链接。我想更新数据并使用该链接保存
$op_sql = "SELECT * FROM tbl_users";
$result = $con->query($op_sql);
if ($result->rowCount() > 0)
{
echo "<table class='table' style='margin-left: 301px; margin-top: 10px; background-color: white;'>-;
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Name</th>";
echo "<th>Email</th>";
echo "<th>Date of Birth<th>";
echo "<th></th>"; echo "</tr>";
while($row = $result->fetch())
{
echo "<tr>";
echo "<td>" . $rowrID1 . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Email']. "</td>";
echo "<td>" . $row['Date of Birth'] . "</td>";
echo "<td><a href='?id=".$row['ID']."'>Edit</a></td>";
echo "</tr>";
}
echo "</table>"; unset($result);
答案 0 :(得分:0)
您可以在锚标记user_edit.php
中添加href
网址,以便像这样编辑用户详细信息。
echo "<td><a href='user_edit.php?id=".$row['ID']."'>Edit</a></td>";
<强> user_edit.php 强>
在这里,您可以获取特定用户的ID并从数据库中获取详细信息并创建这样的更新表单。
if(isset($_GET['id']) && $_GET['id']!='')
{
$op_sql = "SELECT * FROM tbl_users where id=$_GET['id']";
$result = $con->query($op_sql);
if ($result->rowCount() > 0)
{
$row = $result->fetch();
?>
<form action="user_update.php" method="post">
<input type="text" name="Name" value="<?php $row['Name'] ?>" />
.
.
<input type="hidden" name="id" value="$_GET['id']" />
.
.
<input type="submit" name="update" value="update" />
<?php
}
}
<强> user_update.php 强>
您可以在此处获取更新的表单数据并在数据库中进行更新
if(isset($_POST['update']) && isset($_POST['id']) && $_POST['id']!='' )
{
// here you can get the updated form data and update in database
// update user set Name=$_POST['Name'],.... where id=$_POST['id'];
}
注意:
尝试使用Prepared语句或PDO
答案 1 :(得分:0)
我在This question you can use the same answer in your context给出答案,我在提供接受答案的代码。
您可以根据自己的情况在此处使用get方法代替帖子。
HTML表单:
<form method="post" action="handler.php">
<button type="submit" name="passed" id="passed" class="btn btn-success btn-flat"><i class="fa fa-check"></i></button>
<button type="submit" name="insufficient" id="insufficient" class="btn btn-danger btn-flat"><i class="fa fa-times"></i></button>
</form>
PHP:
<?php
// db connection
if(isset($_POST['passed'])){
$allowed = mysqli_query($conn," UPDATE users SET Access = "1" WHERE id = '27' ");
}
if(isset($_POST['insufficient'])){
$notallowed = mysqli_query($conn," UPDATE users SET Access = "0" WHERE id = '453' ");
}
如果在任何给定点上有任何用户交互,请务必小心。