July 25, 2010

From where do i pass id to delete a record?

Question by TCM

How do i pass id to delete record in this code?

<form action="index.php">
        <?php
                     mysql_connect('localhost', 'root', '');
            mysql_select_db('user');
            $query = mysql_query("Select * from tbluser");
            echo "<center>";
            echo '<table style="border:solid 2px black;">';
            while(($row = mysql_fetch_array($query)) != NULL) {
                echo '<tr>';
                echo '<td>' . $row['UserName'] . '</td>';
                echo '<td>' . $row['Password'] . '</td>';
                echo '<td>' . $row['EmailAddress'] . '</td>';
                echo '<td>' . $row['Address'] . '</td>';
                echo '<td>' . $row['Address'] . '</td>';
                echo '<td><input type = "Submit" value = "Delete" name = "btnDel" /></td>';
                echo '</tr>';
            }
            echo '</table>';
            echo "</center>";
        ?>
          </form>

The above code is in index.php and it is submitting it to itself.

Answer by Wrikken

Without needing javascript, seperate GET urls etc, just plain old HTML & the original POST: just add the ID to the name of the button:

<input type="submit" value="Delete" name="btnDel[<?php echo $id;?>]">

And in receiving code:

if(isset($_POST['btnDel']) && is_array($_POST['btnDel'])){
    foreach($_POST['btnDel'] as $id_to_delete => $useless_value){
        //delete item with $id_to_delete
    }
}

Answer by Starx

Use this to submit the id as a part of form.

<input type="hidden" id="id" name="id" value="<? echo $row['id']; ?>" />

or you can send values in URL to do the same thing

An example:

<a href="index.php?action=delete&id=<? echo $row['id']; ?>">Delete</a>

A full working sample

<?
    mysql_connect('localhost', 'root', '');
    mysql_select_db('user');
    switch($_GET['action']) { 
        case "delete":
            $query = "DELETE FROM tbluser WHERE id='".$_GET['id']."'"
            $result = mysql_query($query);
            break;
        //well other actions
    }

    $query = mysql_query("Select * from tbluser");
    echo "<center>";
    echo '<table style="border:solid 2px black;">';
    while(($row = mysql_fetch_array($query)) != NULL) {
        echo '<tr>';
        echo '<td>' . $row['UserName'] . '</td>';
        echo '<td>' . $row['Password'] . '</td>';
        echo '<td>' . $row['EmailAddress'] . '</td>';
        echo '<td>' . $row['Address'] . '</td>';
        echo '<td>' . $row['Address'] . '</td>';
        echo '<td><a href="thispage.php?action=delete&id='.$row['id'].'">Delete</a></td>';
        echo '</tr>';
    }
    echo '</table>';
    echo "</center>";
?>

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!