Re: MySql Update.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



How about:

function do_query ($table, $fields, $where)
{
	$sql = 'update ' . $table . ' Set ';
	foreach ($fields as $k=>$v)
		$sql = $k . ' = \'' . $v . '\',';
	return mysql_query ($sql);
}

Not sure if it adapts 100% to your case but you can probably fix it
up...


Marco


-- 
------------
php|architect - The magazine for PHP Professionals
The monthly worldwide magazine dedicated to PHP programmers

Come visit us at http://www.phparch.com!
--- Begin Message ---


Making an update query that adapts to the number of fields that are to be 
updated

Is there anyway to do like a for loop to create the values part of the query 
then combine it with the first part of the string.

something like what i have below... although something that works correctly 
as this doesn't.
======================================

$table is the table name
$num_headers is the number of headers (fieldnames) for that table
$table_headers is an array with the names of the headers
$values is an array with the names of the input boxes on the previous page

======================================
$query1 = "UPDATE ".$table." SET " ;

$query2 = ( for ($x=0; $x <=$num_headers; $x++){
                   ". $table_headers[$x] . "=" . $values[$x] . " , "
                   }) ;

$query = $query1.$query2 ;

$result = mysql_query ($query) ;
etc...



_________________________________________________________________
Help STOP SPAM with the new MSN 8 and get 2 months FREE*  
http://join.msn.com/?page=features/junkmail


-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


--- End Message ---
-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux