Canman2005 Posted May 20, 2010 Share Posted May 20, 2010 Hi all I have some PHP code which basically outputs the following form <input name="name[]" type="text" value="apple" /> <input name="product[]" type="text" value="mp3" /> <input name="id[]" type="hidden" value="2" /> <br /><br /> <input name="name[]" type="text" value="microsoft" /> <input name="product[]" type="text" value="software" /> <input name="id[]" type="hidden" value="10" /> <br /><br /> <input name="name[]" type="text" value="apple" /> <input name="product[]" type="text" value="software" /> <input name="id[]" type="hidden" value="11" /> when I submit the form, I want to run an update QUERY for everything on the form, using the following QUERY $sql = "UPDATE `items` SET `name` = '".$_POST['name']."', `product` = '".$_POST['product']."' WHERE `id` = '".$_POST['id']."'"; $query = mysql_query($sql); So it's almost like a need to run 3 updates scripts Is this possible? I thought maybe with a foreach() but i've no idea where to start. any ideas anyone? thanks all dave Quote Link to comment https://forums.phpfreaks.com/topic/202419-updating-multiple-records/ Share on other sites More sharing options...
Psycho Posted May 20, 2010 Share Posted May 20, 2010 Either three queries or one complex query. If you will only ever have three items, then three queries would probably be the most straitforward approach. If you will have many items, then you should probably do some testing to see what is the most efficient. Anyway, this is what the "complex" query would look like: $query = "UPDATE `items` SET `name` = CASE id WHEN {$_POST['id'][0]} THEN {$_POST['name'][0]} WHEN {$_POST['id'][1]} THEN {$_POST['name'][1]} WHEN {$_POST['id'][2]} THEN {$_POST['name'][2]} END product = CASE id WHEN {$_POST['id'][0]} THEN {$_POST['product'][0]} WHEN {$_POST['id'][1]} THEN {$_POST['product'][1]} WHEN {$_POST['id'][2]} THEN {$_POST['product'][2]} END WHERE id IN ({$_POST['id'][0]}, {$_POST['id'][0]}, {$_POST['id'][0]})" Of course, if the number of items is variable, you would want to script that: $itemCount = count($_POST['id']); $nameCases = ''; $productCases = ''; for($itemIdx=0; $itemIdx<$itemCount; $itemIdx++) { $nameCases .= "WHEN {$_POST['id'][$itemIdx]} THEN {$_POST['name'][$itemIdx]}\n"; $productCases .= "WHEN {$_POST['id'][$itemIdx]} THEN {$_POST['product'][$itemIdx]}\n"; } $query = "UPDATE `items` SET `name` = CASE id {$nameCases} END product = CASE id {$productCases} END WHERE id IN (" . implode(',', $_POST['id']) . ")"; Quote Link to comment https://forums.phpfreaks.com/topic/202419-updating-multiple-records/#findComment-1061302 Share on other sites More sharing options...
Canman2005 Posted May 20, 2010 Author Share Posted May 20, 2010 thanks man, that's a great help, ill have a try Quote Link to comment https://forums.phpfreaks.com/topic/202419-updating-multiple-records/#findComment-1061304 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.