更新查询无法正常工作

时间:2011-10-21 20:10:25

标签: php mysql

好的......我正在使用mySQL数据库,这段代码片段是从flex编码swf调用的登录过程的一部分

代码基本上做了两件事:

1)检查记录是否存在,然后检查记录是否存在

如果记录存在且超过六个月,则会执行更新查询

2)如果没有记录,它将使用插入

发生了什么......插件工作得很好...更新查询有效,但公司和LastName的值被删除,数据库中没有存储值

为什么呢?这是一千万美元的问题......为什么此代码的更新部分不更新公司和LastName字段?

以下代码

//此函数处理来自flex中的“更新联系人信息”弹出窗口的请求,以更新数据库中的记录

function updateContactInformation() {

    //Initialize variable for result
    $Result = false;

    //ESTABLISH A CONNECTION WITH THE DATABASE
    global $Return;
    global $mysql;  
    global $username;


    //ACQUIRE VALUES FROM THE HTTPS REQUEST(FLEX)
    //mysql_real_escape_string is used to ensure no code is injected into the input fields 
    $uUCI_MSUN = mysql_real_escape_string($username); //value used in DB to associate login username with the users formal name

    $uUCI_firstname = mysql_real_escape_string($_POST['firstname']);//first name of user
    $uUCI_lastname = mysql_real_escape_string($_POST ["lastname"]);//last name of user
    $uUCI_company = mysql_real_escape_string($_POST ["company"]);//Name of users company
    $uUCI_email = mysql_real_escape_string($_POST["email"]); //email of the user
    $uUCI_phone = mysql_real_escape_string($_POST["phone"]); //phone # of the user
    //** Note: we do not collect time as the database will automatically update the Last_Updated_Date field with a new timestamp when the record is added or modified

    //CHECK TO SEE IF A RECORD EXISTS FOR THE USER ***by checking number of rows returned in a query for login username
    if(mysql_num_rows(mysql_query("SELECT MS_UserName FROM usercontactinformation WHERE MS_UserName = '" . $uUCI_MSUN . "'"))){
        // UPDATE RECORD IN DATABASE 
            $query2 = "UPDATE usercontactinformation SET FirstName = '" . $uUCI_firstname . "', LastName = '" . $uUCI_lastname . "', Company = '" . $uUCI_company . "', Email = '" . $uUCI_email . "', Phone = '" . $uUCI_phone ."' WHERE idUserContactInformation = " . getUID($username) . " ;";
            //send Request to mySQL
            $Result = mysql_query($query2, $mysql);


    } else {
        //INSERT NEW RECORD INTO DATABASE
            $query ="INSERT INTO usercontactinformation (MS_UserName,FirstName,LastName,Company,Email,Phone) VALUES('" . $uUCI_MSUN . "','" . $uUCI_firstname . "','" . $uUCI_lastname . "','" . $uUCI_company . "','" . $uUCI_email . "','" . $uUCI_phone . "');";
            //send Request to mySQL
            $Result = mysql_query($query, $mysql);


    }


    //RETURN A RESULT TO FLEX

    if ($Result) {
        $Return .= "<SuccessCode>1</SuccessCode>";
    } else {
        $Return .= "<SuccessCode>0</SuccessCode>";
    }

}

function getUID($username) {
    global $mysql;      //access Global mysql connection

    //Create Query to verify user exists and check difference between current date and Date Last Modified for the Users Contact Information
    $query = "Select idUserContactInformation from mydatabasename.UserContactInformation where MS_username = '" . $username . "'";
    //Send The Query To the SQL server
    $result = mysql_query($query, $mysql); 
    //parse results and return access level to calling function

    while ( $User = mysql_fetch_object( $result ) ) { 
        return $User->idUserContactInformation;

    }

}
$Return .= "</Result>";
print ($Return)

Somone要求提供表单值...以下代码是flex的一个片段,它将表单值传递给PHP文件

public function useHttpService():void {                //Alert.show("Use HTTPS“);

            service = new HTTPService();
            service.method = "POST";
            service.useProxy  = false;
            service.url = parentApplication.relativeDir + "/somepath/phpfileprocessinginformation.php";
            service.request.req = "updateContactInformation";
            service.request.username = parentApplication.User.username;
            service.request.password = parentApplication.User.password;

            //pass user supplied new information to query
            service.request.firstname = firstname.text;
            service.request.lastname = lastname.text;
            service.request.company = company.text;
            service.request.email = email.text;
            service.request.phone = phone.text;


            service.addEventListener(ResultEvent.RESULT, httpResult);
            service.addEventListener(FaultEvent.FAULT, httpFault);

            service.send();
        }

2 个答案:

答案 0 :(得分:1)

在两行代码中有额外的空格,您应该获取这些值:

... $_POST ["lastname"]);//last name of user
... $_POST ["company"]);//Name of users company

这与以下不一样:

... $_POST["lastname"]);//last name of user
... $_POST["company"]);//Name of users company

HTH。

答案 1 :(得分:0)

不是一个大的PHP人 - 在查看MySQL内容时发现了这个问题 - 所以我不确定它是否有效,但是在设置lastname和company变量的地方,你有$之间的空格_POST和括号([),这可能是问题吗? (其他人没有空间。)