0

I have this script where I want to post values from the form into my database. I want to post many text boxes into one form.

The problem is, only the last text box gets updated.

<form action='' method='post'>
<?php
    for ($i=0; $i<$count; $i++) {
        echo "<input type='text' name='".$i."' />
        <input type='hidden' name='img_id' value='".$img_id."' />
        <input type='hidden' name='loop' value='".$i."' />";
    }
?>
<input type='submit' value='Update' name='update' /></form>
<?php 
    if (isset($_POST['update'])) {
        //now i need help
        $query = "UPDATE photos SET description = '".$_POST[$_POST['loop']]."' WHERE id = '".$img_id."'";
        $res = mysql_query($query) or die(mysql_error);
    }
?>

For example, loop goes 4 times, that means I have 4 text boxes.

In my script, which is not fully written here, the code updates only the last loop into the database.

Rob Baillie
  • 3,436
  • 2
  • 20
  • 34
sectumsempra
  • 31
  • 1
  • 1
  • 6

3 Answers3

1

First, you need to use the PHP array notation for the name attribute in your HTML. Otherwise you will have multiple inputs with the same name and only one of those will be posted

echo "<input type='text' name='".$i."' />
      <input type='hidden' name='img_id[]' value='".$img_id."' />
      <input type='hidden' name='loop[]' value='".$i."' />";

Now you can access those elements in your PHP as an array.

foreach ($_POST['img_id'] as $key=>$value) {
    // img_id's value is in $value
    // loop's value is in $_POST['loop'][$key]
}

Use the foreach to build your queries and after the loop execute all of them.


Your query is wide open to SQL injections. Use mysql_real_escape_string at least (even better: prepared statements, this will also be better performance wise since you always have the same query only different input). Also keep in mind that mysql_* functions are officially deprecated and hence should not be used in new code. You can use PDO or MySQLi instead. See this answer on SO for more information.

Community
  • 1
  • 1
kero
  • 10,647
  • 5
  • 41
  • 51
0

You need to make the names of your form elements unique so that they don't clash. One way would be to post them through as arrays instead:

Also, can't you use the same $count variable to loop over the potential results?

It looks like you're posting the hiddens with the name loop in order to pass through how many times over the loop you need to go. If not, you can post the loop variable through like this, but you use it in place of $count for your update loop. Also, I'd consider just adding that element in once, at the end - using the value of $count instead.

E.g. Something like this may work for you...

<form action='' method='post'>
<?php
    for ($i=0; $i<$count; $i++) {

        // How is img_id set here?  You have no indication in your example - I'll assume that you've put them into an array...

        echo "<input type='hidden' name='img_id[\"".$i.\""]' value='".$img_id[$i]."' />";
        echo "<input type='text' name='name[".$i."]' />";
    }
?>
<input type='submit' value='Update' name='update' /></form>
<?php 
    if (isset($_POST['update'])) {
        for ($i=0; $i<$count; $i++) {
            if ( array_key_exists( 'name', $_POST ) && is_array( $_POST['name'] ) && array_key_exists( $i, $_POST['name' ] ) {
                $query = "UPDATE photos SET description = '".$_POST['name'][$i]."' WHERE id = '".$_POST['img_id'][]."'";
                $res = mysql_query($query) or die(mysql_error);
            }
        }
    }
?>

Or, if you can't use $count:

<form action='' method='post'>
<?php
    echo( "<input type='hidden' name='loop' value='".$count."' />" );
    for ($i=0; $i<$count; $i++) {

        // How is img_id set here?  You have no indication in your example - I'll assume that you've put them into an array...

        echo "<input type='hidden' name='img_id[\"".$i.\""]' value='".$img_id[$i]."' />";
        echo "<input type='text' name='name[".$i."]' />";
    }
?>
<input type='submit' value='Update' name='update' /></form>
<?php 
    if (isset($_POST['update'])) {

        for ($i=0; $i<$_POST['loop']; $i++) {

            if ( array_key_exists( 'name', $_POST ) && is_array( $_POST['name'] ) && array_key_exists( $i, $_POST['name'] ) {
                $query = "UPDATE photos SET description = '".$_POST['name'][$i]."' WHERE id = '".$_POST['img_id'][$i]."'";
                $res = mysql_query($query) or die(mysql_error);
            }
        }
    }
?>
Rob Baillie
  • 3,436
  • 2
  • 20
  • 34
  • Kingkero has also made comment on your use of mysql and the fact that you are open to sql injection attacks - do not ignore it! – Rob Baillie Dec 15 '13 at 13:03
  • `$img_id` is undefined? Also your line with `array_key_exists` could be simplified to `if(isset($_POST['name][$i])) {` – AlexP Dec 15 '13 at 13:04
  • If `$img_id` is undefined in my script, then it is undefined in the provided example. As is `$count` - I have assumed that the supplied code is not complete. Your point about the `if` statement is taken, though I like to be complete on such points for beginners so that it's easier to see what *could* be missing or go wrong. It may behave the same, but the longer version conveys more information in a tutorial. – Rob Baillie Dec 15 '13 at 13:10
  • No, actually, it's not. – sectumsempra Dec 15 '13 at 13:12
  • @AmirRami No what's not? – Rob Baillie Dec 15 '13 at 13:14
  • Sorry, wasn't clear. I meant that `$img_id` would probably be `$_POST['img_id']` to avoid the "only the last text box gets updated" – AlexP Dec 15 '13 at 13:14
  • I think it's a fair point - that is likely the underlying problem. How foolish of me! – Rob Baillie Dec 15 '13 at 13:18
  • `$img_id` is defined but not written here. – sectumsempra Dec 15 '13 at 13:42
  • And, in your loop it never changes, meaning that all the udpates will apply to the same img_id. That may well be your problem. – Rob Baillie Dec 15 '13 at 13:59
-1

You have to use something like,

if (isset($_POST['update'])) {

for($i=0; $i<=$_POST['loop']; $i++){
Balaji Perumal
  • 830
  • 1
  • 6
  • 20