0

I want to update fields in database. here is the code in which i fetch the records from database and show them in a form.

<form name="create_album" method="POST" action="" style="width:auto;">

<label for="First Name">Album Name</label>

<input type="text" name="album" id="album" placeholder="Album Name"value="<?php echo $album_name; ?>" />


<?php 
$result = mysql_query("select * from user_uploads where album = '$album_name' ");
while($row=mysql_fetch_array($result))
{
?>
<img src="<?php echo $row['image_name']; ?>" height="140" width="140" /> 
<input type="text" name="des[]" value="<?php echo $row['image_description']; ?>" placeholder="Image Description Here"/>
<input type="text" name="id[]" value="<?php echo $row['id']; ?>" placeholder="Image Description Here"/>

<?php } ?>
<input type="submit" name="submit" value="Upload Album"/>

but now when i want to update the records in database it update all the records into the table with the value of last input field. here is the php code.

  <?php 
if(IsSet($_POST["submit"]))
{
     $album = $_POST["album"];
             $des=$_POST["des"];
    foreach($_POST['id'] as $id)
    {

$update_qry = "update user_uploads set album='$album', image_description='$des' where id = '$id' ";

     $result_update_image = mysql_query($update_qry);
  if (!$result_update_image) 
  {
            header("location:create_album.php?errmsg=Album Not Updated");
        }



        }

    }
    }
 ?>

But i dont know how to get all the values of des form array to update in table according to id.

2
  • what is your problem ? Commented Nov 11, 2013 at 8:43
  • <input type="text" name="des[<?php echo $row['id']; ?>]" value="<?php echo $row['image_description']; ?>" placeholder="Image Description Here"/> this field is creadted dynamically from db records. now i want to update each image description simuntainously. but my code update all records by last image description value. Commented Nov 11, 2013 at 8:47

2 Answers 2

2

Change the code like this and check.

   $i=0;
    $des=$_POST["des"];
    foreach($_POST['id'] as $id)
        {

          $update_qry = "update user_uploads set album='".$album."', image_description='".$des[$i]."' where id = '".$id."' ";

         $result_update_image = mysql_query($update_qry);
    $i++;
    }
Sign up to request clarification or add additional context in comments.

Comments

1

try this

<input type="text" name="des[]" value="<?php echo $row['image_description']; ?>" placeholder="Image Description Here"/>

I have changed the name.

11 Comments

i already tried this but same problm. only last image description value update in all records
@ApoorvaChauhan, just try print of post values as print_r($_POST) and check what values your getting in variables. what is that $album_name value
@ApoorvaChauhan check your where condition.Will your album_name be unique as the loop proceeds??
@Shafeeq if i print the values it printsonly last input field value. $album_ name value is the album name in which image description will be updated. album name successfully updated. in this code.
@sanal k yes album name is unique. but in a album there are several images. so i want to update each row where album name is $album_name simuntainously with the image name.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.