PDA

View Full Version : Table update function


Mythotical
01-17-2008, 06:27 PM
Ok I have this function:
function saveSettings()
{
global $db;

$xsdl_active = $_REQUEST['xsdl_active'];
$server_path = $_REQUEST['server_path'];
$xsdl_closed = $_REQUEST['xsdl_closed'];
$nav_active = $_REQUEST['nav_active'];
$xsdl_file_name = $_REQUEST['xsdl_file_name'];

$db->query_write("
UPDATE `" . TABLE_PREFIX . "xsdl_settings`
SET set_value= array('$xsdl_closed', '$server_path', '$xsdl_active', '$nav_active', '$xsdl_file_name') WHERE varname = array('xsdl_closed', 'server_path', 'xsdl_active', 'nav_active', 'xsdl_file_name')
");

return $db->insert_id();
}

I'm not sure how I should use array so it will run as I show above. I tried that but it failed, gonna keep trying while I wait for a reply here but could anyone give me the proper way to handle array's like above?

Thanks
Steve

Opserty
01-17-2008, 08:14 PM
Is this using vBulletin back-end? If so make sure you read this: Using the vBulletin Input Cleaner (https://vborg.vbsupport.ru/showthread.php?t=119372), before you go any further in reading my post.

To store an array in a database row you need to serialize it in a string. See the PHP manual for information on serialize() (http://uk.php.net/manual/en/function.serialize.php) and unserialize() (http://uk.php.net/manual/en/function.unserialize.php).

An example:

$arrayone = array('bla', 'bla', 'bla');
$arrayone = serialize($arrayone);
// $db->query_write("UPDATE....SET somecol = ". $arrayone ."...

//=================================

// $db->query_read("SELECT * FROM....
// $row = $db->fetch_array...
$data = unserialize($data['somecol']);

Adrian Schneider
01-17-2008, 08:23 PM
What you want is a CASE statement.

UPDATE table
SET set_value =
CASE
WHEN set_varname = 'X'
THEN $value_for_x

WHEN set_varname = 'Y'
THEN $value_for_y
END
WHERE set_varname IN ('X', 'Y')

Opserty
01-17-2008, 08:30 PM
Oh whoops I didn't see all of your query Steve M. My apologies. :erm: