0

I would like to update information that is already in the database, but using an array. I get 'successfully update' message white the data in the database is not updated.

the following is the function I use to do the insertion:

function update_knowledge_modules($update_data,$value)
{   
    $update = array();
    array_walk($update_data,'array_clean');

    foreach($update_data as $field=>$data)
    {
        $update[] = '.$field. = \''.$data.'\'';
    }



    $query = "UPDATE knowledge_modules SET".implode(', ',$update)."WHERE curr_code =$value";

    mysql_query($query)
    or die(mysql_error);

}

<?php

                    if(isset($_GET['success']) == true && empty($_GET['success'])==true)
                        {
                            echo 'Changed successfully';    
                        }
                    else
                        {
                            if(empty($_POST)== false && empty($errors)== true ) 
                                {
                                    $update_module = array(
                                    'KM_Number'=>$_POST['KM_Number'],
                                    'KM_Title'=>$_POST['KM_Title'],
                                    'NQF_Level'=>$_POST['NQF_Level'],
                                    'Credits'=>$_POST['Credits']


                                    );

                                    update_knowledge_modules($update_module,$_SESSION['Curr_Code']); 
                                    header('Location:edit_km_details.php?success');
                                    exit();
                                }
                        else if(empty($errors)== false)
                            {
                                echo output($errors);
                            }

              ?>

            <form action="edit_km_details.php" method="POST">
user1783675
  • 346
  • 2
  • 7
  • 25
  • 2
    `$update[] = '.$field. = \''.$data.'\'';` will output the text "$field2" not the content of the `$field` variable, try `$update[] = "$field = '$data'";` instead? – Stu Dec 13 '12 at 17:45
  • Have you echoed out the query to make sure it is correct? – Mattt Dec 13 '12 at 17:46
  • Can you post example of final query that you run. `$query` – E_p Dec 13 '12 at 17:46
  • yes, @Stu thats the output I get, but I would like to get the content instead – user1783675 Dec 13 '12 at 17:53
  • I just got the following error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.$field. = '120399111-KM-01', .$field. = 'skdjfas', .$field. = 'l5', .$field. = ' at line 1 – user1783675 Dec 13 '12 at 17:56
  • `$update[] = "$field = '$data'";` or `$update[] = $field.' = "'.$data.'"';` might work for you. As for using quotes you might want to read this: [PHP: different quotes?](http://stackoverflow.com/questions/1318028/php-different-quotes) – Stu Dec 13 '12 at 19:05

2 Answers2

1

Well, first of all, you are outputting the "changed successfully" message solely based on $_GET['success'] being truthy. It has nothing to do with whether you had success or failure in your update_knowledge_modules function call at all, which seems odd.

Second of all, I don't see anywhere where you are actually making a database connection.

Third, your query is undoubtedly being formed improperly. Look at this:

$update[] = '.$field. = \''.$data.'\'';

you are going to get a literal $field and backslashes in your query string. Try this:

$update[] = $field . " = '" . $data . "'";

Also where you put your imploded array in the final query, you don't have spaces after SET and before WHERE.

Anytime you are having problems with a query, just var_dump it and run it on the database directly to see why it isn't working and look at your errors, including mysql errors.

Finally, you should not be using the mysql_* family of functions. They are deprecated.

Mike Brant
  • 70,514
  • 10
  • 99
  • 103
0

Try: $update[] = $field . " = '" . $data . "'";

Output:

Array
(
    [0] => KM_Number = 'blah'
)
SeanWM
  • 16,789
  • 7
  • 51
  • 83