0

My website will create a table from a MySQL database. I was wondering if their was a way to have a delete button next to each row so members can delete specific rows on their table. Everything else works fine. I would like users to be able to delete their rows from their table.

<form action="addcel.php" method="post">
Name <input type="text" name="Name"/>
Year <input type="text" name="year"/>
<input type="submit" />

<html><head><title>MySQL Table Viewer</title></head><body>
<?php
echo "<style type = 'text/css'>

table {width:700px;border:none;text-align:center;background-color:#B0B0B0;font-family:'Arial';}

#tabledata {padding:2px;border-width:0px;}

#tableheader {border-width:0px;}

#line {border:1px solid black;padding:0px;}

</style>";
$db_host = 'localhost';
$db_user = 'root';
$db_pwd = '';

$database = 'Name_gage';
$table = 'Name';

if (!mysql_connect($db_host, $db_user, $db_pwd))
    die("Can't connect to database");

if (!mysql_select_db($database))
    die("Can't select database");

// sending query
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
    die("Query to show fields from table failed");
}

$fields_num = mysql_num_fields($result);

echo "<h1>Table: {$table}</h1>";
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
    $field = mysql_fetch_field($result);
    echo "<th id = 'tableheader'>{$field->name}</th>";
}
echo "</tr>\n";
echo "<tr><td id = 'line'></td id = 'line'><td id = 'line' ></td><td id = 'line' ></td><td id = 'line' ></td><td id = 'line' ></td></tr>";
// printing table rows
while($row = mysql_fetch_row($result))
{
    echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell)
        echo "<td id = 'tabledata'>$cell</td>";

    echo "</tr>\n";
}
mysql_free_result($result);
?>
</body></html>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user1175551
  • 231
  • 1
  • 4
  • 6
  • 1
    Are you saying they should be able to delete the HTML table row, or the database row that generated it, or both? – freefaller Jun 18 '12 at 17:19

3 Answers3

1

You could put a form in each row to call a delete script given the row id.

while($row = mysql_fetch_row($result))
{
    echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell)
        echo "<td id = 'tabledata'>$cell</td>";
    echo "<form method='post' action='delete.php'><input type='hidden' name='id' value='$row[id]'/><input type='submit' value='Delete'/></form>";
    echo "</tr>\n";
}
Musa
  • 96,336
  • 17
  • 118
  • 137
  • Thanks, but when ever I try this it gives me the error: Notice: Undefined index: id in C:\xampp\htdocs\hi\index.php on line 60. Line 60 is were I have echo "
    "; Could you please help thanks
    – user1175551 Jun 18 '12 at 22:00
  • @user1175551 replace `$row[id]` with `$row[table_primary_key]` where table_primary_key is the primary key for the table, you will also need a delete.php to delete the row in the database and maybe redirect to index.php. – Musa Jun 18 '12 at 22:03
0

Take a look at this stackoverflow question which is similar:

Jquery Ajax remove rows from table and in db

The above link references and explains how to use Ajax to achieve the desired results.

If you wish, you could always create an onClick function for the delete row button which will link the user to a delete row page, and then refresh the user back to the table page. This is how they did it before AJAX, but would be a less seamless approach.

Community
  • 1
  • 1
Mike Mackintosh
  • 13,917
  • 6
  • 60
  • 87
0

That requires ajax call. when your user clicks delete button of a particular html table row, you will need to do a ajax call to the server and, in server side, delete that particular row from database.

thavan
  • 2,409
  • 24
  • 32