Notice: Undefined index using (isset($_POST['submit']))
Posted: Sun Oct 04, 2015 10:33 am
Code: Select all
<?php
/*
NEW.PHP
Allows user to create a new entry in the database
*/
// creates the new record form
// since this form is used multiple times in this file, I have made it a function that is easily reusable
function renderForm($location, $department, $catnumber, $description, $hostname, $ipaddress, $macaddress, $os, $hpprodnum, $hpsn, $startdate, $enddate, $error)
{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Tech Workstations</title>
<link rel="stylesheet" type="text/css" href="../css/basic.css" />
</head>
<body>
<h2>Add a new Workstations</h2>
<?php
// if there are any errors, display them
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<form action="" method="post">
<div>
<strong>Location: *</strong> <input type="text" name="Location" value="<?php echo $location; ?>" /><br/>
<strong>Department: *</strong> <input type="text" name="Department" value="<?php echo $department; ?>" /><br/>
<strong>Catalog Number: *</strong> <input type="text" name="Catalog_Number" value="<?php echo $catnumber; ?>" /><br/>
<strong>Description: *</strong> <input type="text" name="Description" value="<?php echo $description; ?>" /><br/>
<strong>Host Name: *</strong> <input type="text" name="Hostname" value="<?php echo $hostname; ?>" /><br/>
<strong>IP Address: *</strong> <input type="text" name="IP_Address" value="<?php echo $ipaddress; ?>" /><br/>
<strong>MAC Address: *</strong> <input type="text" name="MAC_Address" value="<?php echo $macaddress; ?>" /><br/>
<strong>Operating System: *</strong> <input type="text" name="OS" value="<?php echo $os; ?>" /><br/>
<strong>HP Product Number: *</strong> <input type="text" name="HP_PROD_Number" value="<?php echo $hpprodnum; ?>" /><br/>
<strong>HP Serial Number: *</strong> <input type="text" name="HP_S/N" value="<?php echo $hpsn; ?>" /><br/>
<strong>Manufacturing Date: *</strong> <input type="text" name="Start_Date" value="<?php echo $startdate; ?>" /><br/>
<strong>Warranty End Date: *</strong> <input type="text" name="End_Date" value="<?php echo $enddate; ?>" /><br/>
<p>* required</p>
<input type="submit" name="submit" value="Submit">
</div>
</form>
</body>
</html>
<?php
}
// connect to the database
include('connect-db.php');
// check if the form has been submitted. If it has, start to process the form and save it to the database
if (isset($_POST['submit']))
{
// get form data, making sure it is valid
$location = mysqli_real_escape_string($connection, $_POST['location']);
$department = mysqli_real_escape_string($connection, $_POST['department']);
$catnumber = mysqli_real_escape_string($connection, $_POST['catnumber']);
$description = mysqli_real_escape_string($connection, $_POST['description']);
$hostname = mysqli_real_escape_string($connection, $_POST['Hostname']);
$ipaddress = mysqli_real_escape_string($connection, $_POST['ipaddress']);
$macaddress = mysqli_real_escape_string($connection, $_POST['macaddress']);
$os = mysqli_real_escape_string($connection, $_POST['os']);
$hpprodnum = mysqli_real_escape_string($connection, $_POST['hpprodnum']);
$hpsn = mysqli_real_escape_string($connection, $_POST['hpsn']);
$startdate = mysqli_real_escape_string($connection, $_POST['startdate']);
$enddate = mysqli_real_escape_string($connection, $_POST['enddate']);
// check to make sure both fields are entered
if ($location == '' || $department == '' || $catnumber == '' || $description == '' || $hostname == '' || $ipaddress == '' || $macaddress == '' || $os == '' || $hpprodnum == '' || $hpsn == '' || $startdate == '' || $enddate == '')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';
// if either field is blank, display the form again
renderForm($location, $department, $catnumber, $description, $hostname, $ipaddress, $macaddress, $os, $hpprodnum, $hpsn, $startdate, $enddate, $error);
}
else
{
// save the data to the database
mysqli_query($connection, "INSERT workstations SET location='$location', department='$department', catnumber='$catnumber', description='$description', hostname='$hostname', ipaddress='$ipaddress', macaddress='$macaddress', os='$os', hpprodnum='$hpprodnum', hpsn='$hpsn', startdate='$startdate', enddate='$enddate'")
or die(mysql_error());
// once saved, redirect back to the view page
header("Location: view.php");
}
}
else // if the form hasn't been submitted, display the form
{
renderForm('','','','','','','','','','','','','');
}
?>