如何使用自己的管理面板编辑特定 table 条记录?
How to edit specific table records with own Admin Panel?
我从废话中简化了我的代码,所以我们可以更容易地找到问题。
我有一个 table 来自数据库的记录。我可以插入新的,删除现有的,但不知何故我不能修改它们。
当我单击“编辑”按钮时,好的那个出现了,但是当我编辑它并单击“更改”按钮时,最后一行总是更改我尝试修改的任何内容。
对不起我的英语,我写了一个例子:
狗
猫
车
主席
如果我编辑猫,椅子就会改变...非常令人沮丧:D
这是我的代码:
<table>
<tr>
<th>ID</th>
<th>Title</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<?php
include("connect.php");
$get_cats = "SELECT * FROM categories";
$run_cats = mysqli_query($con, $get_cats);
while ($row_cats = mysqli_fetch_array($run_cats)) {
$cat_id = $row_cats['cat_id'];
$cat_title = $row_cats['cat_title'];
?>
<tr>
<td><?php echo $cat_id; ?></td>
<td><?php echo $cat_title; ?></td>
<td><a href="index.php?manage_cats&edit_cat=<?php echo $cat_id; ?>">Edit</a></td>
<td><a href="index.php?manage_cats&delete_cat=<?php echo $cat_id; ?>">Delete</a></td>
</tr>
<?php } ?> <!-- end while -->
<tr>
<td>Insert New Category</td>
<td colspan="2"><input type="text" name="insert_cat_title" /></td>
<td><input type="submit" name="insert_cat" value="Insert"></td>
</tr>
<?php
if(isset($_GET['edit_cat'])) {
$edit_id = $_GET['edit_cat'];
$get_cat = "SELECT * FROM categories WHERE cat_id = '$edit_id'";
$run_cat_new = mysqli_query($con, $get_cat);
while ($row_cat = mysqli_fetch_array($run_cat_new)) {
$cat_id = $row_cat['cat_id'];
$cat_title = $row_cat['cat_title'];
?>
<tr>
<td>Change Category Name</td>
<td colspan="2"><input type="text" name="update_cat_title" value="<?php echo $cat_title; ?>"/></td>
<td><input type="submit" name="update_cat" value="Change"></td>
</tr>
<?php } } ?>
</table>
</form>
<?php
// Insert Category
if(isset($_POST['insert_cat'])) {
$cat_title = $_POST['insert_cat_title'];
if($cat_title == '') {
echo "<script>alert('Please insert Category Name')</script>";
echo "<script>window.open('index.php?manage_cats', '_self')</script>";
}
else {
$insert_cat = "INSERT INTO categories (cat_title) VALUES ('$cat_title')";
$run_cat = mysqli_query($con, $insert_cat);
echo "<script>window.open('index.php?manage_cats', '_self')</script>";
}
}
// Delete Category
if(isset($_GET['delete_cat'])) {
$delete_id = $_GET['delete_cat'];
$delete_cat = "DELETE FROM categories WHERE cat_id = '$delete_id'";
$run_delete = mysqli_query($con, $delete_cat);
echo "<script>window.open('index.php?manage_cats', '_self')</script>";
}
// Edit Category
if(isset($_POST['update_cat'])) {
$cat_title_new = $_POST['update_cat_title'];
if($cat_title == '') {
echo "<script>alert('Please insert Category Name')</script>";
echo "<script>window.open('index.php?manage_cats&edit_cat', '_self')</script>";
}
else {
$update_cat = "UPDATE categories SET cat_title = '$cat_title_new' WHERE cat_id = '$cat_id'";
$run_update = mysqli_query($con, $update_cat);
echo "<script>window.open('index.php?manage_cats', '_self')</script>";
}
}
?>
添加隐藏字段
<input type="hidden" name="update_cat_id" value="<?php echo $cat_id; ?>"/>
之后
<input type="text" name="update_cat_title" value="<?php echo $cat_title; ?>"/>
并设置 where 条件,例如:
$cat_id = $_POST['update_cat_id'];
$update_cat = "UPDATE categories SET cat_title = '$cat_title_new' WHERE cat_id = '$cat_id'";
您的问题出在具有同名属性的输入上。
会给你一个像
这样的查询字符串
http.example.com/index.php?test=1&test=2&test=3&test=4
在你的 URL 中,但在 php 中 var_dump($_GET) 将 return 你只有这个
数组(1) { ["test"]=> 字符串(1) "4" }
您只需覆盖 $_GET['test'] 的值四次,它就会采用最后一个值。
可能的解决方案:
<form method="get">
<input type="text" name="test[]" value="1" />
<input type="text" name="test[]" value="2" />
<input type="text" name="test[]" value="3" />
<input type="text" name="test[]" value="4" />
<input type="submit" />
</form>
然后 var_dump($_GET) 给你这个:
array(1) {
["test"]=> array(4) {
[0]=> string(1) "1"
[1]=> string(1) "2"
[2]=> string(1) "3"
[3]=> string(1) "4"
} }
更温和的解决方案是像普通 table 单元格(无输入)一样打印类别,并使用 jquery 通过单击按钮将单元格内容转换为输入,link, 等等
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td>Cell 1</td>
<td><a href="#">Edit</a>
</td>
<td>Cell 2</td>
<td><a href="#">Edit</a>
</td>
<td>Cell 3</td>
<td><a href="#">Edit</a>
</td>
<td>Cell 4</td>
<td><a href="#">Edit</a>
</td>
</tr>
</table>
<script>
$(document).ready(function() {
$("td a").click(function() {
var vvaall = $(this).parent("td").prev("td").html();
$(this).parent("td").prev("td").empty().html('<input type="text" name="test" value="' + vvaall + '" /><input type="submit" />');
});
});
</script>
我从废话中简化了我的代码,所以我们可以更容易地找到问题。
我有一个 table 来自数据库的记录。我可以插入新的,删除现有的,但不知何故我不能修改它们。
当我单击“编辑”按钮时,好的那个出现了,但是当我编辑它并单击“更改”按钮时,最后一行总是更改我尝试修改的任何内容。
对不起我的英语,我写了一个例子:
狗 猫 车 主席
如果我编辑猫,椅子就会改变...非常令人沮丧:D
这是我的代码:
<table>
<tr>
<th>ID</th>
<th>Title</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<?php
include("connect.php");
$get_cats = "SELECT * FROM categories";
$run_cats = mysqli_query($con, $get_cats);
while ($row_cats = mysqli_fetch_array($run_cats)) {
$cat_id = $row_cats['cat_id'];
$cat_title = $row_cats['cat_title'];
?>
<tr>
<td><?php echo $cat_id; ?></td>
<td><?php echo $cat_title; ?></td>
<td><a href="index.php?manage_cats&edit_cat=<?php echo $cat_id; ?>">Edit</a></td>
<td><a href="index.php?manage_cats&delete_cat=<?php echo $cat_id; ?>">Delete</a></td>
</tr>
<?php } ?> <!-- end while -->
<tr>
<td>Insert New Category</td>
<td colspan="2"><input type="text" name="insert_cat_title" /></td>
<td><input type="submit" name="insert_cat" value="Insert"></td>
</tr>
<?php
if(isset($_GET['edit_cat'])) {
$edit_id = $_GET['edit_cat'];
$get_cat = "SELECT * FROM categories WHERE cat_id = '$edit_id'";
$run_cat_new = mysqli_query($con, $get_cat);
while ($row_cat = mysqli_fetch_array($run_cat_new)) {
$cat_id = $row_cat['cat_id'];
$cat_title = $row_cat['cat_title'];
?>
<tr>
<td>Change Category Name</td>
<td colspan="2"><input type="text" name="update_cat_title" value="<?php echo $cat_title; ?>"/></td>
<td><input type="submit" name="update_cat" value="Change"></td>
</tr>
<?php } } ?>
</table>
</form>
<?php
// Insert Category
if(isset($_POST['insert_cat'])) {
$cat_title = $_POST['insert_cat_title'];
if($cat_title == '') {
echo "<script>alert('Please insert Category Name')</script>";
echo "<script>window.open('index.php?manage_cats', '_self')</script>";
}
else {
$insert_cat = "INSERT INTO categories (cat_title) VALUES ('$cat_title')";
$run_cat = mysqli_query($con, $insert_cat);
echo "<script>window.open('index.php?manage_cats', '_self')</script>";
}
}
// Delete Category
if(isset($_GET['delete_cat'])) {
$delete_id = $_GET['delete_cat'];
$delete_cat = "DELETE FROM categories WHERE cat_id = '$delete_id'";
$run_delete = mysqli_query($con, $delete_cat);
echo "<script>window.open('index.php?manage_cats', '_self')</script>";
}
// Edit Category
if(isset($_POST['update_cat'])) {
$cat_title_new = $_POST['update_cat_title'];
if($cat_title == '') {
echo "<script>alert('Please insert Category Name')</script>";
echo "<script>window.open('index.php?manage_cats&edit_cat', '_self')</script>";
}
else {
$update_cat = "UPDATE categories SET cat_title = '$cat_title_new' WHERE cat_id = '$cat_id'";
$run_update = mysqli_query($con, $update_cat);
echo "<script>window.open('index.php?manage_cats', '_self')</script>";
}
}
?>
添加隐藏字段
<input type="hidden" name="update_cat_id" value="<?php echo $cat_id; ?>"/>
之后
<input type="text" name="update_cat_title" value="<?php echo $cat_title; ?>"/>
并设置 where 条件,例如:
$cat_id = $_POST['update_cat_id'];
$update_cat = "UPDATE categories SET cat_title = '$cat_title_new' WHERE cat_id = '$cat_id'";
您的问题出在具有同名属性的输入上。
会给你一个像
这样的查询字符串http.example.com/index.php?test=1&test=2&test=3&test=4
在你的 URL 中,但在 php 中 var_dump($_GET) 将 return 你只有这个
数组(1) { ["test"]=> 字符串(1) "4" }
您只需覆盖 $_GET['test'] 的值四次,它就会采用最后一个值。
可能的解决方案:
<form method="get">
<input type="text" name="test[]" value="1" />
<input type="text" name="test[]" value="2" />
<input type="text" name="test[]" value="3" />
<input type="text" name="test[]" value="4" />
<input type="submit" />
</form>
然后 var_dump($_GET) 给你这个:
array(1) {
["test"]=> array(4) {
[0]=> string(1) "1"
[1]=> string(1) "2"
[2]=> string(1) "3"
[3]=> string(1) "4"
} }
更温和的解决方案是像普通 table 单元格(无输入)一样打印类别,并使用 jquery 通过单击按钮将单元格内容转换为输入,link, 等等
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td>Cell 1</td>
<td><a href="#">Edit</a>
</td>
<td>Cell 2</td>
<td><a href="#">Edit</a>
</td>
<td>Cell 3</td>
<td><a href="#">Edit</a>
</td>
<td>Cell 4</td>
<td><a href="#">Edit</a>
</td>
</tr>
</table>
<script>
$(document).ready(function() {
$("td a").click(function() {
var vvaall = $(this).parent("td").prev("td").html();
$(this).parent("td").prev("td").empty().html('<input type="text" name="test" value="' + vvaall + '" /><input type="submit" />');
});
});
</script>