0

在页面上,我试图在具有其他编辑功能的更新表单上使用删除按钮,并且在所有尝试中,我的删除似乎与此混淆。

这是该站点的链接,因此您可以进行测试:http ://exhibitjewellery.com/editstocktest.php (该按钮当前位于“所有系列作品”下,单击名称将表单作为可折叠面板打开)

如果这有助于了解我的其余代码的外观,我已经使用了 dreaweaver cs5 向导。你能帮助我吗?

<?php 
if (isset($_GET['delete'])) {
echo 'Do you really want to delete ' . $_GET['delete'] . '? <a  href="editstock.php?yesdelete=' . $_GET['delete'] . '">Yes</a> | <a href="editstock.php">No</a>';
exit();
}
if (isset($_GET['yesdelete'])) {
$deleteSQL = sprintf("DELETE FROM pieces WHERE id='%s' LIMIT 1",
                   GetSQLValueString($_POST['id'], "int"));

mysql_select_db($database_connectmysql, $connectmysql);
$Result1 = mysql_query($deleteSQL, $connectmysql) or die(mysql_error());
header("location: editstock.php"); 
exit();
}
?>

上面的代码要通过这个按钮来激活:

<input name="delete" type="button" id="delete" value="delete" style="width:20%; float:right"/>
4

3 回答 3

0

如果您查看http://exhibitjewellery.com/editstocktest.php中的源代码,您将看到您的表单正在使用method="post"

<form action="/editstocktest.php?" method="post" name="form4" id="form4">

所以你不能我们$_GET-

if (isset($_GET['delete'])) {}

另外,在您的删除链接中,您使用的是 的值delete,但我认为您想要id-$_POST['id']

最后,当您单击该链接时,将$_POST['id']无法再在 中使用if (isset($_GET['yesdelete'])),因此您需要将 添加id到 url


此处的编辑 1
是更改它的一种建议方法,尽管根据 url 删除从来都不是一个好主意。最好确保只有管理员可以通过这种方式访问​​/删除。注意,GetSQLValueString()是 Dreamweaver 创建的函数,因此您需要确保包含定义的文件。

<?php 
if (isset($_POST['delete'])) {
echo 'Do you really want to delete ' . $_POST['piece'] . '? <a  href="editstock.php?yesdelete=' . $_POST['id'] . '">Yes</a> | <a href="editstock.php">No</a>';
exit();
}
if (isset($_GET['yesdelete'])) {
include('file where GetSQLValueString() is defined'); // you have to change **file where GetSQLValueString() is defined** to the file path
$deleteSQL = sprintf("DELETE FROM pieces WHERE id='%s' LIMIT 1",
                   GetSQLValueString($_GET['yesdelete'], "int"));

mysql_select_db($database_connectmysql, $connectmysql);
$Result1 = mysql_query($deleteSQL, $connectmysql) or die(mysql_error());
header("location: editstock.php"); 
exit();
}
?>

编辑 #2
由于您想使用onclick执行删除功能,您需要修改您的location.href和使用$_GET.

你的 html 按钮

<input name="delete" type="button" id="delete" value="delete" style="width:20%; float:right" onclick="window.location.href = '?delete=delete&id=<?php echo $row['id']; ?>&piece=<?php echo $row['piece']; ?>'"/>

我不知道你是如何填充你的表单元素的,所以改变$row['id']$row['piece']的方法。

然后你的删除功能 -

<?php 
if (isset($_GET['delete'])) {
echo 'Do you really want to delete ' . $_GET['piece'] . '? <a  href="editstock.php?yesdelete=' . $_GET['id'] . '">Yes</a> | <a href="editstock.php">No</a>';
exit();
}
if (isset($_GET['yesdelete'])) {
include('file where GetSQLValueString() is defined'); // you have to change **file where GetSQLValueString() is defined** to the file path
$deleteSQL = sprintf("DELETE FROM pieces WHERE id='%s' LIMIT 1",
                   GetSQLValueString($_GET['yesdelete'], "int"));

mysql_select_db($database_connectmysql, $connectmysql);
$Result1 = mysql_query($deleteSQL, $connectmysql) or die(mysql_error());
header("location: editstock.php"); 
exit();
}
?>

编辑 3
如果您找不到 Dreamweaver 定义函数的位置,您可以重新声明它 -

<?php 
if (isset($_GET['delete'])) {
echo 'Do you really want to delete ' . $_GET['piece'] . '? <a  href="editstock.php?yesdelete=' . $_GET['id'] . '">Yes</a> | <a href="editstock.php">No</a>';
exit();
}
if (isset($_GET['yesdelete'])) {

// redefine if does not exist
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType,
$theDefinedValue = "", $theNotDefinedValue = "") 
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}

$theValue = function_exists("mysql_real_escape_string") ?
mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;    
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}


$deleteSQL = sprintf("DELETE FROM pieces WHERE id='%s' LIMIT 1",
                   GetSQLValueString($_GET['yesdelete'], "int"));

mysql_select_db($database_connectmysql, $connectmysql);
$Result1 = mysql_query($deleteSQL, $connectmysql) or die(mysql_error());
header("location: editstock.php"); 
exit();
}
?>
于 2013-07-11T03:42:29.850 回答
-1

尝试这个

<form action="" method="get">
    <input name="delete" type="submit" id="delete" value="delete" style="width:20%; float:right"/>
</form>

或者

<input name="delete" type="button" id="delete" value="delete" style="width:20%; float:right" onclick="window.location.href = '?delete=delete'" />
于 2013-07-11T03:27:11.393 回答
-1

我知道这听起来像是一个愚蠢的问题,但是您使用的是 HTML 表单吗?此外,您可能希望输入类型为“提交”而不是“按钮”

又名

<form action="" method="GET">
    <input type="submit" name="delete" id="delete" value="delete" style="width:20%; float:right"/>
</form>
于 2013-07-11T03:27:16.190 回答