2

I have represented data in tabular form with no problem. But when i click the delete button i want the $id i.e. row[0] of my database to pass to another page delete_user_post.php where i can capture this value of $id using $_REQUEST. When i echo $id it shows its associated id. But i am not able to pass that value in next page.what have i done wrong?? `

<tbody>
        <?php while($row = mysql_fetch_array($result)){
        ?>

          <tr>  
            <td><?php echo $row[0] ?></td>  
            <td><?php echo $row[1] ?></td>  
            <td><?php echo $row[2] ?></td>
            <td>
                <div class="control-group">
                <label class="control-label"></label>
                <div class="controls"> 

                <?php $id = $row[0];
                echo $id;
                ?>

                <a href="delete_user_post.php?roomid=$id" class="btn btn-success" type="button">Delete</a> 
                </td>   



          </div>
          </tr>  
          <?php } ?>
</tbody>

My user_delete_post.php looks like this:

<?php
      include ('include/DB_Functions.php');      
      $id =$_REQUEST['roomid'];     
      // sending query
      mysql_query("DELETE FROM room_tb WHERE roomID=$id") or die(mysql_error());
?>
3
  • 2
    sidenote: stop using deprecated mysql_*() functions. Use PDO and MySQLi instead.
    – Raptor
    Aug 14, 2013 at 5:03
  • 1
    Is it just me or you're not really printing the $id in the link's href attribute? use the <?php ?> tags and echo it properly. You should also avoid just taking request parameters from the user and perform SQL commands with it. It makes a security opening called SQL Injection. For further reading: php.net/manual/en/security.database.sql-injection.php
    – Itay
    Aug 14, 2013 at 5:04
  • On a side note, since you are explicitly supplying the variables to the target page in the URL, consider using GET to retrieve them in delete_user_post.php, instead if REQUEST. The REQUEST method hunts for the variable in the GET, POST as well as Cookie arrays and can have unwanted repercussions which may be hard to debug, in case a variable similar to $id is present in any of those. Aug 14, 2013 at 6:41

2 Answers 2

5

It looks like you're not actually echoing the $id.

Replace your a tag with this:

<a href="delete_user_post.php?roomid=<?php echo $id; ?>" class="btn btn-success" type="button">Delete</a>

Also please look up mysqli_ or PDO as a replacement for mysql_

Also think long and hard about if you really want unescaped and unverified IDs to be deleted from your database.

1
  • You should be mentioning PDO to give the OP a choice
    – jeremy
    Aug 14, 2013 at 5:03
2

Try to put $id in php tags like,

<a href="delete_user_post.php?roomid=<?php echo $id; ?>" class="btn btn-success" type="button">Delete</a> 
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.