Issue with updating records

Hello. I have an update page for updating data. It has a working select from option so all the data needing updating is shown. I can type a new value, but when I select the update button I get several "
Notice: Undefined variable: …
. All the variables in the form are undefined. What am I missing.

Thanks.

The update page code:

<?php include 'db-connection/dbcon.php'; ?>
<!DOCTYPE html>
<html lang="en">
	
<head>
	
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<meta name="description" content="Ancient Egypt">
	<title>My Family Tree</title>
	
	<!-- Latest compiled and minified CSS -->
	<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
	
	<!-- Optional theme -->
	<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap-theme.min.css">
	
	<!-- jQuery CSS -->
	<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
	
	<!-- site styles  -->
	<link rel="stylesheet" type="text/css" media="screen" href="css/styles.css">
	
	<!-- jQuery CDN -->
	<script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
	
	<!-- jquery UI -->
	<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
	
	<!-- Latest compiled and minified JavaScript -->
	<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>

</head>

<body>

	<div class="container">
		
		<h1>Update Record</h1> 
		
		<h2><a href='index.php' class='btn btn-default'>Create New Record</a> | <a href='view.php' class='btn btn-default'>Record List</a></h2>
		
		<h5 class="alert text-center"><strong>No details (N/D), Not Applicable (N/A)</strong></h5>
		
	<?php 
	
	if(isset($_GET['id'])) {
	
	$id = $_GET['id'];
	$r = mysqli_query($dbc, "SELECT * FROM records where id = $id");
	
		while($record = mysqli_fetch_assoc($r)){
			
		$uid = $record['uid'];
		$first = $record['first'];
		$middle = $record['middle'];
		$last = $record['last'];
		$dob = $record['dob'];
		$birthloc = $record['birthloc'];
		$bc = $record['bc'];
		$bcregdate = $record['bcregdate'];
		$bcregloc = $record['bcregloc'];
		$bcocert = $record['bcocert'];
		$bco = $record['bco'];
		$bcoloc = $record['bcoloc'];
		$bcodate = $record['bcodate'];
		$father = $record['father'];
		$mother = $record['mother'];
		$siblings = $record['siblings'];
		$children = $record['children'];
		$marriagecert = $record['marriagecert'];
		$marriedto = $record['marriedto'];
		$marriedat = $record['marriedat'];
		$marriedon = $record['marriedon'];
		$divorced = $record['divorced'];
		$divorcedon = $record['divorcedon'];
		$partner = $record['partner'];
		$partnername = $record['partnername'];
		$deathcert = $record['deathcert'];
		$diedon = $record['diedon'];
		$cause = $record['cause'];
		$deathregdate = $record['deathregdate'];
		$ic = $record['ic'];
		$icdate = $record['icdate'];
		$icloc = $record['icloc'];
		$employ = $record['employ'];
		$address = $record['address'];
		$other = $record['other'];
			
		} # end while loop
		
	} # end if 
	
	if(isset($_POST['id'])) {
			
			
			
		$q = "UPDATE records SET uid = $_POST[uid], first = $_POST[first], middle = $_POST[middle], last = $_POST[last], dob = $_POST[dob], birthloc = $_POST[birthloc], bc = $_POST[bc], bcregdate = $_POST[bcregdate], bcregloc = $_POST[bcregloc], bcocert = $_POST[bcocert], bco = $_POST[bco], bcoloc = $_POST[bcoloc], bcodate = $_POST[bcodate], father = $_POST[father], mother = $_POST[mother], siblings = $_POST[siblings], children = $_POST[children], marriagecert = $_POST[marriagecert], marriedto = $_POST[marriedto], marriedat = $_POST[marriedat], marriedon = $_POST[marriedon], divorced = $_POST[divorced], divorcedon = $_POST[divorcedon], partner = $_POST[partner], partnername = $_POST[partnername], deathcert = $_POST[deathcert], diedon = $_POST[diedon], cause = $_POST[cause], deathregdate = $_POST[deathregdate], ic = $_POST[ic], icdate = $_POST[icdate], employ = $_POST[employ], address = $_POST[address], other = $_POST[other] WHERE id = $_GET[id]";
				
		$r = mysqli_query($dbc, $q);
		 
		if($r){
		        	
		echo "<div class='alert alert-success'>Record was updated.</div>";
		
		}
			else{
					
			echo "<div class='alert alert-danger'>Unable to update record. Error: </div>".mysqli_error($dbc);
			echo "<div class='alert alert-danger'>.$q.</div>";
			} # end if else 
			
	} # end if
	
	?>
		
		
		<form action="update.php" method="post" role="form">
		
			<div class="container">
				
				<div class="row">
					<div class="col-md-12 block text-center">
						<h4>UID</h4>
					</div>
				</div><!-- row end: -->
		
				<div class="row">
					<div class="col-md-12 block2 text-center">
						<h4>
							<input name="uid" id="uid" type="text" value="<?php echo $uid; ?>" />
						</h4>
					</div>
				</div><!-- row end: -->
		
				<div class="row">
					<div class="col-md-4 block text-center">
						<h4>First name</h4>
					</div>
					<div class="col-md-4 block text-center">
						<h4>Middle name(s)</h4>
					</div>
					<div class="col-md-4 block text-center">
						<h4>Last name</h4>
					</div>
				</div><!-- row end: -->
			
				<div class="row">
					<div class="col-md-4 block2 text-center">
						<h4>
							<textarea name="first" id="first"><?php echo $first; ?></textarea>
						</h4>
					</div>
					<div class="col-md-4 block2 text-center">
						<h4>
							<textarea name="middle" id="middle"><?php echo $middle; ?></textarea>
						</h4>
					</div>
					<div class="col-md-4 block2 text-center">
						<h4>
							<textarea name="last" id="last"><?php echo $last; ?></textarea>
						</h4>
					</div>
				</div><!-- row end: -->
				
				<div class="row">
					<div class="col-md-4 block text-center">
						<h4>Date of birth</h4>
					</div>
					<div class="col-md-4 block text-center">
						<h4>Location of birth</h4>
					</div>
					<div class="col-md-4 block text-center">
						<h4>Birth certificate</h4>
					</div>
				</div><!-- row end: -->
				
				<div class="row">
					<div class="col-md-4 block2 text-center">
						<h4>
							<textarea name="dob" id="dob"><?php echo $dob; ?></textarea>
						</h4>
					</div>
					<div class="col-md-4 block2 text-center">
						<h4>
							<textarea name="birthloc" id="birthloc"><?php echo $birthloc; ?></textarea>
						</h4>
					</div>
					<div class="col-md-4 block2 text-center">
						<h4>
							<textarea name="bc" id="bc"><?php echo $bc; ?></textarea>
						</h4>
					</div>
				</div><!-- row end: -->
				
				<div class="row">
					<div class="col-md-4 block text-center">
						<h4>BC Registration date</h4>
					</div>
					<div class="col-md-4 block text-center">
						<h4>BC Registration location</h4>
					</div>
					<div class="col-md-4 block text-center">
						<h4>BCO certificate</h4>
					</div>
				</div><!-- row end: -->

				<div class="row">
					<div class="col-md-4 block2 text-center">
						<h4>
							<textarea name="bcregdate" id="bcregdate"><?php echo $bcregdate; ?></textarea>
						</h4>
					</div>
					<div class="col-md-4 block2 text-center">
						<h4>
							<textarea name="bcregloc" id="bcreloc"><?php echo $bcregdate; ?></textarea>
						</h4>
					</div>
					<div class="col-md-4 block2 text-center">
						<h4>
							<textarea name="bcocert" id="bcocert"><?php echo $bcocert; ?></textarea>
						</h4>
					</div>
				</div><!-- row end: -->

				<div class="row">
					<div class="col-md-4 block text-center">
						<h4>BCO</h4>
					</div>
					<div class="col-md-4 block text-center">
						<h4>Location BCO</h4>
					</div>
					<div class="col-md-4 block text-center">
						<h4>BCO date</h4>
					</div>
				</div><!-- row end: -->
	
				<div class="row">
					<div class="col-md-4 block2 text-center">
						<h4>
							<textarea name="bco" id="bco"><?php echo $bcocert; ?></textarea>
						</h4>
					</div>
					<div class="col-md-4 block2 text-center">
						<h4>
							<textarea name="bcoloc" id="bcoloc"><?php echo $bcoloc; ?></textarea>
						</h4>
					</div>
					<div class="col-md-4 block2 text-center">
						<h4>
							<textarea name="bcodate" id="bcodate"><?php echo $bcodate; ?></textarea>
						</h4>
					</div>
				</div><!-- row end: -->
				
				<div class="row">
					<div class="col-md-3 block text-center">
						<h4>Father</h4>
					</div>
					<div class="col-md-3 block text-center">
						<h4>Mother</h4>
					</div>
					<div class="col-md-3 block text-center">
						<h4>Sibling(s)</h4>
					</div>
					<div class="col-md-3 block text-center">
						<h4>Children</h4>
					</div>
				</div><!-- row end: -->

				<div class="row">
					<div class="col-md-3 block2 text-center">
						<h4>
							<textarea name="father" id="father"><?php echo $father; ?></textarea>
						</h4>
					</div>
					<div class="col-md-3 block2 text-center">
						<h4>
							<textarea name="mother" id="mother"><?php echo $mother; ?></textarea>
						</h4>
					</div>
					<div class="col-md-3 block2 text-center">
						<h4>
							<textarea name="siblings" id="siblings"><?php echo $siblings; ?></textarea>
						</h4>
					</div>
					<div class="col-md-3 block2 text-center">
						<h4>
							<textarea name="children" id="children"><?php echo $children; ?></textarea>
						</h4>
					</div>
				</div><!-- row end: -->
			
				<div class="row">
					<div class="col-md-3 block text-center">
						<h4>Marriage certificate</h4>
					</div>
					<div class="col-md-3 block text-center">
						<h4>Married to</h4>
					</div>
					<div class="col-md-3 block text-center">
						<h4>Married at</h4>
					</div>
					<div class="col-md-3 block text-center">
						<h4>Married on</h4>
					</div>
				</div><!-- row end: -->

				<div class="row">
					<div class="col-md-3 block2 text-center">
						<h4>
							<textarea name="marriagecert" id="marriagecert"><?php echo $marriagecert; ?></textarea>
						</h4>
					</div>
					<div class="col-md-3 block2 text-center">
						<h4>
							<textarea name="marriedto" id="marriedto"><?php echo $marriedto; ?></textarea>
						</h4>
					</div>
					<div class="col-md-3 block2 text-center">
						<h4>
							<textarea name="marriedat" id="marriedat"><?php echo $marriedat; ?></textarea>
						</h4>
					</div>
					<div class="col-md-3 block2 text-center">
						<h4>
							<textarea name="marriedon" id="marriedon"><?php echo $marriedon; ?></textarea>
						</h4>
					</div>
				</div><!-- row end: -->
				
				<div class="row">
					<div class="col-md-3 block text-center">
						<h4>Divorced</h4>
					</div>
					<div class="col-md-3 block text-center">
						<h4>Divorced on</h4>
					</div>
					<div class="col-md-3 block text-center">
						<h4>Partner</h4>
					</div>
					<div class="col-md-3 block text-center">
						<h4>Partner name</h4>
					</div>
				</div><!-- row end: -->

				<div class="row">
					<div class="col-md-3 block2 text-center">
						<h4>
							<textarea name="divorced" id="divorced"><?php echo $divorced; ?></textarea>
						</h4>
					</div>
					<div class="col-md-3 block2 text-center">
						<h4>
							<textarea name="divorcedon" id="divorcedon"><?php echo $divorcedon; ?></textarea>
						</h4>
					</div>
					<div class="col-md-3 block2 text-center">
						<h4>
							<textarea name="partner" id="partner"><?php echo $partner; ?></textarea>
						</h4>
					</div>
					<div class="col-md-3 block2 text-center">
						<h4>
							<textarea name="partnername" id="partnername"><?php echo $partnername; ?></textarea>
						</h4>
					</div>
				</div><!-- row end: -->
					
				<div class="row">
					<div class="col-md-3 block text-center">
						<h4>Death certificate</h4>
					</div>
					<div class="col-md-3 block text-center">
						<h4>Died on</h4>
					</div>
					<div class="col-md-3 block text-center">
						<h4>Cause</h4>
					</div>
					<div class="col-md-3 block text-center">
						<h4>Death registered on</h4>
					</div>
				</div><!-- row end: -->

				<div class="row">
					<div class="col-md-3 block2 text-center">
						<h4>
							<textarea name="deathcert" id="deathcert"><?php echo $deathcert; ?></textarea>
						</h4>
					</div>
					<div class="col-md-3 block2 text-center">
						<h4>
							<textarea name="diedon" id="diedon"><?php echo $diedon; ?></textarea>
						</h4>
					</div>
					<div class="col-md-3 block2 text-center">
						<h4>
							<textarea name="cause" id="cause"><?php echo $cause; ?></textarea>
						</h4>
					</div>
					<div class="col-md-3 block2 text-center">
						<h4>
							<textarea name="deathregdate" id="deathregdate"><?php echo $deathregdate; ?></textarea>
						</h4>
					</div>
				</div><!-- row end: -->
	
				<div class="row">
					<div class="col-md-4 block text-center">
						<h4>Interred / Cremated</h4>
					</div>
					<div class="col-md-4 block text-center">
						<h4>IC on</h4>
					</div>
					<div class="col-md-4 block text-center">
						<h4>IC location</h4>
					</div>
				</div><!-- row end: -->

				<div class="row">
					<div class="col-md-4 block2 text-center">
						<h4>
							<textarea name="ic" id="ic"><?php echo $ic; ?></textarea>
						</h4>
					</div>
					<div class="col-md-4 block2 text-center">
						<h4>
							<textarea name="icdate" id="icdate"><?php echo $icdate; ?></textarea>
						</h4>
					</div>
					<div class="col-md-4 block2 text-center">
						<h4>
							<textarea name="icloc" id="icloc"><?php echo $icloc; ?></textarea>
						</h4>
					</div>
				</div><!-- row end: -->
	
				<div class="row">
					<div class="col-md-4 block text-center">
						<h4>Employment details</h4>
					</div>
					<div class="col-md-4 block text-center">
						<h4>Address from - to</h4>
					</div>
					<div class="col-md-4 block text-center">
						<h4>Other</h4>
					</div>
				</div><!-- row end: -->
				
				<div class="row">
					<div class="col-md-4 block2 text-center">
						<h4>
							<textarea name="employ" id="employ"><?php echo $employ; ?></textarea>
						</h4>
					</div>
					<div class="col-md-4 block2 text-center">
						<h4>
							<textarea name="address" id="address"><?php echo $address; ?></textarea>
						</h4>
					</div>
					<div class="col-md-4 block2 text-center">
						<h4>
							<textarea name="other" id="other"><?php echo $other; ?></textarea>
						</h4>
					</div>
				</div><!-- row end: -->
				
				<div class="row">
						<div class="col-md-12 block text-center">
							<input type='submit' value='Update Record' class='btn btn-primary' />
						</div>
				</div><!-- row end: -->
				
			</div><!-- container end: -->
			
		</form><!-- form end: -->

