-2

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'];
   }
?>
Lim
  • 39
  • 8
  • `But there's some errors when I run the code` ... and what are these errors? – GetSet Mar 20 '20 at 02:13
  • Like `mysqli_fetch_assoc() expects parameter 1 to be mysqli_result` – Lim Mar 20 '20 at 02:14
  • Change your question title to what you just said. – GetSet Mar 20 '20 at 02:15
  • and also I have a problem with `$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 want to make the result to be able to have `E_$year_(the increment code)` output – Lim Mar 20 '20 at 02:15
  • I don't know where is wrong sir, it would be great to be able to point out the problem – Lim Mar 20 '20 at 02:25

1 Answers1

0

I can not understand your problem properly but i will make some suggestions for you."mysqli_fetch_assoc() expects parameter 1 to be mysqli_result" means there is problem with connection or the query.i can see there is no error with the connection so the error must be in the query.you can use your sql editor to type that query and see it gives the expected output.then if it gives the expected output you can copy it to php code.if you can not so, please share your db details ('es_sys') so it will help to answer properly.