Saturday, September 17, 2011

Simple Web Application – Delete Data line

Once again I should like to acknowledge the Virginia Tech Simple Web Application sample code, which provided a template for this little project, which has really helped to improve my practical understanding of PHP.

Having said that, the code, required to delete lines from the database, is pretty slim. Once again I bloated it out with some diagnostic HTML, just to check the query. I hate running blind.

The bloated code was as follows:

<?php
$id = $_GET['id'];
if (empty($id)) {
Header("Location: listcontacts.php");
exit;
}
include('dbinfo.php');// collect database variables and connect.
$con = mysql_connect( $dbhost, $dbuser, $dbpass );
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($dbname, $con);
$query = "DELETE FROM mytable WHERE Itemid=".$id;
$result = mysql_query($query) or die(mysql_error());
mysql_close($con); //close connection because job finished
// Header("Location: jsphp4.php"); //return to main display form
?>
<html>
<head>
<title>Delete item diagnostics</title>
</head>
<body>
<h1>Delete Item</h1>
<p>The item id is now: <?php echo $id ?> </p>
<p>The query is: <?php echo $query ?> </p>
</body>
</html>

And the page was as shown below:

After checking the query, the slimmed down “production” code was:

<?php
$id = $_GET['id'];
if (empty($id)) {
Header("Location: listcontacts.php");
exit;
}
include('dbinfo.php');// collect database variables and connect.
$con = mysql_connect( $dbhost, $dbuser, $dbpass );
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($dbname, $con);
$query = "DELETE FROM mytable WHERE Itemid=".$id;
$result = mysql_query($query) or die(mysql_error());
mysql_close($con); //close connection because job finished
Header("Location: jsphp4.php"); //return to main display form
?>

And of course there is no pic to show, because after the delete code has run we are returned to the main page.

In fact this whole exercise derived from a desire to check how that is done - modifying data in a database while (having the illusion of) remaining on the same page.

So now I have called a JavaScript function from an Applet, and I have called a PHP script, which has modified data in a database, from a button on a web page.

My next step is to call a PHP script from an Applet.