Jump to content

[SOLVED] PHP/mysql update multiple tables with all $_POST


lindm

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.