lindm Posted July 27, 2008 Share Posted July 27, 2008 With help from the board the much appreciated script below was written to update all columns of a mysql database with the corresponding $_POST value. It also had the option to leave out columns from this update. This script was used with one single table. Now I have a three table design of my mysql database and of course have trouble since to my understanding an update of multiple selected tables must be in the form of: SET table1.fieldname1 ='1', table2.fieldname2='2'etc while the code below produces SET fieldname1='1', fieldname2='2' etc Thow code below doesn't adjust for this and simply adding all tables to the UPDATE query as follows does not work: $q = 'UPDATE `table1, table2, table3` SET '. implode( ', ', $qArray ) .' WHERE `userName` = \''. $user2 .'\''; Is it possible to adjust this code for updating multiple tables or is it impossible? Original script if ( count($_POST) > 0 ) { # The names of any elements you don't want # added to mysql... most importantly the # submit button If you put the fields you # want added in their own array, there's no # need for this $doNotAdd = array( 'column1','column2','column3' ); # Create the array to store each query row $qArray = array(); # Loop through post data foreach ( $_POST as $key => $val ) # Make sure key isn't in 'doNotAdd' list if ( !in_array( $key, $doNotAdd ) ) $qArray[] = '`'. $key .'` = \''. mysql_real_escape_string( $val ) .'\''; # Verify there's actually data to insert if ( count($qArray) > 0 ) { # Build query $q = 'UPDATE `table` SET '. implode( ', ', $qArray ) .' WHERE `userName` = \''. $user2 .'\''; # Debug - incase stuff goes wrong remove # below # echo $q; # Check if query goes through if ( !mysql_query($q) ) # Query broked! echo 'Could not update set...'; } } } Link to comment https://forums.phpfreaks.com/topic/116834-solved-phpmysql-update-multiple-tables-with-all-_post/ Share on other sites More sharing options...
JasonLewis Posted July 27, 2008 Share Posted July 27, 2008 I believe the syntax to update multiple tables is the same as selecting, which would be something like this: $query = mysql_query("UPDATE table1 t1, table2 t2 SET t1.name='somethingelse', t2.gender='differentgender' WHERE t1.id='{$id}' AND t2.id='{$id}'"); Link to comment https://forums.phpfreaks.com/topic/116834-solved-phpmysql-update-multiple-tables-with-all-_post/#findComment-600799 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.