php
// Display search form
include $_SERVER['DOCUMENT_ROOT'] . '/includes/inc_db_ciq_connection.php';
// Get the basic data from the type table
$result = mysqli_query($link, 'SELECT id, type FROM type');
if (!$result)
{
$error = 'Error fetching type from database!';
include '../errorfetchingbasics.html.php';
exit();
}
while ($row = mysqli_fetch_array($result))
{
$types[] = array('id' => $row['id'],'type' => $row['type']);
}
// Get the basic data from the company status table
$result = mysqli_query($link, 'SELECT id, compstatus FROM compstatus');
if (!$result)
{
$error = 'Error fetching compstatus from database!';
include '../errorfetchingcompstats.html.php';
exit();
}
while ($row = mysqli_fetch_array($result))
{
$compstatuss[] = array('id' => $row['id'],'compstatus' => $row['compstatus']);
}
include 'searchform.html.php';
// The basic SELECT statement
if (isset($GET['action']) and $GET['action'] == 'search')
{
include $_SERVER['DOCUMENT_ROOT'] . '/includes/inc_db_ciq_connection.php';
$select = 'SELECT id, name';
$from = ' FROM company';
$WHERE = 'WHERE TRUE';
// Checking to see if a type was specified in the search querry
$type_id = mysqli_real_escape_string($link, $_GET['type']);
if ($type_id != '') // A type is selected
{
$where .= " AND typeid = '$typeid'";
echo $type_id;
}
// Note: If there are many to many situations between tables, you must create inbetween tables and use joins to link them together. There will be two extra blocks of code per check(type and company status)
// Checking to see if a company status was specified in the search querry
$compstatusid = mysqli_real_escape_string($link, $_GET['compstatus']);
if ($compstatusid != '') // A company status is selected
{
$where .= " AND compstatusid = '$compstatusid'";
echo $compstatusid;
}
// Checking to see if the text of a company was specified in the search querry
$text = mysqli_real_escape_string($link, $GET['text']);
if ($text != '') // Some search text was specified
{
$where .= " AND name LIKE '%$text%'";
include $SERVER['DOCUMENT_ROOT'] . '/includes/inc_db_ciq_connection.php';
}
// Retrieve the results of our querry and display the results
$result = mysqli_query($link, $select . $from . $where);
if (!$result)
{
$error = 'Error fetching companies.';
include '../errorfetchingquerry.html.php';
exit();
}
while ($row = mysqli_fetch_array($result))
{
$companies[] = array('id' => $row['id'], 'text' => $row['name']);
}
include 'companies.html.php';
exit();
}
// Display the blank form when add new company is clicked
include_once $SERVER['DOCUMENT_ROOT'] .'/includes/magicquotes.inc.php';
if (isset($GET['add']))
{
$pagetitle = 'New company';
$action = 'addform';
$text = '';
$id = '';
$type_id = '';
$compstatusid = '';
$address = '';
$city = '';
$state = '';
$zip = '';
$signatory = '';
$title = '';
$button = 'Add company';
include $_SERVER['DOCUMENT_ROOT'] . '/includes/inc_db_ciq_connection.php';
// Build the list of company types
$sql = "SELECT id, type FROM type";
$result = mysqli_query($link, $sql);
if (!$result)
{
$error = 'Error fetching list of types.';
include '../errorfetchinglistoftypes.html.php';
exit();
}
while ($row = mysqli_fetch_array($result))
{
$types[] = array(
'id' => $row['id'],
'type' => $row['type']);
}
// Build the list of company status
$sql = "SELECT id, compstatus FROM compstatus";
$result = mysqli_query($link, $sql);
if (!$result)
{
$error = 'Error fetching list of company status.';
include '../errorfetching.listofcompstat.html.php';
exit();
}
while ($row = mysqli_fetch_array($result))
{
$compstatus[] = array(
'id' => $row['id'],
'compstatus' => $row['compstatus']);
// note: we must add this code if we had one to many situation: 'selected' => FALSE);
}
include 'form.html.php';
exit();
}
// If the user clicks on edit company we must build the form with the populated data to edit
if (isset($POST['action']) and $POST['action'] == 'Edit')
{
include $SERVER['DOCUMENT_ROOT'] . '/includes/inc_db_ciq_connection.php';
$id = mysqli_real_escape_string($link, $POST['id']);
$sql = "SELECT id, name, type_id, compstatusid, address, city, state, zip, signatory, title From company WHERE id ='$id'";
$result = mysqli_query($link, $sql);
if (!$result)
{
$error = 'Error fetching company details.';
include '../errorfetchingcompdetails.html.php';
exit();
}
$row = mysqli_fetch_array($result);
$pagetitle = 'Edit company';
$action = 'editform';
$id = $row['id'];
$text = $row['name'];
$type_id = $row['type_id'];
$compstatusid = $row['compstatusid'];
$address = $row['address'];
$city = $row['city'];
$state = $row['state'];
$zip = $row['zip'];
$signatory = $row['signatory'];
$title = $row['title'];
$button = 'Update company';
// Build the list of types
$sql = "SELECT id, type FROM type";
$result = mysqli_query($link, $sql);
if (!$result)
{
$error = 'Error fetching list of types.';
include '../errorfetchinglistoftypes.html.php';
exit();
}
while ($row = mysqli_fetch_array($result))
{
$types[] = array('id' => $row['id'], 'type' => $row['type']);
}
// Build the list of company status
$sql = "SELECT id, compstatus FROM compstatus";
$result = mysqli_query($link, $sql);
if (!$result)
{
$error = 'Error fetching list of compstatus.';
include '../errorfetchinglistofstatus.html.php';
exit();
}
while ($row = mysqli_fetch_array($result))
{
$compstatus[] = array('id' => $row['id'], 'compstatus' => $row['compstatus']);
}
include 'form.html.php';
exit();
}
// This code processes the form submissions
if (isset($GET['addform']))
{
include $SERVER['DOCUMENT_ROOT'] . '/includes/inc_db_ciq_connection.php';
$text = mysqli_real_escape_string($link, $POST['name']);
$type = mysqli_real_escape_string($link, $POST['type']);
if ($type == '')
{
$error = 'You must choose a type for this company.
Click ‘back’ and try again.';
include '../errormustchoosetypeforcomp.html.php';
exit();
}
$compstatus = mysqli_real_escape_string($link, $_POST['compstatus']);
if ($compstatus == '')
{
$error = 'You must choose a company status for this company.
Click ‘back’ and try again.';
include '../errormustchoosecompanystatus.html.php';
exit();
}
$sql = "INSERT INTO company SET
name='$text',
type_id='$type_id',
compstatusid='$compstatusid',
address='$address',
city='$city',
state='$state',
zip='$zip',
signatory='$signatory',
title='$title'";
if (!mysqli_query($link, $sql))
{
$error = 'Error adding submitted company.';
include '../erroraddingsubmittedcompany.html.php';
exit();
}
header('Location: form.html.php');
exit();
}
// The form processing code for editing existing companies
if (isset($GET['editform']))
{
include $SERVER['DOCUMENT_ROOT'] . '/includes/inc_db_ciq_connection.php';
$text = mysqli_real_escape_string($link, $POST['text']);
$type = mysqli_real_escape_string($link, $POST['type']);
$compstatus = mysqli_real_escape_string($link, $POST['compstatus']);
$id = mysqli_real_escape_string($link, $POST['id']);
if ($type == '')
{
$error = 'You must choose a type for this company.
Click ‘back’ and try again.';
include '../errormustchoosetypeforcomp.html.php';
exit();
}
if ($compstatus == '')
{
$error = 'You must choose a company status for this company.
Click ‘back’ and try again.';
include '../errormustchoosecompanystatus.html.php';
exit();
}
$sql = "UPDATE company SET
name='$text',
type_id='$type_id',
compstatusid='$compstatusid',
date=CURDATE(),
address='$address',
city='$city',
state='$state',
zip='$zip',
signatory='$signatory',
title='$title';
WHERE id='$id'";
if (!mysqli_query($link, $sql))
{
$error = 'Error updating submitted company.';
include '/includes/inc_db_ciq_connection.php';
exit();
}
}
// The form processing code for deleting existing companies
if (isset($POST['action']) and $POST['action'] == 'Delete')
{
include $SERVER['DOCUMENT_ROOT'] . '/includes/inc_db_ciq_connection.php';
$id = mysqli_real_escape_string($link, $POST['id']);
// Delete the company
$sql = "DELETE FROM company WHERE id='$id'";
if (!mysqli_query($link, $sql))
{
$error = 'Error deleting company.';
include '../errordeleting.html.php';
exit();
}
}
/php