Click here to Skip to main content
14,775,228 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Undefined index: darb_result 



My PHP code :

<pre>
      if (isset($_POST['create_darb'])) {
       $darb_title        = escape($_POST['title']);
       $darb_user         = escape($_POST['darb_user']);
       $darb_status       = escape($_POST['darb_status']);

       $darb_content      = escape($_POST['darb_content']);
       $darb_date         = escape(date('d-m-y'));

       $darb_fileUpload      =  escape($_FILES['darb_fileUpload']['name']);
       $darb_fileUpload_temp = escape($_FILES['darb_fileUpload']['tmp_name']);

       $darb_work         =  escape($_POST['darb_work']);
       $darb_finish       =  escape($_POST['darb_finish']);
       $darb_result       =  escape($_POST['darb_result']);


       move_uploaded_file($darb_fileUpload_temp, "../uploads/$darb_fileUpload");

       $query = "INSERT INTO darb(darb_title, darb_user, darb_date, darb_content, darb_status, darb_fileUpload, darb_work, darb_finish, darb_result) ";

       $query .= "VALUES('{$darb_title}','{$darb_user}',now(),'{$darb_content}','{$darb_status}','{$darb_fileUpload}', '{$darb_work}','{$darb_finish}','{$darb_result}')";

       $create_darb_query = mysqli_query($connection, $query);

       confirmQuery($create_darb_query);

       $the_darb_id = mysqli_insert_id($connection);


       echo "<p class='bg-success'>Work Created. <a href='../admin/darb.php?d_id={$the_darb_id}'>View Work </a> or <a href='../admin/darb.php'>Edit More Work</a></p>";
   }


My HTML code :

<pre>    
<table id="hours" class="table table-bordered table-striped">
      <thead>
          <tr>
            <th></th>
            <th></th>
            <th></th>
          </tr>
      </thead>
      <tbody>
        <tr>
          <td><input  type="time" class="form-control form-control-sm" id="start"  name="darb_work" value="0:00"></td>
          <td><input  type="time" class="form-control form-control-sm" id="end" name="darb_finish" value="0:00"></td>
          <td><span   class="form-control form-control-sm"  id="diff"  name="darb_result" value="0:00"></td>
        </tr>
      </tbody>
    </table>


What I have tried:

I have tried a lot of advice online but couldnt resolve this error.
Posted
Updated 25-Apr-20 2:40am
v2
Comments
Peter_in_2780 24-Apr-20 21:58pm
   
It'll be the line
$darb_result = escape($_POST['darb_result']);
that's failing. Your POST data is missing an input named darb_result.
Matas - developer 25-Apr-20 7:24am
   
This is my table where input darb_result is

 
  <table id="hours" class="table table-bordered table-striped">
      <thead>
          <tr>
            <th></th>
            <th></th>
            <th></th>
          </tr>
      </thead>
      <tbody>
        <tr>
          <td><input  type="time" class="form-control form-control-sm" id="start"  name="darb_work" value="0:00"></td>
          <td><input  type="time" class="form-control form-control-sm" id="end" name="darb_finish" value="0:00"></td>
          <td><span   class="form-control form-control-sm"  id="diff"  name="darb_result" value="0:00"></td>
        </tr>
      </tbody>
    </table>
Peter_in_2780 25-Apr-20 9:34am
   
The element you have named darb_result is NOT an <input>, so it doesn't appear in your POST data.
Richard Deeming 29-Apr-20 14:49pm
   
Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation/interpolation to build a SQL query. ALWAYS use a parameterized query.

PHP: SQL Injection - Manual[^]

1 solution

Quote:
Undefined index: darb_result

It means that index darb_result does not exist in array $_POST.
[Update]
Quote:
This is my table where input darb_result is

My experience is that whatever you think it should be, the system is right when reporting a problem, the best is to check and track back until you find where is the problem.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

phpdbg | php debugger[^]
Debugging techniques for PHP programmers[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
   
v2
Comments
Matas - developer 25-Apr-20 7:27am
   
This is my table where input darb_result is

   <table id="hours" class="table table-bordered table-striped">      <thead>          <tr>            <th></th>            <th></th>            <th></th>          </tr>      </thead>      <tbody>        <tr>          <td><input  type="time" class="form-control form-control-sm" id="start"  name="darb_work" value="0:00"></td>          <td><input  type="time" class="form-control form-control-sm" id="end" name="darb_finish" value="0:00"></td>          <td><span   class="form-control form-control-sm"  id="diff"  name="darb_result" value="0:00"></td>        </tr>      </tbody>    </table>
Patrice T 25-Apr-20 7:36am
   
Use Improve question to update your question.
So that everyone can pay attention to this information.
Matas - developer 25-Apr-20 7:40am
   
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900