参数问题

时间:2013-07-22 16:01:13

标签: php pdo

我一直在尝试我的PHP技能,当我尝试将我的Android应用程序中的信息发送到PHP时,它似乎只发送参数名称(数据库显示:Lname作为示例。)到数据库。我们使用PDO作为与MySQL数据库通信的方式。

以下编码如下:

$query = "INSERT INTO Customer ( Lname, Fname, Address, City, State, ZIP, Phone, myusername, mypassword ) VALUES ( ':Lname', ':Fname', ':Address', ':City', ':State', ':ZIP', ':Phone', ':myusername', ':mypassword')";

//Again, we need to update our tokens with the actual data:
$query_params = array(
    ':Lname' => $_POST['LName'],
    ':Fname' => $_POST['FName'],
    ':Address' => $_POST['Address'],
    ':City' => $_POST['City'],
    ':State' => $_POST['State'],
    ':ZIP' => $_POST['ZIP'],
    ':Phone' => $_POST['Phone'],
            ':myusername' => $_POST['username'],
            ':mypassword' => $_POST['password']
);

//time to run our query, and create the user
try {
    $stmt   = $db->prepare($query);
    $result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
    // For testing, you could use a die and message. 
    //die("Failed to run query: " . $ex->getMessage());

    //or just use this use this one:
    $response["success"] = 0;
    $response["message"] = $ex->getMessage();
    die(json_encode($response));
}

2 个答案:

答案 0 :(得分:1)

您在查询字符串中包含了文字值。

$query = "INSERT INTO Customer ( Lname, Fname, Address, City, State, ZIP, Phone, myusername, mypassword ) 
VALUES ( ':Lname', ':Fname', ':Address', ':City', ':State', ':ZIP', ':Phone', ':myusername', ':mypassword')";

应该是

$query = "INSERT INTO Customer ( Lname, Fname, Address, City, State, ZIP, Phone, myusername, mypassword ) 
VALUES ( :Lname, :Fname, :Address, :City, :State, :ZIP, :Phone, :myusername, :mypassword)";

答案 1 :(得分:0)

您需要从SQL值中删除引号,因为它被解释为文字字符串。如果你删除它们,你应该都很好:)

$query = "INSERT INTO Customer ( Lname, Fname, Address, City, State, ZIP, Phone, myusername, mypassword ) VALUES ( ':Lname', ':Fname', ':Address', ':City', ':State', ':ZIP', ':Phone', ':myusername', ':mypassword')";