0

I am creating an admission form for a school, what I want to achieve is that I don't want the users to write form number themselves but generate a form ID based on previously inserted record. I tried fetching the result by executing query:

   $query = "SELECT MAX(id) FROM studentAdmissionFormData;
   $result = mysqli_query($conn, $query);
   while($row = mysqli_fetch_array($result))
      {
         echo ++$row['id']; 
      }
    mysqli_close($conn);

I am using this code inside value attribute so it should go directly into value but it is returning empty. to check this I used a conditional statement and it verified that query is returning empty.

Now what I want to accomplish is that whenever the page is loaded a Form Number is automatically set, based on what was the number of previous ID.

I's using the above PHP code inside value attribute of <input class="form-control" type="text" name="formNumber" value="">

Salman Malik
  • 114
  • 7
  • I know there is no use of while loop for fetching just one cell but I just couldn't figure it out. – Salman Malik Dec 19 '20 at 18:42
  • You should never ever try to predict the next id. there is no reliable way and not a single reason to do so. Just leave it alone and get the usual way AFTER insert – Your Common Sense Dec 19 '20 at 18:42
  • @YourCommonSense so I should just leave the field disabled with nothing in it. and populate it with generated ```id``` once the submission is successful? – Salman Malik Dec 19 '20 at 18:45
  • Exactly that. I don't really get what's the point in having such a form field at all – Your Common Sense Dec 19 '20 at 18:45
  • Please! @S.Sachith I am curious know what's the right way to do it. EDIT You already answered, but can you tell me why is my query wrong, I mean I am asking to select MAX of ```id``` – Salman Malik Dec 19 '20 at 18:48
  • actually you don't need to follow the way you suggested. like @YourCommonSense you can auto generate it from the database table. But the way you asked also possible but it unnecessary – S.Sachith Dec 19 '20 at 18:51
  • @S.Sachith I am sorry but you don't quite understand what are you talking about. First, such a query won't do ANY good, and second, the order is not needed. From the fact you don't understand how max() function works, I make it you don't have much experience with mysql. Hence you cannot foresee all the bad consequences your bad advise could bring. – Your Common Sense Dec 19 '20 at 18:52
  • @YourCommonSense yha man I am not expert like you :) ,but i just gave answer for what he asked for also I told him that's unnecessary :) – S.Sachith Dec 19 '20 at 18:55
  • I'll take that as an advice, the query works now. But I am not following this since I too, feel now that it is unnecessary because it can cause problems when a most recent ID is deleted as it may cause confusion with logs. after deleting the most recent id the form number will get the same number next time a record is to be added. which is not a good thing. PS. I am new with php and mySQL so I beg your pardon. – Salman Malik Dec 19 '20 at 18:57
  • it's okay @SalmanMalik we all started from 0th step :) – S.Sachith Dec 19 '20 at 18:59

0 Answers0