Trying to access array offset on value of type null - 2020 Aug

Good Day PHP guru’s

I am busy creating a interface where one clicks on a user to update it. one will be taken to a page with all the current fields populated where one can update the fields and click update, which should update the data in the database (mysql)

However, as soon as I try and update I get " Trying to access array offset on value of type null in C:\xampp\htdocs\project\clientUpdate.php on line (all the lines of the fields).

I hope someone can point me in the right direction, as I have spent hours and hours on this but I can’t figure it out. I had a look throught the previous posts but I also didn’t seem to find anything that helps my cause.

You have a mistake somewhere in your code. Probably a wrong variable name being used. Just post all the relevant code.

my Code

<?php
include_once 'includes/dbconnect.inc.php';
$msg = "";

$id = isset($_GET['Client_id']) ? $_GET['Client_id'] : "0";

if(isset($_GET['btnSubmit'])) {
	$Client_id = $_GET['txtClientID'];
	$C_name = $_GET['txtName'];
	$C_surname = $_GET['txtSurname'];
	$C_Email = $_GET['txtEmail'];
	$Address = $_GET['txtAddress'];
	$Code = $_GET['txtCode'];
	$C_Tel_H = $_GET['txtC_Tel_H'];
	$C_Tel_W = $_GET['txtC_Tel_W'];
	$C_Tel_C = $_GET['txtC_Tel_C'];
	$Reference_ID = $_GET['txtRefID'];

$query1 = "UPDATE `tblclientinfo` SET Client_id='$Client_id'), C_name='$C_name', C_surname='$C_surname', C_Email='$C_Email', Address='$Address', Code='$Code', C_Tel_H='$C_Tel_H', C_Tel_W='$C_Tel_W', C_Tel_C='$C_Tel_C', Reference_ID='$Reference_ID' WHERE Client_id=".$id;

	if(mysqli_query($conn, $query1)){
$msg = "Record Updated!";
} else {

$msg = "Unable to Update! ". $conn->error ;

//troubleshooting
echo $Client_id ." ";
echo $C_name." ";
echo $C_surname." ";
echo $C_Email." ";
echo $Address." ";
echo $Code." ";
echo $C_Tel_H." ";
echo $C_Tel_W." ";
echo $C_Tel_C." ";
echo $Reference_ID." ";
echo $rec[0] ." ";
}
}

//show data of user
$query = "SELECT * FROM `tblclientinfo` WHERE Client_id=".$id;
$data = mysqli_query($conn, $query);
$rec = mysqli_fetch_array($data);
?>

<body>
<form method=”POST”>

Client ID : <input type="text" value="<?php echo $rec['Client_id']; ?>" name="txtClientID" readonly="readonly"/><br/>
First Name : <input type="text" value="<?php echo $rec['C_name']; ?>" name="txtName"/><br/>
Surname : <input type="text" value="<?php echo $rec['C_surname']; ?>" name="txtSurname"/><br/>
Email : <input type="text" value="<?php echo $rec['C_Email']; ?>" name="txtEmail"/><br/>
Address : <input type="text" value="<?php echo $rec['Address']; ?>" name="txtAddress"/><br/>
Code : <input type="text" value="<?php echo $rec['Code']; ?>" name="txtCode"/><br/>
Tell Nr Home : <input type="text" value="<?php echo $rec['C_Tel_H']; ?>" name="txtC_Tel_H"/><br/>
Tell Nr Work : <input type="text" value="<?php echo $rec['C_Tel_W']; ?>" name="txtC_Tel_W"/><br/>
Tell Nr Cell : <input type="text" value="<?php echo $rec['C_Tel_C']; ?>" name="txtC_Tel_C"/><br/>
Reference Number : <input type="text" value="<?php echo $rec['Reference_ID']; ?>" name="txtRefID"/><br/>
<input type="submit" name="btnSubmit" value="Update"/> <br/>

<?php echo $msg; ?>
</form>
</body>
</html>

Starting with the php error messages for line 38. The $rec variable doesn’t exist at that point in the code, so trying to echo $rec[0] won’t work. The query that is setting $rec is after that point in the code.

I don’t know if you have some code elsewhere that is initially setting $_GET[‘Client_id’] when the page is first requested, but $_GET[‘Client_id’] won’t be set after you submit the form, because the form is actually a get method form (the form data replaces any get parameter that would have been propagated in the url.) The reason for this, despite what looks like a method=‘post’ attribute, is because you apparently copied the method=”POST” attribute from somewhere and it has smart/curly quotes, which are meaningless in programming languages. I recommend that you delete and retype that using straight quotes. This will let you use the proper $_POST data in your form processing code. Also, don’t copy variables to other variables without any reason. Just keep the submitted data as an array and use elements in the array throughout the rest of the code.

$_GET[‘Client_id’] is a ‘required’ input for the page to work. It’s an error if it isn’t an integer greater then zero. You must validate all inputs before using them and setup a user error message for any input that isn’t valid. If there’s not a valid Client_id value, there’s no point in running the SELECT query or outputting the form.

If the SELECT query matches no data, again, this is an application error and you should setup a user error message that the query didn’t match any data and not attempt to output the form.

Lastly, use prepared queries when supplying external, unknown, dynamic values to a query when it gets executed, use exceptions for database statement errors, and switch to the much simpler PDO extension.

Hi PHDR,

Thank you for the effort and the help.
Some of the things mentioned are a bit “above my skillset” but I shall google and read up about them.

The POST I fixed - yes I did copy that code to test/try.
I did change the GET to REQUEST again (which I tried previously)

My main issue was my logic/procedure of how my code fired - I moved my block of code around to fire in the correct order and this works! I actually have a update form to update the data!

Thank you!

Sponsor our Newsletter | Privacy Policy | Terms of Service