Simple Web Application – Add Data Form

While I must still acknowledge the Virginia Tech Simple Web Application sample code, at this stage in the project I was primarily cutting and pasting from the code shown in my previous posts, and manually making a few small changes from the sample code.

The code used in the Add data line form is very similar to that for the Edit data line form, except that there is no existing data to display, and the SQL command is INSERT rather than UPDATE.

I also removed the input validation code. Any data entered manually into my database is bogus and must be deleted after this exercise has finished, so it really doesn't matter what fields are filled or left empty.

With no data to display, there no need to confirm that a data line was being correctly passed from the main display form, and no need to confirm that the line was being correctly read. I therefore left in just a couple of diagnostic messages at the top of the form:

<p>The query is: <?php echo $query ?> </p>
<p>Status is: <?php echo $jonathan ?> </p>

Of course to view these lines after clicking the add button, it is necessary to comment out the instruction to return to the main form right after adding data as follows:

// Header("Location: jsphp4.php"); //return to main display form

When everything is working the comment strokes (on the left) can be removed, and the diagnostic lines can be removed from the HTML.

My working (diagnostic) code was as follows:

<?php
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);
if (isset($_POST['addcontact'])) { // run this when the user hits the "Add Contact" button
$jonathan="Add loop";
$Partid = $_POST['Partid'];
$OpCode = $_POST['OpCode'];
$Itemdet = $_POST['Itemdet'];
$Raw = $_POST['Raw'];
$Rate = $_POST['Rate']; // input validation removed from under here
$query = "INSERT INTO mytable (Partid, OpCode, Itemdet, Raw, Rate) VALUES (".$Partid.",".$OpCode.",'".$Itemdet."',".$Raw.",".$Rate.")";
$result = mysql_query($query) or die(mysql_error()); //adds form inputs
mysql_close($con); //close connection because job finished
Header("Location: jsphp4.php"); //return to main display form
}
?>
<html>
<head>
<title>Add a Line</title>
</head>
<body>
<h1>Add a Line</h1>
<p>The query is: <?php echo $query ?> </p>
<p>Status is: <?php echo $jonathan ?> </p>
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table style="text-align:right;">
<tr><td>Partid:<input name="Partid" type="text" value="<?php echo $Partid; ?>"></td></tr>
<tr><td>Opcode:<input name="OpCode" type="text" value="<?php echo $OpCode; ?>"></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="submit" name="addcontact" value="Add Line"></td></tr>
</table>
</form>
<p><a href="jsphp4.php">Click here to return to List</a></p>
</body>
</html>

And after clicking the Add Line button, the page, with diagnostic code, was as shown below:

After removing the diagnostic code the page came up as shown below:

Comments

Dubai said…
Thanks for your tutorial. I learned lots from here like a e-book. Keep up your work.
Google android apps development| IPhone App Development| Android apps developer|

Popular posts from this blog

A few notes on JavaScript

Forum Comments on Java Applets

Creating a Custom Swing Component