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">
6
  • 2
    $update[] = '.$field. = \''.$data.'\''; will output the text "$field2" not the content of the $field variable, try $update[] = "$field = '$data'"; instead? Commented Dec 13, 2012 at 17:45
  • Have you echoed out the query to make sure it is correct? Commented Dec 13, 2012 at 17:46
  • Can you post example of final query that you run. $query Commented Dec 13, 2012 at 17:46
  • yes, @Stu thats the output I get, but I would like to get the content instead Commented Dec 13, 2012 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 Commented Dec 13, 2012 at 17:56

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Output:

Array
(
    [0] => KM_Number = 'blah'
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.