Aureole Posted February 11, 2008 Share Posted February 11, 2008 Does anyone have any idea why this isn't working? I'm stumped. The output is: UPDATE `members` SET WHERE `mem_id` = '1' When it should be a little more like: UPDATE `members` SET `mem_title` = 'Testing...', `mem_profile_message` = 'Testing...' WHERE `mem_id` = '1' <?php function quick_update( $table, $array, $where = '' ) { compile_update_array( $array ); $query = "UPDATE `{$table}` SET {$cdata}"; if( $where !== '' ) { $where = explode( '|', $where ); $query .= " WHERE `{$where[0]}` = '{$where[1]}'"; } echo( $query ); exit; } function compile_update_array( $data ) { $cdata = ''; foreach( $data as $k => $v ) { if( is_numeric( $v ) and intval( $v ) == $v ) { $cdata .= '`' . $k . '`' . " = " . $v . ","; } else { $cdata .= '`' . $k . '`' . " = '" . $v . "',"; } } $cdata = preg_replace( "/,$/", "", $cdata ); return $cdata; } quick_update( 'members', array( 'mem_title' => 'Testing...', 'mem_profile_message' => 'Testing...' ), 'mem_id|1' ); ?> Quote Link to comment Share on other sites More sharing options...
aschk Posted February 11, 2008 Share Posted February 11, 2008 Yeah where's {$cdata} coming from? Because the only scope it exists in is the compile_update_array function, NOT outside it. Quote Link to comment Share on other sites More sharing options...
Aureole Posted February 11, 2008 Author Share Posted February 11, 2008 I also tried this and it didn't work, so that's not it: $whatever = compile_update_array( $array ); $query = "UPDATE `{$table}` SET {$whatever}"; You are right about the scope, I forgot about that... but even when I assign the result of compile_update_array() to a variable inside the other function, then use it - it still doesn't work. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 11, 2008 Share Posted February 11, 2008 I made a few changes to your code: <?php function quick_update( $table, $array, $where = '' ) { $cdata = compile_update_array( $array ); $query = "UPDATE `$table` SET $cdata"; if( $where !== '' ) { $where = explode( '|', $where ); $query .= " WHERE `$where[0]` = '$where[1]'"; } return( $query ); } function compile_update_array( $data ) { $cdata = ''; foreach( $data as $k => $v ) { if( is_numeric( $v ) and intval( $v ) == $v ) { $cdata .= '`' . $k . '`' . " = " . $v . ","; } else { $cdata .= '`' . $k . '`' . " = '" . $v . "',"; } } $cdata = preg_replace( "/,$/", "", $cdata ); return $cdata; } $query = quick_update( 'members', array( 'mem_title' => 'Testing...', 'mem_profile_message' => 'Testing...' ), 'mem_id|1' ); echo $query . "<br>\n"; ?> You should always return values from a function. Using echo from within the function can cause output to placed in places you don't want it. I got rid of the "{ }" where they were not needed. When I tested my script, it seemed to work correctly. Ken Quote Link to comment Share on other sites More sharing options...
Aureole Posted February 11, 2008 Author Share Posted February 11, 2008 I know not to echo from within a function, I was just doing that to test it. Thanks a lot for helping, I'll try your code now. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.