Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Where I am so far in my CRUD form...
<?php
/*
Allows the user to both create new records and edit existing records
*/
// connect to the database
include("open_db.php");
// creates the new/edit record form
// since this form is used multiple times in this file, I have made it a function that is easily reusable
function renderForm($case_no = '', $full_name ='', $company_name ='', $claim_principal ='', $claim_interest = '', $claim_int_perc = '', $claim_total = '', $incident_date = '', $incident_town = '', $incident_state_then = '', $incident_country_then = '', $incident_state_2012 = '', $incident_country_2012 = '', $home_port_town = '', $home_port_state_then = '', $home_port_country_then = '', $home_port_state_2012 = '', $home_port_country_2012 = '', $destination_port_town = '', $destination_port_state_then = '', $destination_port_country_then = '', $destination_port_state_2012 = '', $destination_port_country_2012 = '', $destination_goods_town = '', $destination_goods_state_then = '', $destination_goods_country_then = '', $destination_goods_state_2012 = '', $destination_goods_country_2012 = '', $claim_date = '', $ship_name = '', $ship_type = '', $US_award = '', $Mexican_award = '', $final_principal = '', $final_interest = '', $final_total = '', $decision_date = '', $error = '')
{ ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>
<?php if ($case_no != '') { echo "Edit Record"; } else { echo "New Record"; } ?>
</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<h1><?php if ($case_no != '') { echo "Edit Record"; } else { echo "New Record"; } ?></h1>
<?php if ($error != '') {
echo "<div style='padding:4px; border:1px solid red; color:red'>" . $error
. "</div>";
} ?>
<form action="" method="post">
<div>
<p>Case Number: <br />
<input type="text" name="case_no" size="3" value="<?php echo $case_no; ?>"/> <br />
</p>
<h1>Claimants</h1>
<p>People: <em>Check the list below. If the people involved in the claim are not included in the list below, please add information about them.</em><br />
//menu of people
$claimant_person = $conn->query("SELECT person_no, last_name, first_name FROM person ORDER BY last_name"); ?>
<select name='claimant_person[]' multiple='yes' size='5'>
<option value='empty'> --- </option>
<?php while($row = mysqli_fetch_array($claimant_person)){ ?>
<option value = "<?php echo $row['person_no']; ?>"
<?php if($case_no !=''){
foreach($claimant_ID as $claim_ID){
if ($claim_ID == $row['person_no']){
echo 'selected';}
}
} ?> >
<?php echo $row['first_name', ' ', 'last_name'];?> </option>
<?php }
?>
</select><br />
//inputs for people
<p>Names: <em>(Separate the names with commas.)</em></p>
<input type="text" name="full_name" size="10" value="<?php echo $full_name; ?>"/> <br />
</p>
<p>Companies: <em>Check the list below. If the companies involved in the claim are not included in the list below, please add information about them.</em><br />
//menu of companies
$claimant_company = $conn->query("SELECT company_no, company_name FROM company ORDER BY company_name"); ?>
<select name='claimant_company[]' multiple='yes' size='5'>
<option value='empty'> --- </option>
<?php while($row = mysqli_fetch_array($claimant_company)){ ?>
<option value = "<?php echo $row['company_no']; ?>"
<?php if($case_no !=''){
foreach($claimant_co_ID as $claim_co_ID){
if ($claim_co_ID == $row['company_no']){
echo 'selected';}
}
} ?> >
<?php echo $row['company_name'];?> </option>
<?php }
?>
</select><br />
//inputs for companies
<p>Names: <em>(Separate the names with commas.)</em></p>
<input type="text" name="company_name" size="10" value="<?php echo $company_name; ?>"/> <br />
</p>
<h1>Information about claim</h1>
<h2>Financials</h2>
<p>Claim Principal: <br />
<input type="text" name="claim_principal" size="10" value="<?php echo $claim_principal; ?>"/> <br />
</p>
<p>Claim Interest: <br />
<input type="text" name="claim_interest" size="10" value="<?php echo $claim_interest; ?>"/> <br />
</p>
<p>Claim Interest Percentage: <br />
<input type="text" name="claim_int_perc" size="10" value="<?php echo $claim_int_perc; ?>"/> <br />
</p>
<p>Claim Total: <br />
<input type="text" name="claim_total" size="10" value="<?php echo $claim_total; ?>"/> <br />
</p>
<h2>Incident</h2>
<p>Date of incident (mm/dd/yyyy): <br />
<input type="text" name="incident_date" size="10" value="<?php echo $incident_date; ?>"/> <br />
</p>
<p>Place of incident: <em>Select from the list below. If the place is not included, please add information about it.</em></p>
//list of places for incident
$incident_place = $conn->query("SELECT place_no, town, state_then, country_then, state_2012, country_2012 FROM place ORDER BY town"); ?>
<select name='incident_place[]' multiple='yes' size='5'>
<option value='empty'> --- </option>
<?php while($row = mysqli_fetch_array($incident_place)){ ?>
<option value = "<?php echo $row['place_no']; ?>"
<?php if($case_no !=''){
foreach($incident_place_ID as $inc_place_ID){
if ($inc_place_ID == $row['place_no']){
echo 'selected';}
}
} ?> >
<?php echo $row['town', ', ', 'state_then ', 'country_then'];?> </option>
<?php }
?>
</select><br />
<p>Town name: <br />
<input type="text" name="incident_town" size="20" value="<?php echo $incident_town; ?>"/> <br />
</p>
<p>State (at time of incident; use two-letter abbreviation): <br />
<input type="text" name="incident_state_then" size="20" value="<?php echo $incident_state_then; ?>"/> <br />
</p>
<p>Country (at time of incident): <br />
<input type="text" name="incident_country_then" size="20" value="<?php echo $incident_country_then; ?>"/> <br />
</p>
<p>State (today; use two-letter abbreviation): <br />
<input type="text" name="incident_state_2012" size="20" value="<?php echo $incident_state_2012; ?>"/> <br />
</p>
<p>Country (today): <br />
<input type="text" name="incident_country_2012" size="20" value="<?php echo $incident_country_2012; ?>"/> <br />
</p>
<p>Home port: <em>Select from the list below. If the place is not included, please add information about it.</em></p>
//list of places for home port
$home_port_place = $conn->query("SELECT place_no, town, state_then, country_then, state_2012, country_2012 FROM place ORDER BY town"); ?>
<select name='home_port_place[]' multiple='yes' size='5'>
<option value='empty'> --- </option>
<?php while($row = mysqli_fetch_array($home_port_place)){ ?>
<option value = "<?php echo $row['place_no']; ?>"
<?php if($case_no !=''){
foreach($home_port_place_ID as $hp_place_ID){
if ($hp_place_ID == $row['place_no']){
echo 'selected';}
}
} ?> >
<?php echo $row['town', ', ', 'state_then ', 'country_then'];?> </option>
<?php }
?>
</select><br />
<p>Town name: <br />
<input type="text" name="home_port_town" size="20" value="<?php echo $home_port_town; ?>"/> <br />
</p>
<p>State (at time of incident; use two-letter abbreviation): <br />
<input type="text" name="home_port_state_then" size="20" value="<?php echo $home_port_state_then; ?>"/> <br />
</p>
<p>Country (at time of incident): <br />
<input type="text" name="home_port_country_then" size="20" value="<?php echo $home_port_country_then; ?>"/> <br />
</p>
<p>State (today; use two-letter abbreviation): <br />
<input type="text" name="home_port_state_2012" size="20" value="<?php echo $home_port_state_2012; ?>"/> <br />
</p>
<p>Country (today): <br />
<input type="text" name="home_port_country_2012" size="20" value="<?php echo $home_port_country_2012; ?>"/> <br />
</p>
<p>Destination port: <em>Select from the list below. If the place is not included, please add information about it.</em></p>
//list of places for destination port
$destination_port_place = $conn->query("SELECT place_no, town, state_then, country_then, state_2012, country_2012 FROM place ORDER BY town"); ?>
<select name='destination_port_place[]' multiple='yes' size='5'>
<option value='empty'> --- </option>
<?php while($row = mysqli_fetch_array($destination_port_place)){ ?>
<option value = "<?php echo $row['place_no']; ?>"
<?php if($case_no !=''){
foreach($destination_port_place_ID as $dest_port_place_ID){
if ($dest_port_place_ID == $row['place_no']){
echo 'selected';}
}
} ?> >
<?php echo $row['town', ', ', 'state_then ', 'country_then'];?> </option>
<?php }
?>
</select><br />
<p>Town name: <br />
<input type="text" name="destination_port_town" size="20" value="<?php echo $destination_port_town; ?>"/> <br />
</p>
<p>State (at time of incident; use two-letter abbreviation): <br />
<input type="text" name="destination_port_state_then" size="20" value="<?php echo $destination_port_state_then; ?>"/> <br />
</p>
<p>Country (at time of incident): <br />
<input type="text" name="destination_port_country_then" size="20" value="<?php echo $destination_port_country_then; ?>"/> <br />
</p>
<p>State (today; use two-letter abbreviation): <br />
<input type="text" name="destination_port_state_2012" size="20" value="<?php echo $destination_port_state_2012; ?>"/> <br />
</p>
<p>Country (today): <br />
<input type="text" name="destination_port_country_2012" size="20" value="<?php echo $destination_port_country_2012; ?>"/> <br />
</p>
<p>Destination of goods: <em>Select from the list below. If the place is not included, please add information about it.</em></p>
//list of places for destination of goods
$destination_goods_place = $conn->query("SELECT place_no, town, state_then, country_then, state_2012, country_2012 FROM place ORDER BY town"); ?>
<select name='destination_goods_place[]' multiple='yes' size='5'>
<option value='empty'> --- </option>
<?php while($row = mysqli_fetch_array($destination_goods_place)){ ?>
<option value = "<?php echo $row['place_no']; ?>"
<?php if($case_no !=''){
foreach($destination_goods_place_ID as $dest_goods_place_ID){
if ($dest_goods_place_ID == $row['place_no']){
echo 'selected';}
}
} ?> >
<?php echo $row['town', ', ', 'state_then ', 'country_then'];?> </option>
<?php }
?>
</select><br />
<p>Town name: <br />
<input type="text" name="destination_goods_town" size="20" value="<?php echo $destination_goods_town; ?>"/> <br />
</p>
<p>State (at time of incident; use two-letter abbreviation): <br />
<input type="text" name="destination_goods_state_then" size="20" value="<?php echo $destination_goods_state_then; ?>"/> <br />
</p>
<p>Country (at time of incident): <br />
<input type="text" name="destination_goods_country_then" size="20" value="<?php echo $destination_goods_country_then; ?>"/> <br />
</p>
<p>State (today; use two-letter abbreviation): <br />
<input type="text" name="destination_goods_state_2012" size="20" value="<?php echo $destination_goods_state_2012; ?>"/> <br />
</p>
<p>Country (today): <br />
<input type="text" name="destination_goods_country_2012" size="20" value="<?php echo $destination_goods_country_2012; ?>"/> <br />
</p>
<p>Date claim filed (mm/dd/yyyy): <br />
<input type="text" name="claim_date" size="10" value="<?php echo $claim_date; ?>"/> <br />
</p>
<h2>Ship Information</h2>
<p>Ship: <em>Select from the list below. If the ship is not included, please add information about it.</em></p>
//list of ships
$ship_list = $conn->query("SELECT ship_no, ship_name, ship_type FROM ship ORDER BY ship_name"); ?>
<select name='ship_list[]' multiple='yes' size='5'>
<option value='empty'> --- </option>
<?php while($row = mysqli_fetch_array($ship_list)){ ?>
<option value = "<?php echo $row['ship_no']; ?>"
<?php if($case_no !=''){
foreach($ship_ID as $sh_ID){
if ($sh_ID == $row['ship_no']){
echo 'selected';}
}
} ?> >
<?php echo $row['ship_name', ', ', 'ship_type'];?> </option>
<?php }
?>
</select><br />
<p>Ship name: <br />
<input type="text" name="ship_name" size="20" value="<?php echo $ship_name; ?>"/> <br />
</p>
<p>Ship type: <br />
<input type="text" name="ship_type" size="20" value="<?php echo $ship_type; ?>"/> <br />
</p>
<h1>Information about decision</h1>
<p>Award by U.S. judge: <br />
<input type="text" name="US_award" size="10" value="<?php echo $US_award; ?>"/> <br />
</p>
<p>Award by Mexican judge: <br />
<input type="text" name="Mexican_award" size="10" value="<?php echo $Mexican_award; ?>"/> <br />
</p>
<h3>Final award:</h3>
<p>Principal: <br />
<input type="text" name="final_principal" size="10" value="<?php echo $final_principal; ?>"/> <br />
</p>
<p>Interest: <br />
<input type="text" name="final_interest" size="10" value="<?php echo $final_interest; ?>"/> <br />
</p>
<p>Total: <br />
<input type="text" name="final_total" size="10" value="<?php echo $final_total; ?>"/> <br />
</p>
<p>Date of final decision (mm/dd/yyyy): <br />
<input type="text" name="decision_date" size="10" value="<?php echo $decision_date; ?>"/> <br />
</p>
<br /><input type="submit" name="submit" value="Submit" />
</div>
</form>
</body>
</html>
<?php } // ends function renderForm
?>
//edit record
//this sets the ID from the record
<?php if (isset($_GET['case_no'])) {
echo "Isset Success. <br />";}
else{ echo "Problems";}
// make sure the 'case_no' value is valid
if (is_numeric($_GET['case_no']) && $_GET['case_no'] > 0) {
// get 'case_no' from URL
$case_no = $_GET['case_no'];
echo $case_no;}
//
if($stmt = $conn->prepare("SELECT case_no, claim_principal, claim_interest, claim_total, claim_int_perc, US_award, Mexican_award, final_principal, final_interest, final_total, incident_date, claim_date, decision_date, FK_incident_place, FK_home_port, FK_destination_port, FK_destination_goods, FK_case_no, FK_company_no, FK_case_no, FK_person_no, FK_ship_no, company_no, company_name, person_no, last_name, first_name, place_no, town, state_then, state_2012, country_then, country_2012, ship_no, ship_name, ship_type FROM claim, claim_company, claim_person, claim_ship, company, person, place, ship WHERE case_no=?")) {
$stmt->bind_param("i", $case_no);
$stmt->execute();
$stmt->bind_result($case_no, $full_name, $company_name, $claim_principal, $claim_interest, $claim_int_perc, $claim_total, $incident_date, $incident_town, $incident_state_then, $incident_country_then, $incident_state_2012, $incident_country_2012, $home_port_town, $home_port_state_then, $home_port_country_then, $home_port_state_2012, $home_port_country_2012, $destination_port_town, $destination_port_state_then, $destination_port_country_then, $destination_port_state_2012, $destination_port_country_2012, $destination_goods_town, $destination_goods_state_then, $destination_goods_country_then, $destination_goods_state_2012, $destination_goods_country_2012, $claim_date, $ship_name, $ship_type, $US_award, $Mexican_award, $final_principal, $final_interest, $final_total, $decision_date);
$stmt->fetch();
$stmt->close();
}
// query and fetch person
$person = "SELECT DISTINCT person.person_no, person.last_name, person.first_name, claim.case_no, claim_person.FK_case_no, claim_person.FK_person_no FROM person, claim_person, claim WHERE claim.case_no = claim_person.FK_case_no AND person.person_no = claim_person.FK_person_no" ;
$personArray = mysqli_query($person);
while ($row = mysqli_fetch_assoc($personArray)) {
$personID[] = $row['person_no'];}
print_r($personID);
//run form
renderForm($case_no, $full_name, $company_name, $claim_principal, $claim_interest, $claim_int_perc, $claim_total, $incident_date, $incident_town, $incident_state_then, $incident_country_then, $incident_state_2012, $incident_country_2012, $home_port_town, $home_port_state_then, $home_port_country_then, $home_port_state_2012, $home_port_country_2012, $destination_port_town, $destination_port_state_then, $destination_port_country_then, $destination_port_state_2012, $destination_port_country_2012, $destination_goods_town, $destination_goods_state_then, $destination_goods_country_then, $destination_goods_state_2012, $destination_goods_country_2012, $claim_date, $ship_name, $ship_type, $US_award, $Mexican_award, $final_principal, $final_interest, $final_total, $decision_date);
}
else {echo "Um, yeah, I'm going to have to sort of, um, disagree with you there";}
?>
//below is code from my old entry form. I'm figuring out whether to keep a similar structure or trash it.
<?php
$case_no = $_POST['case_no'];
$claim_principal = $_POST['claim_principal'];
$claim_interest = $_POST['claim_interest'];
$claim_total = $_POST['claim_total'];
$incident_date = $_POST['incident_date'];
$claim_date = $_POST['claim_date'];
$US_award = $_POST['US_award'];
$Mexican_award = $_POST['Mexican_award'];
$final_principal = $_POST['final_principal'];
$final_interest = $_POST['final_interest'];
$final_total = $_POST['final_total'];
$decision_date = $_POST['decision_date'];
include("open_db.php");
$query="INSERT INTO claim (case_no, claim_principal, claim_interest, claim_total, incident_date, US_award, Mexican_award, final_principal, final_interest, final_total, decision_date) VALUES ('".$case_no."', '".$claim_principal."', '".$claim_interest."', '".$claim_total."', '".$incident_date."', '".$US_award."', '".$Mexican_award."', '".$final_principal."', '".$final_interest."', '".$final_total."', '".$decision_date."')";
echo $query;
mysqli_query($conn, $query, $resultmode = MYSQLI_STORE_RESULT) or die ('Error updating database');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.