gpt4 book ai didi

php - 切换到从 PHP 编辑 Mysql 数据

转载 作者:行者123 更新时间:2023-11-29 05:49:14 25 4
gpt4 key购买 nike

我的 php 中有一个表,其中显示来自 mysql 的“用户”表的数据。在表格中,我添加了一个编辑按钮,这样,如果您单击该按钮,表格下方会显示一个表格,您可以在其中编辑表格内容的某些字段,我还在其旁边添加了一个删除按钮以删除该行您想要的表格,但该按钮可以正常工作。

我遇到的问题是编辑按钮,当按下该按钮时,它不会显示我在表格下方添加的表单,但它会将我带到“registerTeacher.php?edit = 3”页面例子。按“编辑”按钮,它应该显示可以编辑的表单,但它什么也没做,也没有显示任何错误。有人可以帮我找到问题出在哪里吗?谢谢。

这是我的“indexAdmin.php”代码:

<?php
include('server.php');
include('Security.php');
include('Conexion.php');
include('registerTeacher.php');
?>

<?php
$style = "style='display:none;'";
if (isset($_GET['edit'])) {
$id = $_GET['edit'];
$update = true;

$record = mysqli_query($conn, "SELECT * FROM users WHERE id=$id");

if (@count($record) == 1 ) {
$n = mysqli_fetch_array($record);
$username = $n['username'];
$email = $n['email'];
$style = "style='display:block;'";
}
}
?>

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="styleIndexAdmin.css">
<meta charset="utf-8">
</head>
<body>

<!-- notification message -->
<?php if (isset($_SESSION['success'])) : ?>
<div class="error success" >
<h3>
<?php
echo $_SESSION['success'];
unset($_SESSION['success']);
?>
</h3>
</div>
<?php endif ?>

<!-- logged in user information -->
<?php if (isset($_SESSION['username'])) : ?>


<div class="navbar" id="navbar">
<a class="tablink" onclick="openPage('professorsRegistered', this, 'lightblue')" id="defaultOpen">Profesores</a>
</div>

<!----------- PROFESSROS REGISTERED TABLE ----------->

<div id="professorsRegistered" class="tabcontent"></br><br>

<table class="professorsRegistered">
<tr>
<th colspan="3"><h2>PROFESSORS REGISTERED</h2></th>
</tr>
<tr>
<th> Name </th>
<th> Edit </th>
<th> Delete </th>
</tr>

<?php
$result = $conn->query($sql);
$sql = "SELECT * FROM users WHERE rol='profesor'";
$result = $conn->query($sql);
if ($result->num_rows==0){
echo 'No professors';
}else{
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>".$row["username"]."</td>
<td><a href='registerTeacher.php?edit=".$row["id"]."' class='edit_btn' ><i class='fa fa-pencil-square-o' style='cursor:pointer;'></i></a></td>

<td><a class='eliminate' onClick=\"javascript: return confirm('Confirm to delete');\" href=\"deleteTeacher.php?id=".$row['id']."\">X</a></td>
</tr>";
}
}
?>
</table><br><br>

<!------- FORM TO EDIT REGISTERED TEACHERS ---------->
<form method="post" action="registerTeacher.php" <?php echo $style;?>>
<!----- newly added field--->
<input type="hidden" name="id" value="<?php echo $id; ?>">

<div class="input-group">
<label>USER</label>
<input type="text" name="username" value="<?php echo $username; ?>">
</div>
<div class="input-group">
<label>EMAIL</label>
<input type="email" name="email" value="<?php echo $email; ?>">
</div>
<div class="input-group">
<!--------BUTTON----->
<?php if ($update == true): ?>
<button class="btn_update" type="submit" name="update">EDIT</button>
<?php endif ?>
</div>
</form>
</div>

<?php endif ?>

<script>
function getUrlVars(){
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value){
vars[key] =value;
});
return vars;
}

function openPage(pageName,elmnt,color) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablink");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].style.backgroundColor = "";
}
document.getElementById(pageName).style.display = "block";
elmnt.style.backgroundColor = color;
}
p = getUrlVars()["page"];
//alert(p);
if (p==undefined){
document.getElementById("defaultOpen").click();
}else{
openPage(p, this, 'lightblue');
}

</script>

</body>

<?php if (isset($_SESSION['message'])): ?>
<div class="msg">
<?php
echo $_SESSION['message'];
unset($_SESSION['message']);
?>
</div>
<?php endif ?>
</html>

这是我的“registerTeacher.php”文件:

<?php
include('Conexion.php');

$username = "";
$email = "";
$errors = array();
$id = 0;
$update = false;

// UPDATE
if (isset($_POST['update'])) {
$id = $_POST['id'];
$username = $_POST['username'];
$email = $_POST['email'];

mysqli_query($conn, "UPDATE users SET username='$username', email='$email' WHERE id=$id");
$_SESSION['message'] = "User edited!";
header("Location: indexAdmin.php?page=professors");
}
?>

最佳答案

因为 <a href=..>在您的情况下用于重定向到某个页面,它重定向到 registerTeacher.php您在 href 中指定的页面attribute.Instead 你可以像下面这样:

低于<a href..>时点击你可以把表格放在registerTeacher.php上如下所示:

<?php 

if (isset($_GET['edit'])) {
$id = $_GET['edit'];
$update = true;

$record = mysqli_query($conn, "SELECT * FROM users WHERE id=$id");

if (@count($record) == 1 ) {
$n = mysqli_fetch_array($record);
$username = $n['username'];
$email = $n['email'];
$style = "style='display:block;'";
}
}
?>

<!------- FORM TO EDIT REGISTERED TEACHERS ---------->
<form method="post" action="abcpage.php" <?php echo $style;?>>
<!----- newly added field--->
<input type="hidden" name="id" value="<?php echo $id; ?>">

<div class="input-group">
<label>USER</label>
<input type="text" name="username" value="<?php echo $username; ?>">
</div>
<div class="input-group">
<label>EMAIL</label>
<input type="email" name="email" value="<?php echo $email; ?>">
</div>
<div class="input-group">
<!--------BUTTON----->
<?php if ($update == true): ?>
<button class="btn_update" type="submit" name="update">EDIT</button>
<?php endif ?>
</div>
</form>

然后您可以将此详细信息传递给您的 action="abcpage.php" .并在该页面中像下面这样写,即:

// UPDATE
if (isset($_POST['update'])) {
$id = $_POST['id'];
$username = $_POST['username'];
$email = $_POST['email'];

mysqli_query($conn, "UPDATE users SET username='$username', email='$email' WHERE id=$id");
$_SESSION['message'] = "User edited!";
header("Location: indexAdmin.php?page=professors");
}
?>

希望对您有所帮助!

注意:也可以尝试使用 prepared statement这是安全的安全。

关于php - 切换到从 PHP 编辑 Mysql 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55992367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com