-2

this is my HTML code:

    <!doctype html>
    <html lang="fr">
    <head>
    <meta charset="utf-8">
    <title>supprimer un moyen</title>
    <link rel="stylesheet" href="style.css">
    </head>
    <body class="example">
    <br><br><br>
    <form name="f" method="post" action=" supprimer_moyen.php ">
    <center><b><h1>supprimer un moyen</h1> </b>
    <label> &nbsp  Entrer le code du moyen :   </label>
    <input type="text" name="code_moy" value="" placeholder="Code" required> 
    &nbsp  <input id="gobutton" type="submit" name="sup" value="Supprimer"><br><br>
    </center>
    </form>
    </body>
    </html>

and this is my php code:

<?php

if (isset ($_POST['sup']))
{
    $code_moy= $_POST["code_moy"];
    $con=mysql_connect("localhost","root","") or die("Echec de connexion au serveur.".mysql_error()); 
    mysql_select_db("ttp",$con) or die("Echec de sélection de la base.".mysql_error());
    $sql = "delete from moyen_transport where ID='$code_moy'";

    if (mysql_query($sql))
        {
         echo '<br>';   
         echo '<h1><center><font color="white"> suppression avec succès <font></center> </h1>';
         echo '<center><a href="supprimer_moyen.html" ><img src="images/precedent-icone-5823-128.png" width="120px" height="70px" alt="Accueil"></a></center>';
    }
    else
    {       
            echo '<br>';
            echo '<h1><center><font color="red"> ce moyen n\'&eacutexiste pas <font></center> </h1> ';
            echo '<center><a href="supprimer_moyen.html" ><img src="images/precedent-icone-5823-128.png" width="120px" height="70px" alt="Accueil"></a></center>';
    }
    mysql_close();
}   
?>

My problem is that no matter what i enter in input the php result is always "suppression avec succès" even if the "ID" dosen't exist in my database!!!

  • There is always a result returned, it may not have anything in it, but there is a result. You need to check if any rows were returned. – yaakov Apr 13 '17 at 22:40
  • 1
    Actually, check rows affected. A delete will not return any rows. – Sloan Thrasher Apr 13 '17 at 22:44
  • 3
    Also, mysql functions are depreciated. Use PDO or mysqli functions instead. – Sloan Thrasher Apr 13 '17 at 22:44
  • 1
    You are using unsecure, unmaintained, and deprecated code. Alternatives like PDO have been available for more than 10 years. There's [no excuse for this any longer](http://stackoverflow.com/q/12859942/1255289)! – miken32 Apr 13 '17 at 22:44
  • As a Side note: you should **Stop** using mysql_ functions and instead use `mysqli_` functions. They're much safer, and are not Deprecated, as mysql_ functions are. – Martin Apr 13 '17 at 22:45
  • Thank you all for your answers, i'll try to use mysqli and i'll keep you posted... – Raddadi Mohamed Apr 13 '17 at 23:01

2 Answers2

0

First try using mysqli second you can use mysqli_num_rows like that

<?php
if (isset ($_POST['sup'])){
$code_moy= $_POST["code_moy"];
$con=mysqli_connect("localhost","root","","ttp") or die("Echec de connexion au serveur.".mysqli_error($con)); 
$sql = "select * from moyen_transport where ID='$code_moy'";
$query = mysqli_query($con,$sql);
if(mysqli_num_rows($query) == 0){
//No id found
    echo '<br>';
    echo '<h1><center><font color="red"> ce moyen n\'&eacutexiste pas <font></center> </h1> ';
    echo '<center><a href="supprimer_moyen.html" ><img src="images/precedent-icone-5823-128.png" width="120px" height="70px" alt="Accueil"></a></center>';
}else{
//id found
$sql = "delete from moyen_transport where ID='$code_moy'";
$query = mysqli_query($con,$sql)or die(mysqli_error($con));
echo '<br>';   
 echo '<h1><center><font color="white"> suppression avec succès <font></center> </h1>';
 echo '<center><a href="supprimer_moyen.html" ><img src="images/precedent-icone-5823-128.png" width="120px" height="70px" alt="Accueil"></a></center>';}}
?>
  • thanks a lot .. this solve the problem just there is a missing semicolon on line 5 ! $sql = "select * from moyen_transport where ID='$code_moy'"; – Raddadi Mohamed Apr 13 '17 at 23:50
-1

Try checking if the returned value is empty

$result = mysql_query($sql);
if(!empty($result)){
//success code here
} else {
//fail code here
}
Reed Raymond
  • 223
  • 2
  • 12