0

Hello every one i have probleme with my form of deliting users , when i submit the button nothing happend what is the problem , everything work on the db when i fetch infos they come but when i click on delete nothing happend

  <?php



        $host ="localhost";
        $dbname ="justnew";
        $u_name="root";
        $u_pass="youcef02";

        echo'
             <table border="1px solid #efefef" width="42%">
          <tr>
          <th>ID</th>
            <th>Name</th>
            <th>Password</th>
        <th>DELETE</th>
          </tr>


        ';
        try{
        $Conn = new PDO("mysql:host=$host;dbname=$dbname",$u_name,$u_pass);

        }

        catch(PDOEXCEPTION $e){
        echo'There is a prblm' .$e->getMessage();

        }

        $sql = "SELECT * FROM addu";
        $result = $Conn->query($sql);

        while($row = $result->fetch(PDO::FETCH_OBJ)) {
        echo"
          <tr>
          <td>" .$row->u_id."</td>
            <td>" .$row->u_name."</td>
            <td>" .$row->u_pass."</td>
             <td><button name='dlt'><a href='attribute.php?
           type=dlt&u_id=".$row->u_id."' name='dlt'>DELETE</a></button></td>
          </tr>
        ";
        }


        if($_GET['type'] == ['dlt']){

        $id = intval ($_GET['u_id']);


        $Dsql = "DELETE * FROM `addu` WHERE `u_id` ='".$id."'";
            $Dresult = $Conn->exec($Dsql);



        }
        ?>        
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • 2
    `if($_GET['type'] == ['dlt'])` should be throwing an error. You need to add error checking to the whole thing. – Jay Blanchard Jan 16 '18 at 21:18
  • Remove the `*` in delete. Also `dlt` is string not array – Rotimi Jan 16 '18 at 21:18
  • @JayBlanchard https://stackoverflow.com/questions/23315022/delete-query-not-working-in-mysql if you wish. – Funk Forty Niner Jan 16 '18 at 21:41
  • what's the datatype of the u_id column in your database? int or varchar? If it's int (which I suspect since you're parsing the $_GET variable as an int) then remove the single quotes round it in the query, otherwise mysql thinks it's a string and it won't match with an int value in the column, therefore it'll find no rows to delete. If you'd used parameterised queries (which you really should) this kind of problem would simply not occur. – ADyson Jan 16 '18 at 21:43

1 Answers1

1

Seeing nobody wanted to post an answer for this, I am submitting the following.

As stated: if($_GET['type'] == ['dlt']) is invalid. The brackets around the dlt need to be removed and would have thrown an syntax error.

  • if($_GET['type'] == 'dlt')

You should however, check if the GET array is set/not empty though, since without it, that too will throw an undefined index warning having error reporting set on your system.

if(isset($_GET['type']) && $_GET['type'] == 'dlt')

or

if(!empty($_GET['type']) && $_GET['type'] == 'dlt')

Then the DELETE'ing part of your query is also invalid.

It should read as:

$Dsql = "DELETE FROM `addu` WHERE `u_id` ='".$id."'";

The asterisk is only valid with a SELECT statement:

Having used (PDO) error handling, you'd of surely gotten back an syntax error:

Now this bit could have adverse effect:

<td><button name='dlt'><a href='attribute.php?
           type=dlt&u_id=".$row->u_id."' name='dlt'>DELETE</a></button></td>
          </tr>
";

It's (usually) best to have this all in one line:

<td><button name='dlt'><a href='attribute.php?type=dlt&u_id=".$row->u_id."'>DELETE</a></button></td>
          </tr>
        ";

The added spaces could count here.

Note: You used the name attribute twice.

If the above fails, then the <a href></a> shouldn't be inside a <button></button>, just use the <a href></a>.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141