rage4848 Posted October 16, 2010 Share Posted October 16, 2010 Hi all, I have a data base with the following table: ---member_data--- id ^ mid ^fieldname^ fieldvalue 1 - 2 - applicant - Seth Smith 2 - 4 - firstname - Chris 3 - 4 - lastname - Cooper 4 - 7 - full_name - Joey Jones 5 - 7 - occupation - Programmer 6 - 7 - phone - 800-555-1212 And below is a snippet that I use to display the selected members data for editing ------------------------------- <table> <tr> <td>Name:</td> <td>Value:</td> </tr> <?php $query = "SELECT * FROM member_data WHERE mid='$mid'"; $result= mysql_query($query); while($row = mysql_fetch_array($result)) { $fieldname = $row["fieldname"]; $fieldvalue = $row["fieldvalue"]; ?> <tr> <td><input type="text" name="<?php echo $fieldname;?>" value="<?php echo $fieldname;?>"></td> <td><input type="text" name="<?php echo $fieldvalue;?>" value="<?php echo $fieldvalue;?>"></td> <?php } ?> </tr> </table> ------------------------------- My question is: - using the Db example above: - lets say Ive selected mid#7 (member id 7) to edit which is displayed using the snippet code from above. - once displayed how do I edit mid#7 given the fact that mid#7 is located on 3 seperate rows from the same table? Thank you, Dave *ALSO - I tried the snippet below which did Not work. foreach($_POST as $key => $val) { $sql = "UPDATE member_data SET fieldname='$key', fieldvalue='$val' WHERE mid='$mid'"; $result = mysql_query($sql); } Quote Link to comment https://forums.phpfreaks.com/topic/216002-update-multi-rows-with-same-id-in-1-table/ Share on other sites More sharing options...
litebearer Posted October 16, 2010 Share Posted October 16, 2010 An initial thought, Wouldn't you table structure be more effective and useful as... id|applicant|firstname|lastname|occupation|phone Quote Link to comment https://forums.phpfreaks.com/topic/216002-update-multi-rows-with-same-id-in-1-table/#findComment-1122752 Share on other sites More sharing options...
rage4848 Posted October 16, 2010 Author Share Posted October 16, 2010 you see - I already have a script where you can: Type in any random external URL (must be URL of Form page), then click Submit. Next, the script grabs ALL the name attributes + value attributes from all of the <INPUT TAGs> within that Form page. Then - those name attributes + value attributes are INSERTED into the Db. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: *Example below takes (2) random Form pages and... :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: RANDOM FORM PAGE (a): ---------------------------------------- <input type="text" name="fname" value="Sam"> <input type="text" name="lname" value="Smith"> <input type="text" name="anything" value="whatever"> WOULD BE INSERTED IN Db LIKE: ---------------------------------------- id ^ mid ^fieldname^ fieldvalue 1 - 1 - fname - Sam 2 - 1 - lname - Smith 3 - 1 - anything - whatever :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: *Now lets Grab/Insert a different random Form.. (see below) :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: RANDOM FORM PAGE (b): ---------------------------------------- <input type="text" name="user" value="Richard"> <input type="text" name="alias" value="Rambo"> <input type="text" name="music" value="jazz"> <input type="text" name="age" value="22"> <input type="text" name="car" value="Honda"> APPENDED IN Db NOW LOOKS LIKE: ---------------------------------------- id ^ mid ^fieldname^ fieldvalue 1 - 1 - fname - Sam 2 - 1 - lname - Smith 3 - 1 - anything - whatever 4 - 2 - user - Richard 5 - 2 - alias - Rambo 6 - 2 - music - jazz 7 - 2 - age - 22 8 - 2 - car - Honda :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: You see - the script processes random external Form pages, so the <INPUT TAG> name + value attributes are NOT unique and will vary majority of the time from Form to Form. I just dont know how to UPDATE them in the Db. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: Quote Link to comment https://forums.phpfreaks.com/topic/216002-update-multi-rows-with-same-id-in-1-table/#findComment-1122770 Share on other sites More sharing options...
rage4848 Posted October 19, 2010 Author Share Posted October 19, 2010 Hi, this question seems to be more difficult than I anticipated. so I've attached a graphic visual to in hopes of resolving this post. thanks again. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/216002-update-multi-rows-with-same-id-in-1-table/#findComment-1123770 Share on other sites More sharing options...
sasa Posted October 19, 2010 Share Posted October 19, 2010 try <?php foreach($_POST as $key => $val) { $sql = "UPDATE member_data SET fieldvalue='$val' WHERE mid='$mid' AND fieldname='$key'"; $result = mysql_query($sql); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/216002-update-multi-rows-with-same-id-in-1-table/#findComment-1123960 Share on other sites More sharing options...
ultimatum Posted October 19, 2010 Share Posted October 19, 2010 Simple fix, instead of having this: $fieldname = $row["fieldname"]; $fieldvalue = $row["fieldvalue"]; replace it with: $fieldname = $row["fieldname"]; $fieldvalue = $row["id"]; Basically, you want to update the row with that unique ID since you already have the row you need when you requested the needed rows WHERE mid = X. Quote Link to comment https://forums.phpfreaks.com/topic/216002-update-multi-rows-with-same-id-in-1-table/#findComment-1123966 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.