gpt4 book ai didi

php - 尝试使用php编辑/删除sql数据库上的记录

转载 作者:行者123 更新时间:2023-11-30 00:15:35 25 4
gpt4 key购买 nike

我有一个表,我需要在其中使用 php 编辑/删除 sql 数据库中的行。问题(历史悠久)是删除/编辑功能都不能正常工作。我没有收到任何错误消息,所以我花了几个小时梳理代码并谷歌搜索和搜索这里的其他类似问题,但没有任何东西真正解决我的具体问题。

我可能错过了引号或类似的愚蠢的东西,但是一些指示将非常感激。

这是我的编辑代码:

//if the 'id' variable is set in the URL, we know that we need to edit a record 
if (isset($_GET['GroomingID']))
{
//if the form's submit button is clicked, we need to process the form
if (isset($_POST['submit']))
{
//make sure the 'id' in the URL is valid
if (is_numeric($_POST['GroomingID']))
{
//get variables from the URL/form
$GroomingID = $_POST['GroomingID'];
$firstName = htmlentities($_POST['FirstName'], ENT_QUOTES);
$lastName = htmlentities($_POST['LastName'], ENT_QUOTES);

//check that firstname and lastname are both not empty
if ($firstName == '' || $lastName == '')
{
//if they are empty, show an error message and display the form

$error = 'ERROR: Please fill in all required fields';
renderForm($firstName, $lastName, $error, $GroomingID);
}
else
{
//if everything is fine, update the record
if($stmt = $link->prepare("UPDATE grooming SET FirstName = ?, LastName = ? WHERE GroomingID=?"))
{
$stmt->bind_param("ssi", $firstName, $lastName, $GroomingID);
$stmt->execute();
$stmt->close();
}
//show an error message if the query encounters an error
else
{
echo "Error: could not preapre sql statement.";
}

//redirect the user once the form is updated
header("Location: PS_Manage_Appnts.php");
exit();
}
}
//if the 'id' variable isn't valid, show error message
else
{
echo "Error";
}
}
//if the form hasn't been submitted yet, get the info from the database and show the form
else
{
//make sure the 'id' value is valid
if (is_numeric($_GET['GroomingID']) && $_GET['GroomingID'] > 0)
{
//get 'id' from URL
$id = $_GET['GroomingID'];

//get the record from the database
if($stmt = $link->prepare("SELECT * FROM grooming WHERE GroomingID=?"))
{
$stmt->bind_param("i", $GroomingID);
$stmt->execute();

$stmt->bind_result($GroomingID, $firstName, $lastName);
$stmt->fetch();

//show the form
renderForm($firstName, $lastName, NULL, $GroomingID);

$stmt->close();
}

//show an error if the query has an error
else
{
echo "Error: could not prepare SQL statement.";
}
}
//if the 'id' value is not valid, redirect the user back to the PS_Manage_Appnts.php page
else {
header("location:PS_Manage_Appnts.php");
exit();
}
}
}

我的删除代码(与编辑代码分开):

<?php
//connect to the database
include ("newDBconn.php");

//confirm that the 'id'(in this case, 'GroomingID') variable has been set
if (isset($_GET['GroomingID']) && is_numeric($_GET['GroomingID']))
{
//get the 'id' variable from the URL
$GroomingID = $_GET['GroomingID'];

//delete record from the database
if ($stmt = $link->prepare("DELETE FROM grooming WHERE GroomingID = '?' LIMIT 1"))
{
$stmt->bind_param("i", $GroomingID);
$stmt->execute();
$stmt->close();
}
else
{
echo "ERROR: could not prepare sql statement.";
}
$link->close();
// redirect user after delete is successful
header ("location:PS_Manage_Appnts.php");
exit();
}
else
//if the 'id' variable isn't set, redirect the user
{
header("location:PS_Manage_Appnts.php");
exit();
}

?>

最后,我的数据库连接代码位于单独的 php 文件中:

<?php
//connect to db
$link = mysqli_connect('localhost', 'root', 'pwdpwd', 'pet_shop');

//check connection
if (!$link) {
printf("Connection failed: %s\n", mysqli_connect_error());
exit();
}
?>

注释:我知道 msql 注入(inject),但是 a) 这是在我的机器上进行的测试,b) 我想先解决这个问题。谢谢您的帮助!

编辑:我确实通过 FireFox 的 Firebug 运行了这个,也没有出现任何错误。但是,我确实注意到,当单击我的表格上的“删除”链接时,Firebug 会显示以下内容:

http://i.imgur.com/oPqIfOk.png

没有代码将上面带下划线的 css 文件链接到我的删除文件 (PS_delete_post.php),所以我不知道为什么它试图获取 css 文件。 PS_delete_post.php 一开始就不应该有任何链接到它的 css。

EDIT2:我已按照 Neo 的建议将 $firstName/$lastName 变量更改为全部小写。现在,当单击“编辑”时,用户会(正确地)定向到空表单来输入值,但点击提交会添加新行,而不是编辑现有行。 “删除”仍然不起作用。

最佳答案

您是否通过 Post 和 Get 发送 GroomingID

在更新文件中,您可以使用此 $GroomingID = $_POST['GroomingID']; 检索 ID,但我不确定您是通过 Post 发送还是仅通过 Get 发送。

关于php - 尝试使用php编辑/删除sql数据库上的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23687364/

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