I am trying to build an array from a list of URL parameters to transact with a database. Basically I have several functions that do different things. (Insert, Update, etc) so depending on what it is specifically trying to do it may not need ALL columns in the database at all times.
So say I have these 5 potential parameters that might get passed: id,gr,bl,yw,re
If I am inserting a new row all 5 coulmns need some sort of value. If I am say updating the bl column then I only pass the id and bl parameters.
So I am doing this to build the query string(not actual code just example):
foreach($_GET as $key => $value) {
$key = strtolower($key);
if (preg_match($acceptedGETS,$key)) $$key = $value;
}
$table_col = array (
'p_id' => $_GET['id'],
'p_gr' => $_GET['gr'],
'p_bl' => $_GET['bl'],
'p_yw' => $_GET['yw'],
'p_re' => $_GET['re']);
$vstring = implode(',' , $table_col);
Now this works perfectly, as long as ALL variable keys have a value (or not NULL). My question is, how can I build a string from an array, but exclude the keys that don't get passed values. Right now if keys are missing values I get this for example:
URL: http://www.mysite.com/myscript.php?id=4&re=9 would get me:
4,,,,9
when I need to just get 4,9
Thanks!
mysql_real_escape_string()
before adding them to a query, if you're not using prepared statements. – rid Apr 16 '12 at 22:07