简体   繁体   中英

updating data in mysql database using arrays

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">

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.

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

Output:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM