0

I know my question answer available on stackoverflow. but i need solution using mysqli_query Function to insert multi rows in table attendance. I have codes oop class function to fetchdata to display all Records from table. i designed one dropdown with options present and absent with this form. when all records display from table a dropdown list display in last colmn of html for tick employee absent or present.

Form Fields Staff_id Staff_name attendance

This is my HTML Form Codes

<form action="atup.php" method="post" >
   <table class="table-bordered table mt-3">
      <thead>
         <tr>
            <th>#</th>
            <th>Staff ID</th>
            <th>Employee Name</th>
            <th>Department</th>
            <th>Desgignation</th>
            <th>Action</th>
         </tr>
      </thead>
      <tbody  >
         <?php
            $fetchdata=new DB_con();
            $sql=$fetchdata->fetchdata();
            $cnt=1;
            while($row=mysqli_fetch_array($sql))
            {
             ?>
         <tr>
            <td><?php echo htmlentities($cnt);?></td>
            <td><input type="text" disabled name="sname" value="<?php echo htmlentities($row['id']);?>" ></td>
            <td><input type="text" disabled name="sname" value="<?php echo htmlentities($row['name']);?>"></td>
            <td><input type="text" disabled name="dept" value="<?php echo htmlentities($row['dept']);?>"></td>
            <td><input type="text" disabled name="desi" value="<?php echo htmlentities($row['desi']);?>"></td>
            <td>
               <select name="att" class="form-control" required="" id="">
                  <option value="">Select Option</option>
                  <option  value="Present">Present</option>
                  <option  value="Absent">Absent</option>
               </select>
            </td>
         </tr>
         <?php
            // for serial number increment
            $cnt++;
            } ?>
         <tr>
            <td class="text-right" colspan="6" >
               <input type="hidden" name="action" value="add_attendance">
               <input type="hidden" data-provide="datepicker" data-date-end-date="0d" name="attendance_date" value="19-May-2022">
               <input type="submit" value="Submit" class="btn btn-primary">
            </td>
         </tr>
      </tbody>
   </table>
</form>

// include database connection file
include_once("functions.php");
//Object
$updatedata=new DB_con();
if(isset($_POST['submit']))
{

// Posted Values
//$sname=$_POST['sname'];

for ($i = 0; $i < count($_POST['sname']); $i++) {
    $sname = $_POST['sname'][$i];
    print_r($sname);
   
}

//Function Calling
$sql=$updatedata->insertatt($sname);
// Mesage after updation
echo "<script>alert('Record Inserted successfully');</script>";
// Code for redirection
echo "<script>window.location.href='allstaff.php'</script>";
}

my php file atup.php codes

  • `name="att[]`", otherwise the browser will only submit one single value for that field, rather than an array of values. You'll also need a hidden field containing the ID, to match that value with. Is that your real question? You ask about inserting using mysqli, but then show us some HTML which has a different mistake in it. I feel like you asked the wrong question. And you didn't actually specifically describe what is going wrong in your code when you run it, so I'm merely guessing based on my initial observation of your code. – ADyson May 20 '22 at 09:46
  • For future reference, please take the [tour] and read [ask] and how to create a [mre] of your problem. This guidance will help you understand how to ask a clear, useful, answerable question here. Remember we can't see your screen or read your mind. You need to explain an actual problem with your code so that we've got something to focus on and fix. "I need" is not a problem description :-). – ADyson May 20 '22 at 09:47
  • when i use array name="att[]", I give error Warning: Undefined array key "att" – Manmohan Singh May 20 '22 at 10:07
  • And where do you get that, exactly? Did you read the article about giving a [mre] of your issue? We cannot help you fix code that we cannot see. You can [edit] your question when you need to provide more info. You also need to edit it anyway, so that it focuses on your real question - it doesn't seem to be anything to do with mysqli, although I could be wrong because you've simply replied to say you tried my idea, but still without actually acknowledging or explaining the problem from your point of view. Again...we are not mind-readers. – ADyson May 20 '22 at 10:09
  • Please Review My question... I change it – Manmohan Singh May 20 '22 at 10:20
  • Ok. And which line is giving you the "Undefined array key "att"" error? Because I don't see any lines which would be likely to do that. – ADyson May 20 '22 at 10:25
  • However I can see you might get errors about `$_POST['sname']`, because in your HTML form you've marked that field as `disabled`, and disabled form fields are not submitted to the server. – ADyson May 20 '22 at 10:25
  • no asking according you mention array name=att[]... – Manmohan Singh May 20 '22 at 10:26
  • Not sure what you mean. It's a simple question: which line gives you that error? – ADyson May 20 '22 at 10:26
  • for ($i = 0; $i < count($_POST['sname']); $i++) { this line generate error – Manmohan Singh May 20 '22 at 10:27
  • That would give you `"Undefined array key "sname""`, not `"Undefined array key "att""`. Make sure you are telling me the correct error. And if you [look above](https://stackoverflow.com/questions/72316260/how-insert-multi-rows-in-database-using-mysqli-query?noredirect=1#comment127758732_72316260), I've already told you why you will get that error. And earlier in my very first comment, I already told you how to solve it - you need a hidden field containing the ID, so it will be submitted to the server alongside the attendance data. – ADyson May 20 '22 at 10:28
  • 1
    Thanks Lot. according your direction i got solution. My codes work now... I appreciate your help – Manmohan Singh May 20 '22 at 10:38

0 Answers0