0

I want to update all table rows using one submit form ,and I find this way that i make the name of the input implement an array but I can't get it's values plus I'm getting undefined index for foo array. my code:

    <form class="form-horizontal form-row-seperated"  
     enctype="multipart/form-data"   action="updateSetting.php"  method="post">

        <div class="portlet">

            <div class="portlet-body">



             <?php    while ($row = mysqli_fetch_array($result)){ ?>




                <div class="form-group">
                    <label class="col-md-2 control-label"><?php echo $row['title_en'] ?>
                        <span class="required"> * </span>
                    </label>
                    <div class="col-md-10">
                        <input   type="text" class="form-control" placeholder=""
                               value="<?php echo $row['value'];?>" name="foo[<?php echo $row['setting_id']; ?>] "


                        >
  <!--                            <input type="text" name="foo[--><?php //echo $row['key']; ?><!--]" value='--><?php //echo $row['key'];?><!--' />-->


                        <span class="help-block"></span>

                    </div>
                </div>
                <?php   } ?>


            </div>

        <div class="portlet margin-bottom">
            <button class="btn btn-primary pull-right btn-lg" type="submit" value="submit" id="submit">
                <i class="fa fa-check"></i> Edit
            </button>
            <div class="clearfix"></div>
        </div>


       </form>

updateSetting.php

  <?php $value=$_POST['foo[]'];




    foreach ($_POST['foo'] as $key => $value) {

      $sql1 = "UPDATE settings SET value='$value' WHERE setting_id='$key'" 
      or 
      die(mysqli_error());
      $result1 = mysqli_query($sql1);
      }
        echo "update complete";

can any one tell me what's my mistake or give me another suggestion to update the rows in one form ...thanks in advance

SHOSHA K
  • 25
  • 1
  • 7
  • 2
    Find out the structure of `$_POST['foo']` by doing, `print_r($_POST['foo'])`, then loop over it accordingly. Your also open to sql injection and XSS. – Lawrence Cherone Aug 23 '17 at 06:19
  • What issue are you getting? Is there any error? – B. Desai Aug 23 '17 at 06:22
  • 1
    Note: You're currently having `or die(mysqli_error())` after you're creating a string. – M. Eriksson Aug 23 '17 at 06:22
  • `foreach ($_POST['foo']` change to `foreach ($_POST['foo[]']` – Brian Gottier Aug 23 '17 at 06:24
  • 1
    This row: `$value=$_POST['foo[]'];` is redundant and should actually throw a "undefined index"-notice. Have you checked your error log? A good idea is also to turn `display_errors` on in your local PHP environment. Read more here: [How do I get PHP errors to display?](http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) – M. Eriksson Aug 23 '17 at 06:24
  • @BrianGottier When posting a form containing arrays, you just access it with `$_POST['foo']`, not `$_POST['foo[]']`. The `[]` simply makes the values into an array. – M. Eriksson Aug 23 '17 at 06:25
  • @MagnusEriksson I'm getting "undefined index" but when I loop over foo I get this: Notice: Undefined index: foo[] in C:\xampp\htdocs\tfs\MilkMan\admin\includes\pages\updateSetting.php on line 1 Notice: Undefined index: foo[] in C:\xampp\htdocs\tfs\MilkMan\admin\includes\pages\updateSetting.php on line 4 6 times – SHOSHA K Aug 23 '17 at 06:29
  • So you are getting errors? You need to edit your question to include _all_ errors and what actually happens when you run your code. And that error is for the first row, that I mentioned. – M. Eriksson Aug 23 '17 at 06:31
  • @BrianGottier foreach invalid argument you can't do this – SHOSHA K Aug 23 '17 at 06:31
  • Remove `$value=$_POST['foo[]'];` from starting – B. Desai Aug 23 '17 at 06:33
  • @B.Desai can you see my comment to Magnus? where should I put it ? sorry but it's the first time that i tried to pass an array – SHOSHA K Aug 23 '17 at 06:33
  • So remove the first row. It's an invalid key and not needed. – M. Eriksson Aug 23 '17 at 06:34
  • @MagnusEriksson If I remove it so how to get foo values? – SHOSHA K Aug 23 '17 at 06:36
  • You are already getting the values in your foreach-loop: `$_POST['foo']`. You're not actually using the `$value` from the first row. You're using `$value` from your loop (which actually would _overwrite_ the initial `$value`). You should read up on how [foreach()](http://php.net/manual/en/control-structures.foreach.php) actually works. – M. Eriksson Aug 23 '17 at 06:38
  • @MagnusEriksson oh sorry i got it ... i forget the $_post into the foreach – SHOSHA K Aug 23 '17 at 06:41
  • Remove the first row that throws that error, then change your code according to the answer you got from @B.Desai below. – M. Eriksson Aug 23 '17 at 06:42

1 Answers1

2

Your php code should be as below. After executing query it will throw error not after assigning query to string. Also in mysqli you need to pass connection object in mysqli_query and mysqli_error

foreach ($_POST['foo'] as $key => $value) {

      $sql1 = "UPDATE settings SET value='$value' WHERE setting_id='$key'" ;

      $result1 = mysqli_query($con,$sql1) or  die(mysqli_error($con)); //need to add error here
      }
    echo "update complete";
B. Desai
  • 16,414
  • 5
  • 26
  • 47
  • i've tried it but i get this: Notice: Undefined variable: con in C:\xampp\htdocs\tfs\MilkMan\admin\includes\pages\updateSetting.php on line 11 Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\tfs\MilkMan\admin\includes\pages\updateSetting.php on line 11 – SHOSHA K Aug 23 '17 at 06:43
  • Its database connection object replace it with your connection object – B. Desai Aug 23 '17 at 06:44