简体   繁体   English

PHP MySQL成功消息始终显示

[英]PHP MySQL Success Message Always Shows

I'm having an issue with a success message or fail message. 我对成功消息或失败消息有疑问。 My code is below, but the issue is that no matter what, the success message always shows - even before the query occurs. 我的代码在下面,但是问题是无论如何,成功消息总是显示-即使在查询发生之前。 If I remove the success message if command, then it doesn't do what it's supposed to do. 如果我删除成功消息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();
}

My form is 我的表格是

<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>

The onclick function is onclick函数是

<script type='text/javascript'>
function confirm_delete() {
  return confirm('Are you sure you want to delete \'".$row['system_id']."\' from this account? ');
}
</script>

EDIT: 编辑:

Problem solved - adding an extra hidden field to the form named 'delete' makes it work the way it should. 解决了问题-在名为“ delete”的表单中添加了一个额外的隐藏字段使它按应有的方式工作。 Also, this is the final source for this operation: 另外,这是此操作的最终来源:

    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();
        }

}

Lets start with the form. 让我们从表格开始。 The $_PHP_SELF does not exist. $_PHP_SELF不存在。 What you wanted to use was $_SERVER['PHP_SELF'] . 您要使用的是$_SERVER['PHP_SELF'] You're best off not trying to deal with that variable and leaving the action empty: 最好不要尝试处理该变量,而将操作留空:

<form method="post" action="">

Your PHP will always run.... You need to do something like this: 您的PHP将始终运行。...您需要执行以下操作:

if(isset($_POST['delete'])) {
    // run the PHP here.
}

Note 注意

The mysql_* library is deprecated and set for deletion in the future. 不建议使用mysql_*库,并设置为将来删除。 Please look into using more reliable libraries like PDO or MySQLi . 请研究使用更可靠的库,例如PDOMySQLi It's not hard at all. 一点也不难。 Here's a simple PDO example to achieve what you're trying. 这是一个简单的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();

Read More about PHP PDO here 在此处阅读有关PHP PDO的更多信息


You need to supply the code for confirm_delete() that is present in your delete buttons' onClick attribute. 您需要提供delete按钮的onClick属性中存在的confirm_delete()代码。

<?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();
        }
    ?>

Try like this and let me know if it works. 尝试这样,让我知道它是否有效。 If doesn't work enable your error_reporting(). 如果不起作用,请启用error_reporting()。 Put these two lines at the beginning of the page 将这两行放在页面的开头

ini_set('display_errors',1); error_reporting(E_ALL);

EDIT: 编辑:

Change $_PHP_SELF to $_SERVER['PHP_SELF'] $_PHP_SELF to $_SERVER['PHP_SELF']更改$_PHP_SELF to $_SERVER['PHP_SELF']

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" > 

Your problem is with your $clientid variable, you declared it as $clientiddel 您的问题出在您的$ clientid变量上,您将其声明为$ clientiddel

Try this: 尝试这个:

<?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";
    }
}
?>

EDIT: They're right, you also did an error within your form tag, use this: 编辑:他们是对的,您在表单标签内也出错,请使用以下命令:

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>"> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM