无法将数据POST到数据库中

时间:2014-06-16 07:35:28

标签: php android mysql rest

我正在尝试使用POST从我的Android应用程序添加一些数据(字符串)。我目前正在使用Advanced Rest Client测试代码,但是从那里开始测量。

编辑:为了更清楚地说明我的问题是什么 - 它没有取值并分配它们的代码有什么问题?为什么跳过第一个if?

编辑2:我正在使用chrome.google.com/webstore/detail/advanced-rest-client / ...来测试此POST。

注意:

echo @name; - >它什么都不做,不回应一件事

现在,它会自动进入第三个,说缺少所需的字段。

我尝试在$ name上使用var_dump,它声明了以下内容:意外的令牌s =>这没有任何意义。

提前感谢您的答案,如果您需要更多信息,我很乐意提供。

这是代码:

$app->post('/users', function() use ($app, $mysql) {
    $name = $app->request->post('name');
    $email = $app->request->post('email');
    $beer_type = $app->request->post('beer_type');
    $favorite_beer = $app->request->post('favorite_beer');
    $favorite_drink = $app->request->post('favorite_drink');

    echo $name;

 if (!empty($name) and !empty($email) and !empty($beer_type) and !empty($favorite_beer) and !empty($favorite_drink)) {

 $insert = "INSERT INTO clients(name, email) VALUES({$name}, {$email})";
 $getid = "SELECT id_client FROM clients WHERE email = {$email}";
 $insert2 = "INSERT INTO preferences(client_id, beer_type, favorite_beer, favorite_drink) VALUES({$idvalue}, {$beer_type}, {$favorite_beer}, {$favorite_drink})";

$request = $mysql->query($insert);
$test = false;
$id = 0;
$test2 = false;
if ($request !== false) {
    $test = true;
}
if ($test === true) {
    $request = $mysql->query($getid);
    if ($request !== false) {
        $id = $request->fetch_all(MYSQLI_ASSOC);
    }
}
if ($id !== 0) {
    $request = $mysql->query($insert2);
    if ($request !== false) {
        $test2 = true;
    }
}

if ($test2 === true) {
        // successfully inserted into database
        $response["success"] = 1;
        $response["message"] = "Product successfully created.";

        // echoing JSON response
        echo json_encode($response);
    } else {
        // failed to insert row
        $response["success"] = 0;
        $response["message"] = "Oops! An error occurred.";

        // echoing JSON response
        echo json_encode($response);
    }
 }else {
    // required field is missing
    $response["success"] = 0;
   $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}

 });

1 个答案:

答案 0 :(得分:0)

我通过更改java代码解决了我的问题。我使用Ion Library进行POST,它运行得非常好。