Hi webmasters,
I would like to start PHP with mysql with a simple example by my own , so i made the following that tries to edit and remove a row from a table.
the table is named: shop
the database: test2
fields: article,dealer,price
So, I want to have the row displayed and beside every row to have a checkbox (not checked).
There will be two additional buttons below the rows that will display EDIT and DELETE (row).
I started with this:
<html>
<head>
<title>::: EDIT DATABASE :::</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#999999">
PHP Code:
<?php
$connection = mysql_connect("localhost", "", "");
mysql_select_db("test2");
{
$result = mysql_query("SELECT article,dealer,price FROM shop");
$num_rows = mysql_num_rows($result);
$itemchoice = 0;
if (($row = mysql_fetch_array($result)) != 0)
{
echo "<table border=0>";
echo "<tr bgcolor=#cccccc>
<td align=center>Article</td>
<td align=center>Dealer</td>
<td align=center>Price</td>
</tr>";
do
{
$name = $row['article'];
$owner = $row['dealer'];
$species = $row['price'];
$itemchoice = $itemchoice + 1;
echo "<form method='post' action='<? $PHP_SELF ?>'>
<tr>
<td align='center'>
<input disabled type='text' name='article' value='" . $name . "' size='30' maxlength='30'></td>
<td align='center'>
<input disabled type='text' name='dealer' value='" . $owner . "'></td>
<td align='center'>
<input disabled type='text' name='price' value='" . $species . "' size='10' maxlength='10'></td>
<td align='center'>
<input style='background-color:#CCFF33' type=checkbox name='item' id='$itemchoice'>
</tr></font>";
}
while ($row = mysql_fetch_array($result));
echo "<form method='post' action='<? $_POST[$PHP_SELF] ?>'>";
echo "<tr><td><input type='submit' name='delete' value='Delete'>";
echo "<input type='submit' name='edit' value='Edit'></td></tr></table></form>";
}
else
{
echo "No records found!";
}
}
if (isset($_REQUEST["delete"]);
$sql_del = mysql_query("DELETE FROM shop WHERE &itemchoice = ".$HTTP_POST_VARS['$item']);
}
if (!mysql_query ($sql_del)) {
$success = 0;
$msg = 'Record could not be deleted';
}
else {
$msg = 'Record for item '.$HTTP_POST_VARS['item'].' was deleted. Refreshing display...';
}
?>
</body>
</html>
This of course doesnt work ... so any help will be appreciated.! 
Last edited by nasos : 05-06-2004 at 06:11 AM.
|