Jump to content

[SOLVED] update the selected existing records of database


nehrav

Recommended Posts

Hi frndz,

I am new to php and get stuck.... ::)

 

Edit.php > Update.php > Updateaction.php

 

i have 1 edit page displaying results existing in database with chkbox in front of every record and update btn. Its like

 

chkbox    Record    Update-btn

this red row is under while loop

 

and then update page which gives option to edit the values of selected chkboxes with update button

 

RecordtobeUpdated    Update-btn

 

Now problem is that when i made the changes in records of selected checkboxes and click update button. Its not updating the records in database on moving to updateaction.php

...And how can we help you based on that?

 

ok, u need the code??

 

Code for update page is

 

<?php
if(isset($_GET['id']) && is_array($_GET['id']))
{
$idarray=$_GET['id'];
foreach($idarray as $id)
{
	$update = mysql_query("select * from tbl_name WHERE id='$id'");
	while($uresult = mysql_fetch_array($update))
		{
			$id = $uresult['id'];
			$location = $uresult['location'];

		  echo "<tr>				  	
			<td align='left'>
			<input type='hidden' name='id' value='$id'></td>
			<td><input type='text' name='new_location' value='$location' size='45' maxlength='50'></td>
			<td align='center'><input type='submit' value='Update'></td>								
			</tr>";
		  }	  	
 }

}
?>

 

and code for updateaction is:

<?php
if(isset($_GET['id']) && is_array($_GET['id'])) 
		{			
			$idarray=$_GET['id'];
			foreach($idarray as $new_id)
			{
				$new_location = $_GET['new_location'];

				$uaction = mysql_query("UPDATE tbl_name SET location='$new_location' where id='$new_id'");				
				if($uaction)
				  {
				  echo "<tr><td>Successfully Updated</td></tr>";
				  }
				else
				{
				  echo "<tr><td>Updation Failed</td></tr>";
				}
			}
		}
?> 

 

I hope now you can help?......... :o :o

My guess is that you're not providing the correct data, probably at the input stage. For example:

 

<input type='hidden' name='id' value='$id'>

 

With each iteration of the loop you'll just be over-writing the value of "id"; therefore only passing a single string to the 'update page' instead of an array ( $idarray=$_GET['id'] ). Try a var_dump on $idarray and see what you get.

 

A possible solution is to pass the data as an array like this:

 

<input type='hidden' name='ids[]' value='$id'>

 

Obviously you have several inputs though so you may need to play around a bit. Perhaps use a fixed index instead to prevent getting any miss-matched/aligned data.

My guess is that you're not providing the correct data, probably at the input stage. For example:

 

<input type='hidden' name='id' value='$id'>

 

With each iteration of the loop you'll just be over-writing the value of "id"; therefore only passing a single string to the 'update page' instead of an array ( $idarray=$_GET['id'] ). Try a var_dump on $idarray and see what you get.

 

A possible solution is to pass the data as an array like this:

 

<input type='hidden' name='ids[]' value='$id'>

 

Obviously you have several inputs though so you may need to play around a bit. Perhaps use a fixed index instead to prevent getting any miss-matched/aligned data.

 

Its updating the values now but for the last field only out of a big list....

its like if i select 3 chkbox on edit page and get 3 fields to edit on update page..

 

after making changes in respective fields it moves to updateaction page,  records updated successfully message is appearing.

But in database its showing me......only the last record value get repeated/updated on all 3 fields

 

changes made by me in code are :

on update page

<input type='hidden' name='ids[]' value='$id'>

 

on updateaction, code modified is:

if(isset($_GET['ids[]']) && is_array($_GET['ids[]'])) 
		{			
			$idarray=$_GET['ids[]'];
			var_dump($idarray);
			foreach($idarray as $new_id)

It's not $_GET['ids[]'], it's just $_GET['ids']. Have you given thought to the other inputs though?

 

But in database its showing me......only the last record value get repeated/updated on all 3 fields

 

Yes that's because the value of $_GET['id'] was the last record to be selected, as each time you looped through a result you were over-writing the input.

It's not $_GET['ids[]'], it's just $_GET['ids']. Have you given thought to the other inputs though?

 

But in database its showing me......only the last record value get repeated/updated on all 3 fields

 

Yes that's because the value of $_GET['id'] was the last record to be selected, as each time you looped through a result you were over-writing the input.

 

ys i am using $_GET['id'] only

on using var_dump i recieve...

 

array(2) { [0]=> string(2) "55" [1]=> string(2) "58" }

 

I want each n every value to be updated but its filling/repeating the last value only to other selected fields also.........

Have you given thought to the other inputs though?

 

That's because you have the same problem with the other inputs, the last value selected is over-writing the input value. You need to send all the other inputs as arrays too...

By the way you'll want to look into the foreach $key / $k value in order select the same index from the other input arrays.

 

Can u write the piece of code Adam, plz, I am unable to follow.............. :shrug:

Its getting complicated....

Perhaps this will help...

 

foreach ($_GET['ids'] as $key => $id)
{
    echo $_GET['new_locations'][$key];
}

 

Remember though you need to send the "new_location" as an array. To match the example above:

 

<input type='text' name='new_locations[]' value='$location' size='45' maxlength='50'>

Perhaps this will help...

 

foreach ($_GET['ids'] as $key => $id)
{
    echo $_GET['new_locations'][$key];
}

 

Remember though you need to send the "new_location" as an array. To match the example above:

 

<input type='text' name='new_locations[]' value='$location' size='45' maxlength='50'>

 

Adam, after adding ur code on updateaction page

 

May be, I have done some thing wrong, but its not updating still.........

Is code is properly placed

its now become this... :-

if(isset($_GET['ids[]']) && is_array($_GET['ids[]'])) 
		{			
			$idarray=$_GET['ids[]'];
			var_dump($idarray);
			foreach($_GET['ids'] as $key => $id)
			{
			echo $_GET['new_locations'][$key];
}
$location = $_GET['new_locations'];
$uaction = mysql_query("UPDATE test_tbl SET location='???' where id='??'

what will be my querry now..... :confused:

its hard for me to follow now

 

Will there be any change in query too, as its not updating the values yet....

neither any error nor any updation/failed message is there

 

being honest, I am not much versed with $key => $id funda

I only showed you that code to give you a push in the right direction, not to use it literally. Sorry I might not have been very clear before. I think you're actually over complicating things. Once you have your inputs as arrays (I'm assuming they are called ids[] and new_locations[]) all you need to do is something like this:

 

foreach ($_GET['ids'] as $key => $id)
{
    $new_location = $_GET['new_locations'][$key];

    $update = mysql_query("update your_table set location='".$new_location."' where id='".$id."'");
}

 

Of course you'll need to add in some filtering and security, but, do you understand what I mean?

Don't b sorry buddy........it's not ur mistake if I am so dumb or short of knowledge......... :shrug:

 

I am learning new fundas from ur all replies......sooner or later I'll find out solution......yaa, I use the code but still result is same........blank page........

Could you show the full code as you have it now?

 

ok, my code on updateaction page is

if(isset($_GET['ids[]']) && is_array($_GET['ids[]'])) 
{			
	$idarray=$_GET['ids[]'];		
	foreach($_GET['ids'] as $key => $id)
	{
		$new_location = $_GET['new_locations'][$key];			
		$uaction = mysql_query("UPDATE test_tbl SET location='".$new_location."' where id='".$id."'");		
		if($uaction)
		  {
		  echo "<tr><td>Successfully Updated</td></tr>";
		  }
		else
			{
			  echo "<tr><td>Updation Failed</td></tr>";
			}
	 }
}

You only need:

 

if (isset($_GET['ids']))
{
    foreach ($_GET['ids'] as $key => $id)
    {
        $new_location = $_GET['new_locations'][$key];
        $uaction = mysql_query("UPDATE test_tbl SET location='".$new_location."' where id='".$id."'");

        if ($uaction)
        {
            echo "<tr><td>Successfully Updated</td></tr>";
        }
        else
        {
            echo "<tr><td>Updation Failed</td></tr>";
        }
    }
}

 

Remove the other parts. Although you should know by using if ($uaction) { .. you are only testing whether or not the query was successful - it may not have actually updated a row(s). You can use mysql_affected_rows to check the number of rows updated/inserted/etc. for a more accurate output.

Thanks Adam,

you were so calm.....in whole process....

Its updating the results now..............all b'coz of u

 

I am really thankful to u..........be my php mentor here on forum........ :P

 

how I can categorise this thread in solved ones??

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.