简体   繁体   中英

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.

 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

<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. 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. What you wanted to use was $_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:

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

Note

The mysql_* library is deprecated and set for deletion in the future. Please look into using more reliable libraries like PDO or MySQLi . It's not hard at all. Here's a simple PDO example to achieve what you're trying.

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


You need to supply the code for confirm_delete() that is present in your delete buttons' onClick attribute.

<?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(). 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']

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

Your problem is with your $clientid variable, you declared it as $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'];?>"> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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