我遇到成功消息或失败消息的问题。我的代码如下,但问题是,无论如何,成功消息总是显示 - 甚至在查询发生之前。如果我删除了成功消息if
命令,那么它就不会执行它应该做的事情。
isset($_POST['delete']);
$systemid = $_POST['systemid'];
$clientiddel = $_POST['clientiddel'];
$querydel = "DELETE FROM ... WHERE customer = '" . $clientiddel . " ' AND system_id = '" . $systemid . "'";
if(mysql_query($querydel))
{
echo 'SUCCESS';
}
else
{
echo 'FAILED' .mysql_error();
}
我的表单是
<form method='post' action='" . $_PHP_SELF . "'>
<input name='systemid' type='hidden' id='systemid' value='" . $row['system_id'] . "'><input name='clientiddel' type='hidden' id='clientiddel' value='" . $row['customer'] . "'><input name='delete' type='image' src='images/delete.gif' id='delete' alt='Delete' onclick='return confirm_delete()'>
</form>
onclick功能
<script type='text/javascript'>
function confirm_delete() {
return confirm('Are you sure you want to delete \'".$row['system_id']."\' from this account? ');
}
</script>
编辑:
问题已解决 - 在名为&#39;删除&#39;的表单中添加了一个额外的隐藏字段使它按照应有的方式工作。此外,这是此操作的最终来源:
if(isset($_POST['delete'])){
$systemid = $_POST['systemid'];
$clientiddel = $_POST['clientiddel'];
$querydel = "DELETE FROM .... WHERE system_customer = '".$clientiddel." ' AND system_id = '".$systemid."'";
if(mysql_query($querydel))
{
echo 'SUCCESS';
}
else
{
echo 'FAILED' .mysql_error();
}
}
答案 0 :(得分:1)
让我们从表单开始。 $_PHP_SELF
不存在。您想要使用的是$_SERVER['PHP_SELF']
。您最好不要尝试处理该变量并将操作留空:
<form method="post" action="">
你的PHP将永远运行....你需要做这样的事情:
if(isset($_POST['delete'])) {
// run the PHP here.
}
不推荐使用mysql_*
库,并将其设置为将来删除。请考虑使用更可靠的库,例如PDO
或MySQLi
。它根本不难。这是一个简单的PDO示例,可以实现您的尝试。
/* Connect to an ODBC database using driver invocation */
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
$sql = "DELETE FROM table WHERE customer = :client_id AND system_id = :system_id";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':client_id', $_POST['clientiddel'], PDO::PARAM_INT);
$stmt->bindParam(':system_id', $_POST['systemid'], PDO::PARAM_INT);
$stmt->execute();
您需要提供confirm_delete()
按钮中存在的delete
代码。 onClick
属性。
答案 1 :(得分:0)
<?php
if(isset($_POST['delete'])){
$systemid = $_POST['systemid'];
$clientiddel = $_POST['clientiddel'];
$querydel = "DELETE FROM ... WHERE customer = '" . $clientid . " ' AND system_id = '" . $systemid . "'";
if(mysql_query($querydel))
{
echo 'SUCCESS';
}
else
{
echo 'FAILED' .mysql_error();
}
?>
尝试这样,让我知道它是否有效。如果没有工作启用error_reporting()。将这两行放在页面的开头
ini_set('display_errors',1);
error_reporting(E_ALL);
编辑:
更改$_PHP_SELF to $_SERVER['PHP_SELF']
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" >
答案 2 :(得分:0)
您的$ clientid变量出现了问题,您将其声明为$ clientiddel
试试这个:
<?php
if(isset($_POST['delete'])){
$systemid = $_POST['systemid'];
$clientid = $_POST['clientiddel'];
$strSQL = "DELETE FROM ... WHERE customer = '" . $clientid . " ' AND system_id = '" . $systemid . "'";
$queryDel = mysql_query($strSQL);
if(!$queryDel){
echo "FAILED:" . mysql_error();
} else {
echo "SUCCESS";
}
}
?>
编辑: 他们是对的,您在表单标签中也出错了,请使用:
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">