0

我想更新数据库中已经存在的信息,但使用数组。我收到“成功更新”消息,表明数据库中的数据未更新。

以下是我用来进行插入的函数:

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

2 回答 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.

于 2012-12-13T17:55:31.530 回答
0

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

输出:

Array
(
    [0] => KM_Number = 'blah'
)
于 2012-12-13T17:53:40.897 回答