Still having issues with UPDATE

I’ve tried changing the code to allow UPDATE of data but I get errors. The data is inserted correctly, but when I click update, it fails.

First the HTML/PHP:

<?php
	//Database Connection
	include 'db-connection/dbcon.php';
	
	//Get ID from Database
	if(isset($_GET['id'])) {
		
		$id = $_GET['id'];
		
		$q = "SELECT * FROM records where id = $id";
		$r = mysqli_query($dbc, $q);
		
		$row = mysqli_fetch_assoc($r);
				
	}

	//Update Information
	if(isset($_POST['submitted']) == 1) {
		$uid = $_POST['uid'];
		$first = $_POST['first'];
		$middle = $_POST['middle'];
		$last = $_POST['last'];
		$dob = $_POST['dob'];
		
		$q = "UPDATE records SET uid='$uid', first='$first', middle='$middle', last='$last', dob='$dob' 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 updated record. Error: </div>".mysqli_error($dbc);
				echo "<div class='alert alert-danger'>.$q.</div>";
      		}
	}
?>
<!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 -->

<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>
		
		<form method="post" action="update.php">
			<label>UID:</label><input type="text" name="name" placeholder="Name" value="<?php echo $row['uid']; ?>"><br/><br/>
			<label>First:</label><input type="text" name="name" placeholder="Name" value="<?php echo $row['first']; ?>"><br/><br/>
			<label>Middle:</label><input type="text" name="gender" placeholder="Gender" value="<?php echo $row['middle']; ?>"><br/><br/>
			<label>Last:</label><input type="text" name="department" placeholder="Department" value="<?php echo $row['last']; ?>"><br/><br/>
			<label>DOB:</label><input type="text" name="address" placeholder="Address" value="<?php echo $row['dob']; ?>"><br/><br/>
			
			<button type="submit" value='update record' class='btn btn-primary'>Update Record</button>
			<input type="hidden" id="submitted" name="submitted">
		</form>

</body>
</html>

The error messages:
Notice : Undefined index: uid in C:\xampp\htdocs\MyFamilyTree\update.php on line 19

Notice : Undefined index: first in C:\xampp\htdocs\MyFamilyTree\update.php on line 20

Notice : Undefined index: middle in C:\xampp\htdocs\MyFamilyTree\update.php on line 21

Notice : Undefined index: last in C:\xampp\htdocs\MyFamilyTree\update.php on line 22

Notice : Undefined index: dob in C:\xampp\htdocs\MyFamilyTree\update.php on line 23

Notice : Undefined index: id in C:\xampp\htdocs\MyFamilyTree\update.php on line 25

Unable to updated record. Error:

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ‘’ at line 1

.UPDATE records SET uid=’’, first=’’, middle=’’, last=’’, dob=’’ WHERE id=.

This is my third attempt creating an update page. I keep missing something, for sure, but as a beginner, I don’t know what. Any help with solving my issues will be appreciated.

I would also be grateful for any examples of working code.

Thank you.

  • you do not have a form field with name=“uid” and thus there will be no element (index) in the $_POST array with the name uid.
  • Same point for other elements
  • Why set an action type in a hidden formfield while there is only one action?
  • If you want to check if the page is requested in the POST method which is when the user has pressed the Submit button you can do this with
if($_SERVER['REQUEST_METHOD'] == 'POST') {
    ...
}
  • Maybe more important: your database is vulnerable for SQL injection. Use prepared statements or at least escape the $_GET[‘id’] variable with mysqli_real_escape_string(). Or simply cast it to an integer with intval(). You could also validate the input for example to be an integer greater then zero.
1 Like
<label>UID:</label><input type="text" name="uid" placeholder="Name" value="<?php echo $row['uid']; ?>"><br/><br/>
<label>First:</label><input type="text" name="first" placeholder="Name" value="<?php echo $row['first']; ?>"><br/><br/>
<label>Middle:</label><input type="text" name="middle" placeholder="Gender" value="<?php echo $row['middle']; ?>"><br/><br/>
<label>Last:</label><input type="text" name="last" placeholder="Department" value="<?php echo $row['last']; ?>"><br/><br/>
<label>DOB:</label><input type="text" name="dob" placeholder="Address" value="<?php echo $row['dob']; ?>"><br/><br/>

make the changes as mentioned above.

1 Like

Thanks for the advice.:smiley:

Sponsor our Newsletter | Privacy Policy | Terms of Service