Jump to content

Function isn't doing anything


Aureole

Recommended Posts

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' );
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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.