Jump to content

[SOLVED] using an array in mysql?


Dragen

Recommended Posts

Okay, I've got an array which outputs like this using print_r()

Array ( [mon] => mon = '1', [tues] => tues = '1', [weds] => weds = '1', [thurs] => thurs = '1', [fri] => fri = '1', )

 

Each value is the day name then = '1'.. it's sometime 0, but that shouldn't matter at all. I'm trying to set this up to be in a mysql statement. At the moment my mysql statement looks like this:

$sqlup = "UPDATE staff SET mon = '" . $sday['mon'] . "', tues = '" . $sday['tues'] . "', weds = '" . $sday['weds'] . "', thurs = '" . $sday['thurs'] . "', fri = '" . $sday['fri'] . "', `desc` = '" . $desc . "' WHERE firstname = '" . $name['0'] . "' && surname = '" . $name['1'] . "'";

But I'm wanting to dynamically change all of the days with the values of my array. Is this possible?

Link to comment
Share on other sites

Not quite sure what you want to do.

 

You want to update the values of the fields 'mon', 'tues' etc with the 1s and 0s you've stored in your array? You already seem to be doing this in your SQL query?

 

Or you want to only update those fields held in the array, so if you don't have a 'weds' value, you don't want to update it?

 

I'm certain you can make your query more dynamic by using an array, just not sure what you want to achieve. http://uk3.php.net/manual/en/function.array-search.php might help.

Link to comment
Share on other sites

<?php

$array = array("field1" => "value1", "field2" => "value2");

$q = "UPDATE table SET ";

foreach($array as $field => $value) {
$q .= "'$field' = '$value', ";
}

?>

 

Not tested, but you get the idea :)

Link to comment
Share on other sites

Array ( [mon] => '1', [tues] => '1', [weds] => '1', [thurs] => '1', [fri] => '1', );

$sqlup = "UPDATE staff SET mon = '" . $sday['mon'] . "', tues = '" . $sday['tues'] . "', weds = '" . $sday['weds'] . "', thurs = '" . $sday['thurs'] . "', fri = '" . $sday['fri'] . "', `desc` = '" . $desc . "' WHERE firstname = '" . $name['0'] . "' && surname = '" . $name['1'] . "'";

 

my only question is... if your just having it come from the array every time... why not put the #s in manually?

Link to comment
Share on other sites

thanks for all the replies!

 

Thanks also to Chigly who sorted the problem ;D I never realised that I could 'add on' to a variable by using '.=' instead of just '='.

That's solved the whole thing!

 

Thanks

 

note: the reason I don't want to do it manually is because the array is something which could change frequently so instead of editing all the mysql statements each time I can just change the one array and it does it all for me.

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.