Reputation: 241
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>
Upvotes: 0
Views: 3621
Reputation: 14237
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.
Upvotes: 0
Reputation: 2429
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.
Upvotes: 0
Reputation: 97672
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";
}
Upvotes: 1