2

This is how I display the entire array:

print('<pre>');
print_r($_POST);
print('</pre>');

The Array :

Array
(
 [Prod] => Array
    (
        [0] => Array
            (
                ['id'] => 1
                ['name'] => value2
                ['desc'] => value
                ['cost'] => 500.0000
                ['sell_price'] => 1500.0000
                ['quant'] => 9
                ['vendor'] => 1002
                ['last_update'] => Joe Lee
            )

        [1] => Array
            ( ... )

        ['btnUpdate'] => Sumbitted Form
    )

)

but if I change it to $_POST['Prod']['btnUpdate'] it throws an error..

This is how the Error if I try to access "btnUpdate"

Notice: Undefined index: btnUpdate in C:\xampp\htdocs\Projects\Upwork\dharn\scratch\phase2\controller\update_Product.php on line 6

I've read the duplicate but as you can see the entire array of $_POST.. in my knowledge $_POST['Prod']['btpUpdate'] should exist but NO it does not, php can't find it...

UPDATE

i've made a type mistake on creating the array..

while($row = mysqli_fetch_array($result)){
?>
<tr>
  <td>
    <input type="text" hidden name="Prod[<?php echo $x?>]['id']" value="<?php echo $row['id']?>">
    <?php echo $row['id']?>
  </td>
  <td>
    <input type="text" name="Prod[<?php echo $x?>]['name']" value="<?php echo $row['name']?>">
  </td>
  <td>
    <input type="text" name="Prod[<?php echo $x?>]['desc']" value="<?php echo $row['description']?>">
  </td>
  <td>
    <input type="text" name="Prod[<?php echo $x?>]['cost']" value="<?php echo $row['cost']?>">
  </td>
  <td>
    <input type="text" name="Prod[<?php echo $x?>]['sell_price']" value="<?php echo $row['sell_price']?>">
  </td>
  <td>
    <input type="text" name="Prod[<?php echo $x?>]['quant']" value="<?php echo $row['quantity']?>">
  </td>
  <td>
    <select name="Prod[<?php echo $x?>]['vendor']">
      <?php echo Generate_Vendor($row['Vendor_name']); ?>
    </select>
  </td>
  <td>
    <input type="text" hidden name="Prod[<?php echo $x?>]['last_update']" value="<?php echo $row['Employee_name']?>">
    <?php echo $row['Employee_name']?>
  </td>
</tr>
<?php
$x++;
}
?>
</table>

<button type="submit" name="Prod['btnUpdate']" value="'Sumbitted Form'"> Update Product </button>

ANSWER

in a code like this name="Prod[<?php echo $x?>]['id']" right in the id part... that's the typo.. you should not put ( ' ' ) single-qoute on the array because it will generate an array like the one above where you could not access it because it is a string inside the key...

in simple words you should not create an array with a stringed key

Phil
  • 157,677
  • 23
  • 242
  • 245
Sean Reyes
  • 1,636
  • 11
  • 19
  • NO IT IS NOT a DUPLICATE.... I'm trying to access an array index that exist but it can't access it – Sean Reyes Nov 29 '17 at 03:49
  • I've got an answer.. I solved it... – Sean Reyes Nov 29 '17 at 03:53
  • PHP is not lying to you. When you try and access that index, it does not exist. Otherwise you would not see any errors. Unless you can create a reproducible example, you will find the information in the linked post answers your question. – Phil Nov 29 '17 at 03:53
  • no it does not give me the answer.. it tells me how to know if the index exist..... did you guys even look at the array on what wrong to it.. – Sean Reyes Nov 29 '17 at 03:55
  • @GroverReyes Did you? That's not valid php code. – Daedalus Nov 29 '17 at 03:56
  • the Answer was not to use isset() or empty() .... the answer was if you try to make an array don't put single asterisk on the key. – Sean Reyes Nov 29 '17 at 03:56
  • yes that's it... simple typo mistake.. but cleary it was not a duplicate.. just a simple typo... – Sean Reyes Nov 29 '17 at 03:57
  • 1
    @GroverReyes It is clearly a duplicate; the duplicate answers the question. That your code had a typo in it, which is not present in the question, does not make this not a duplicate. Or in other words, **the question, as it is written, is a duplicate because it does not contain the typo you noted in the comments**. – Daedalus Nov 29 '17 at 03:58
  • Nevermind Thank you all... I was only looking at the answer of the duplicate not knowing that there were still more answers that answered my question.. I'll just Delete this.. – Sean Reyes Nov 29 '17 at 04:01
  • I didn't knew it before if I had a Typo or not.. I only knew it the time I had to look back on my code and saw it then I already know the answer.. – Sean Reyes Nov 29 '17 at 04:03
  • because If I already knew that it was a typo then I would have solved it already.. – Sean Reyes Nov 29 '17 at 04:03
  • @GroverReyes In which case this question would be closed as a typo question, instead of a duplicate. However, you didn't post any code. You posted the result of code, but not the code itself. – Daedalus Nov 29 '17 at 04:05
  • Done.. I've explained the typo mistake on the UPDATE Section.. – Sean Reyes Nov 29 '17 at 04:11
  • 1
    Please correctly format your code; and updating it at this point is pointless; you already know the problem is a typo, and a question which is closed cannot be re-closed without being re-opened first, and I don't think that's going to happen, personally. – Daedalus Nov 29 '17 at 04:11
  • FYI, this is an asterisk `*`. `'` is an apostrophe or single-quote – Phil Nov 29 '17 at 04:16
  • it's okay... I just also want to tell those people who are like me that also made a typo like this so they would know the right answer.. and how to correctly format it.. – Sean Reyes Nov 29 '17 at 04:17
  • @Phil I'm soo sorry about that.. just got excited :( Edited – Sean Reyes Nov 29 '17 at 04:20
  • 1
    You know what, I'm going to re-open this. @GroverReyes if you could write up an answer, explaining that you're creating multi-dimensional form data via input names with square-brackets, that would be great. I can see this type of thing being a common mistake so an answer explaining the issue with quotes would be useful to the SO community – Phil Nov 29 '17 at 04:23
  • @GroverReyes The question is re-opened now, so please add the answer in an answer instead of in the question. – Daedalus Nov 29 '17 at 04:30

1 Answers1

2

The error here was that I was creating a parameterized array in a multi-Dimensional Array using brackets..

If you ever create a parameterized array don't do this

<input name="person['name']" ... 

this wont work because the quotes are included in the key in the array.

To access these keys, you would need something like

$_POST['person']["'name'"]

which is unintuitive. Keys in form element names should only be an index like 0,1,2,3 or a word but not a quoted string.

Do it like this instead

<input name="person[name]" ...
Phil
  • 157,677
  • 23
  • 242
  • 245
Sean Reyes
  • 1,636
  • 11
  • 19