#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Location
    Baltimore
    Posts
    190
    Rep Power
    18

    variable as name and array


    Hi,

    I am using this code:

    PHP Code:

      
    while($row mysql_fetch_assoc($gettabledata,$i)){
    echo 
    "<tr>";
        foreach(
    $row as $column=>$value) {
          echo 
    "<td><input type=\"text\" class=\"form-control\" name=\"$column[]\" value=\"$value\">";
        }
       } 
    I am doing a multiple rows update to mySql. So I added the "[]" to "name"...everytime I add "[]" the code gives me an error. But, without the "[]" it works fine but it won't let me update multiple rows.

    Help please...
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,287
    Rep Power
    630
    First you are using the deprecated (for more than a decade) MySQL extensions. You need to switch to PDO as your code is vulnerable to injection attack and will break with the next release of PHP.

    As for your specific problem, if I understand what you are tying to do, you want the [] to be output as a literal rather than using it as part of the variable which makes no sense without including an index.
    PHP Code:
    echo "<td><input type=\"text\" class=\"form-control\" name=\"$column"."[]\" value=\"$value\">"

    Comments on this post

    • FredS agrees
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Super Moderator
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2009
    Location
    Hartford, WI
    Posts
    1,455
    Rep Power
    70
    Well, as far as your foreach() defines, the $column will define the same 'name' for each input box. The foreach() is already taking its own steps through the array or such from MySQL, and the $column/$value items are being assigned individually each loop, and not as arrays. Is this your goal? If you could maybe offer an example of what is to be fetched from MySQL, and your intended goal as to how that is to come out in the end?


    Edit: Ah, gw1500se's mention of the name=\"$column"."[]\" may be what is intended. Didn't think of that.
    Last edited by Triple_Nothing; October 28th, 2014 at 11:00 AM.
    He who knows not that he knows not is a fool, ignore him. He who knows that he knows not is ignorant, teach him. He who knows not that he knows is asleep, awaken him. He who knows that he knows is a leader, follow him.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Location
    Baltimore
    Posts
    190
    Rep Power
    18
    Thanks gw1500se, that did the trick. Also, I am slowly but surely switching all of my code to PDO. Thanks again.
  8. #5
  9. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,031
    Rep Power
    4101
    You could also use the complex-syntax which involves putting {} around your variables.
    PHP Code:
    echo "<td><input type=\"text\" class=\"form-control\" name=\"{$column}[]\" value=\"{$value}\">"
    Or concatenate the variables (and optionally use single-quotes on the strings). This is what I would do.
    PHP Code:
    echo '<td><input type="text" class="form-control" name="' $column '[]" value="' $value '">'
    No ambiguity as to what they variables are. Easy to identify where the variables are in the string. No need to escape every attribute in the HTML. Easily highlighted by syntax highlighting system.

    Comments on this post

    • FredS agrees
    • s-p-n agrees : Another good way is to use sprintf()
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    351
    Rep Power
    258
    I th ink you missed a </td> at the end of that string.
    - The Wise Guy

IMN logo majestic logo threadwatch logo seochat tools logo