</body>

</html>

Your code is wide open to an SQL Injection Attack. If you have it on the Internet, take it down immediately.

You need to start small, with only the code needed for one form field. Once you can design, write, test, and debug the code for that field, you can deal with the code needed for all the rest of the fields. It is not worth your time to write out all the code for 30+ fields, if you must make repeated changes to it before you arrive at the final usage. The way you would deal with the code for multiple fields, is to use a data-driven design, where you have a data structure (array, database table) that defines the expected form fields, what validation steps each needs, and what processing each field will be used for on the server. Also, with a data-driven design, the data will be operated on as a set, using php array functions. The 30+ lines of code copying variables to other variables would become one single line of code that trims all the data at once and places the trimmed version into an array variable that would get used in the rest of the code.

Short-version: in the time it took you to write out all that code, 30+ times, you could have gotten a good start on the actual logic needed to make this work.

What is the logic needed for editing existing data? If the form has not been submitted (there’s no $_POST data), retrieve the existing values from the database to populate the form fields. If the form has been submitted, use the submitted post data to populate the form fields (when there’s a validation error or you want to re-edit the values.) The easiest way of doing this is to use an array variable to hold the data, that’s initially set to an empty array, receives a copy of the form data inside the form processing code, then is tested after the form processing code to decide if the initial data should be retrieved. The initial data would be stored in the same array variable. The elements of this array variable are used for the form field values.

Thank you for the advice. I am aware of this issue but I am testing locally and will address this issue at a later point.

I will take your advice. I will look into using arrays and functions as I’ve not used them before.

Sponsor our Newsletter | Privacy Policy | Terms of Service