Re: Delete rows from database

From: Date: Mon, 07 Jan 2008 08:59:13 +0000
Subject: Re: Delete rows from database
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-266833@lists.php.net to get a copy of this message
Balasubramanyam A wrote:
Hi all, I'm searching names from MySQL and printing it on a browser. Also, I've provided checkbox for all the rows and a delete button for a page. I want to delete the selected rows from MySQL when I click on the "Delete" button. How do I do that? Here is the code which I used to print the rows after fetching the same from MySQL while ($line = mysql_fetch_array($resultset, MYSQL_ASSOC)) {
        echo "\t<tr>\n";
        echo "<td><input type=checkbox name=index /></ td>";
In the above line you need to have the row ID as the value of your check box. Also, you should probably use and array in your form. See below for example and explanation. echo "<td><input type=checkbox name=delete[] value={$line['id']} /></ td>"; Ok, broken down... nothing special about this, pretty standard echo "<td><input type=checkbox This, when submitted to PHP, will create an array in the POST/GET array name=delete[] This will assign the value of ID to the checkbox array value={$line['id']} etc... Now, this is assuming that your id column is called ID and that you will be wanting to delete more then one at a time. on the process page, do a print_r($_POST) You will see what you get
        foreach ($line as $col_value) {
                echo "\t\t<td>&nbsp;$col_value</td>\n";
        }
        //echo "<td><input type=button name=vcancel value=Cancel /></td>";
        echo "\t</tr>\n";
}


« previous php.general (#266833) next »