Thursday, September 15, 2011

Simple Web Application – Data edit form

The Virginia Tech Simple Web Application sample code works perfectly on their server, but as I reported in my previous post, getting something similar to run with my own database has been a fairly long and frustrating task.

I said in my last post that I constructed my own PHP PEAR database layer, as a result of which, my code and the sample code diverged from an early point. Specifically:

include('dbconnect.php');

was stretched out to:

include('dbinfo.php');
$con = mysql_connect( $dbhost, $dbuser, $dbpass );
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($dbname, $con);

I had some specific problems with certain lines of the sample code. One was:

$result = $database->query($query);

I didn’t understand the funny little -> in this line (I think it might have something to do with Perl, which takes it beyond the scope of this post), nor the general syntax, so I replaced it with:

$result = mysql_query($query) or die(mysql_error());

Another example was

$contact = $result->fetchRow(DB_FETCHMODE_ASSOC,0);

Which I replaced with

$contact = mysql_fetch_row($result);

I also replaced the text references to array members:

$Itemid = $contact['Itemid'];

With array member indices:

$Itemid = $contact[0];

Getting the form to work at all was a long slow process, so I added a series of diagnostic variables displayed in notes above the form. For example:

$jonathan="Default loop";

These enabled me to track which branches of code were executing, and which variables were being properly filled.

Further divergence derived from differences in the style of the data. The sample code set out to teach among other things the different methods of entering personal data on a form (radio buttons etc.), whereas my data is mainly numeric. This first required me modify the SQL to accommodate my data types. And on the cosmetic side, because my data is uniform in style, I wanted a uniform look. I therefore put my data entry fields into a table, whereas the sample code used a lot of line breaks.

After all this, my code was as follows:

<?php
// get contact id
$id = $_GET['id'];
if (!empty($id)) { $_SESSION['recordId']=$id; }
else { $id = $_SESSION['recordId']; }
// if the script has been called without ID or the user hit "Cancel" just return to listing
if (empty($id) || isset($_POST['cancel'])) {
Header("Location: jsphp4.php");
exit;
}
include('dbinfo.php');
$con = mysql_connect( $dbhost, $dbuser, $dbpass );
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($dbname, $con);
$query = "SELECT * FROM mytable WHERE Itemid=".$id;
$result = mysql_query($query) or die(mysql_error());
$contact = mysql_fetch_row($result);
$Itemid = $contact[0];
$OpCode = $contact[1];
$Partid = $contact[2];
$Itemdet = $contact[3];
$Raw = $contact[4];
$Rate = $contact[5];
mysql_close($con);
?>
<html>
<head>
<title>Edit Items Table</title>
</head>
<body>
<a href="jsphp4.php">Add a Line</a> | <a href="jsphp4.php">Session Data</a>
<h1>Edit Item</h1>
<p>The item id is now: <?php echo $id ?> </p>
<p>The query is: <?php echo $query ?> </p>
<p>The op code is: <?php echo $query ?> </p>
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table style="text-align:right;">
<tr><td>Itemid:<input name="Itemid" type="text" value="<?php echo $Itemid; ?>"></td></tr>
<tr><td>Opcode:<input name="OpCode" type="text" value="<?php echo $OpCode; ?>"></td></tr>
<tr><td>Partid:<input name="Partid" type="text" value="<?php echo $Partid; ?>"></td></tr>
<tr><td>Itemdet:<input name="Itemdet" type="text" value="<?php echo $Itemdet; ?>"></td></tr>
<tr><td>Raw:<input name="Raw" type="text" value="<?php echo $Raw; ?>"></td></tr>
<tr><td>Rate:<input name="Rate" type="text" value="<?php echo $Rate; ?>"></td></tr>
<tr><td><input type="hidden" name="id" value="<?php echo $id; ?>">
<tr><td><input type="submit" name="ok" value=" OK ">
<tr><td><input type="submit" name="cancel" value="Cancel">
</table>
</form>
</body>
</html>

And it looked like this:

No comments: