nehrav Posted October 12, 2009 Share Posted October 12, 2009 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 Link to comment https://forums.phpfreaks.com/topic/177404-solved-update-the-selected-existing-records-of-database/ Share on other sites More sharing options...
Adam Posted October 12, 2009 Share Posted October 12, 2009 ...And how can we help you based on that? Link to comment https://forums.phpfreaks.com/topic/177404-solved-update-the-selected-existing-records-of-database/#findComment-935474 Share on other sites More sharing options...
nehrav Posted October 12, 2009 Author Share Posted October 12, 2009 ...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 Link to comment https://forums.phpfreaks.com/topic/177404-solved-update-the-selected-existing-records-of-database/#findComment-935484 Share on other sites More sharing options...
nehrav Posted October 13, 2009 Author Share Posted October 13, 2009 anybody suggest me something.......... with the code......... Link to comment https://forums.phpfreaks.com/topic/177404-solved-update-the-selected-existing-records-of-database/#findComment-935943 Share on other sites More sharing options...
Adam Posted October 13, 2009 Share Posted October 13, 2009 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. Link to comment https://forums.phpfreaks.com/topic/177404-solved-update-the-selected-existing-records-of-database/#findComment-935945 Share on other sites More sharing options...
nehrav Posted October 13, 2009 Author Share Posted October 13, 2009 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) Link to comment https://forums.phpfreaks.com/topic/177404-solved-update-the-selected-existing-records-of-database/#findComment-935959 Share on other sites More sharing options...
Adam Posted October 13, 2009 Share Posted October 13, 2009 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. Link to comment https://forums.phpfreaks.com/topic/177404-solved-update-the-selected-existing-records-of-database/#findComment-935963 Share on other sites More sharing options...
nehrav Posted October 13, 2009 Author Share Posted October 13, 2009 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......... Link to comment https://forums.phpfreaks.com/topic/177404-solved-update-the-selected-existing-records-of-database/#findComment-935968 Share on other sites More sharing options...
Adam Posted October 13, 2009 Share Posted October 13, 2009 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... Link to comment https://forums.phpfreaks.com/topic/177404-solved-update-the-selected-existing-records-of-database/#findComment-935971 Share on other sites More sharing options...
Adam Posted October 13, 2009 Share Posted October 13, 2009 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. Link to comment https://forums.phpfreaks.com/topic/177404-solved-update-the-selected-existing-records-of-database/#findComment-935975 Share on other sites More sharing options...
nehrav Posted October 13, 2009 Author Share Posted October 13, 2009 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.............. Its getting complicated.... Link to comment https://forums.phpfreaks.com/topic/177404-solved-update-the-selected-existing-records-of-database/#findComment-935978 Share on other sites More sharing options...
Adam Posted October 13, 2009 Share Posted October 13, 2009 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'> Link to comment https://forums.phpfreaks.com/topic/177404-solved-update-the-selected-existing-records-of-database/#findComment-935989 Share on other sites More sharing options...
nehrav Posted October 13, 2009 Author Share Posted October 13, 2009 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..... 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 Link to comment https://forums.phpfreaks.com/topic/177404-solved-update-the-selected-existing-records-of-database/#findComment-936002 Share on other sites More sharing options...
Adam Posted October 13, 2009 Share Posted October 13, 2009 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? Link to comment https://forums.phpfreaks.com/topic/177404-solved-update-the-selected-existing-records-of-database/#findComment-936045 Share on other sites More sharing options...
nehrav Posted October 13, 2009 Author Share Posted October 13, 2009 Don't b sorry buddy........it's not ur mistake if I am so dumb or short of knowledge......... 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........ Link to comment https://forums.phpfreaks.com/topic/177404-solved-update-the-selected-existing-records-of-database/#findComment-936059 Share on other sites More sharing options...
Adam Posted October 13, 2009 Share Posted October 13, 2009 Could you show the full code as you have it now? Link to comment https://forums.phpfreaks.com/topic/177404-solved-update-the-selected-existing-records-of-database/#findComment-936084 Share on other sites More sharing options...
nehrav Posted October 13, 2009 Author Share Posted October 13, 2009 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>"; } } } Link to comment https://forums.phpfreaks.com/topic/177404-solved-update-the-selected-existing-records-of-database/#findComment-936087 Share on other sites More sharing options...
Adam Posted October 13, 2009 Share Posted October 13, 2009 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. Link to comment https://forums.phpfreaks.com/topic/177404-solved-update-the-selected-existing-records-of-database/#findComment-936097 Share on other sites More sharing options...
nehrav Posted October 13, 2009 Author Share Posted October 13, 2009 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........ how I can categorise this thread in solved ones?? Link to comment https://forums.phpfreaks.com/topic/177404-solved-update-the-selected-existing-records-of-database/#findComment-936137 Share on other sites More sharing options...
Adam Posted October 13, 2009 Share Posted October 13, 2009 No problem... And it's bottom left whilst you're viewing the thread. Link to comment https://forums.phpfreaks.com/topic/177404-solved-update-the-selected-existing-records-of-database/#findComment-936145 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.