【问题标题】:Trying to insert values into mysql using php at the back end and android as front end尝试在后端使用 php 并使用 android 作为前端将值插入 mysql
【发布时间】:2019-05-08 13:01:29
【问题描述】:

我是 php 新手,我正在尝试使用 xampp 服务器将我的 android 应用程序与我的计算机连接起来。首先,我尝试使用 Postman(rest 客户端)检查 php 代码。我试图通过邮递员发出请求来插入值,但正在插入空值。有人可以帮我吗

我已尝试使用其他 REST 客户端应用程序,但出现相同的错误。我使用 isset() 函数来检查值是否设置。值为空

<?php

if($_SERVER["REQUEST_METHOD"]=="POST") {
    require 'conn.php';//This is another php file for connecting php with 
    mysql
    register();
 } else{
    echo "Please give post request";
 }

function register()
{
    global $conn;

    if(isset($_POST['user']) || isset($_POST['pass'])){//Trying to check 
weather it is null or not

        $username = $_POST["user"];
        $password = $_POST["pass"];

        $query = "insert into login(username,password) 
                    values('$username','$password');";

        if(mysqli_query($conn,$query)) {
            echo"Success";
        } else {
            echo"failure";
        }
    }else{
        echo"Blank";//This is printing in that rest client
    }
    mysqli_close($conn);
 }
 ?>

【问题讨论】:

标签: php mysql


【解决方案1】:

按照设计,POST 请求方法请求 Web 服务器接受 body of the request message 中包含的数据,很可能 用于存储它。

相比之下,HTTP GET 请求方法从 服务器。作为 GET 请求的一部分,可以在其中传递一些数据 URL 的 query string,指定(例如)搜索词、日期 范围,或定义查询的其他信息。

来源:https://en.wikipedia.org/wiki/POST_(HTTP)

根据您上传的图片,您似乎正在尝试在 POST 方法中使用 Query Params 发送数据,这是错误的。查询参数应该用于标识服务器中的特定资源,而不是包含新资源。

为了正确地包含新资源,您必须通过 POST 发送数据,该数据包含在请求正文中。 Postman 提供了一个Body 选项卡来帮助您添加数据。 这是带有数据的 POST 方法的 example [1]。更完整的教程可以找到here

【讨论】:

    猜你喜欢
    • 2011-03-09
    • 1970-01-01
    • 2018-06-26
    • 2015-09-14
    • 1970-01-01
    • 1970-01-01
    • 2018-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多