I have this function that will produce a running number (label) purposes via getting the value from a hidden input text when the date is selected (the input text stores the year of the date once calculated).
I wish for the output result to be E_(year)_(increment of 1 after counting the total result, add into 0000) e.g. output: E_2020_0001 (if COUNT = 0) E_2020_0002
But there's some errors when I run the code.
mysqli_fetch_assoc() expects parameter 1 to be mysqli_result
Help needed along with the output result
<?php
if(isset($_POST['getCode'])) {
$connect = mysqli_connect("localhost", "root", "", "sys");
$year = mysqli_real_escape_string($connect, $_POST['getCode']);
$sql = "SELECT CAST(CONCAT('E_$year', SUBSTR('0000' , 1, (LENGTH('0000') - LENGTH(CAST(COUNT(1) AS CHAR)))), COUNT(1) + 1) AS CHAR) as numCode FROM es_sys WHERE year = '$year'"; #I wish this can be E_$year_(increment number) but it cannot allow 'E_$year_' as it includes the _ as the variable
$result = mysqli_query($connect, $sql);
$row = mysqli_fetch_assoc($result);
echo $row['numCode'];
}
?>