-1

Why my last else $msg is not working ? Even I do this correct. Please help and fix my issue.

Code:

 //Cancel button start
 if (isset($_POST['cncl'])) {
     $sql = "DELETE FROM users WHERE Email_ID='$email' AND token='$token' AND isEmailConfirmed='PENDING' AND isMobileConfirmed='PENDING'";
     ///     
     if ($con - > query($sql) === TRUE) {
         $msg = "<div class='alert-box success'><span>Registration cancelled. < /
         span > < /div > ";
     } else {
         $msg = "<div class='alert-box warning'><span>Registration cancelled! Please signup again if you create again!</span> < /
         div > ";
     }

     ///
 }
 //Cancel button end
coder
  • 8,346
  • 16
  • 39
  • 53
Atif
  • 3
  • 7
  • 1
    What are you expecting to happen and what does happen? `0` rows deleted will be `true` because it isn't an error. You also should parameterize your query. – user3783243 Jun 18 '18 at 04:16
  • When I hit button then delete row based by query. It's good and working . But my Issue Is show else or error msg when user already deleted that row and when he again hit button then he will show error msg that like this `you have already done this action` might that's why I uses else.. – Atif Jun 18 '18 at 04:26
  • 1
    see this https://stackoverflow.com/questions/922398/what-does-a-successful-mysql-delete-return-how-to-check-if-delete-was-successfu – Andrey Vorobyev Jun 18 '18 at 05:19
  • Not helpful.. could u help pls share your code – Atif Jun 18 '18 at 05:22
  • use mysqli_affected_rows($con); – rawathemant Jun 18 '18 at 05:23
  • If you've read what Andrey posted you'll know that it is helpful. Do a `var_dump($con->query($sql)); die;` before `if ($con - > query($sql) === TRUE) {` and tell us what you see. – hungrykoala Jun 18 '18 at 06:03
  • That code will give "PHP Parse error: syntax error, unexpected '>' because you miswrote the `T_ARROW` as `- >` when it has to be `->`. – Gordon Jun 18 '18 at 06:23
  • @Gordon Looks like that was just a bad edit, https://stackoverflow.com/posts/50902506/revisions. – user3783243 Jun 18 '18 at 12:51

2 Answers2

0

You can check, Is record delete or not.

if (isset($_POST['cncl'])) {
     $result = $con->query("DELETE FROM users WHERE Email_ID='$email' AND token='$token' AND isEmailConfirmed='PENDING' AND isMobileConfirmed='PENDING'");
     ///     
     if ((mysql_affected_rows($con) > 0) ) {
         $msg = "<div class='alert-box success'><span>Registration cancelled. < /
         span > < /div > ";
     } else {
         $msg = "<div class='alert-box warning'><span>Registration cancelled! Please signup again if you create again!</span> < /
         div > ";
     }

if it will return value more than 0 then record deleted or if it will return 0 then record not delete.

rawathemant
  • 744
  • 1
  • 4
  • 16
  • Error when use your script shows `Fatal error: Uncaught Error: Call to undefined function mysql_affected_rows()` – Atif Jun 18 '18 at 06:18
  • Which connection method you are using? mysql, mysqli or pdo? – rawathemant Jun 18 '18 at 06:22
  • Where from you learn yaar..Tell me the school name..How I get admission there? In india..Looks you are from india. – Atif Jun 18 '18 at 06:25
  • Yes i'm Indian. I'm not learn this from school, Is this answer is helpful for you? – rawathemant Jun 18 '18 at 06:29
  • No.bexiuse when I update this error is arrive here please look https://pastebin.com/Gr3wmZpw my pastebin here here at last `else` not working and show link expire alert only. Show other message when user delete the row and when he click submit button show error message instead `session expired` – Atif Jun 18 '18 at 06:44
  • @rawathemant `mysql_*` functions should not be suggested/recommended anymore. If they are needed a note should be added that it is likely to not work in the future. – user3783243 Jun 18 '18 at 12:52
  • You can use mysqli_* instead of it. – rawathemant Jun 18 '18 at 13:14
0

Using mysqli->query() with a SELECT statement returns an instance of mysqli_result. It is not identical to true (=== true), but nor does it represent an error.

Moreover, $result is undefined.you can try this

if (isset($_POST['cncl'])) {
     $sql = "DELETE FROM users WHERE Email_ID='$email' AND token='$token' AND isEmailConfirmed='PENDING' AND isMobileConfirmed='PENDING'";
     ///     
     if (($result = $con->query($sql)) !== FALSE) {
         $msg = "<div class='alert-box success'><span>Registration cancelled. < /
         span > < /div > ";
     } else {
         $msg = "<div class='alert-box warning'><span>Registration cancelled! Please signup again if you create again!</span> < /
         div > ";
     }

     ///
 }
Kishan Oza
  • 1,707
  • 1
  • 16
  • 38