php - 使用 php Rest api 将 Html 连接到 Mysql

标签 php html mysql api

我有一个包含 2 个字段名称和年龄的 html 表单,我创建了一个 MySQL 数据库,当使用 php API 提交到我的数据库时,我需要存储 html 中的值... 我熟悉 php,但我需要使用 API 在提交表单时从 html 页面发送值...我用于 html 和 API 的代码。

Index.html

<form class="col s12" method="post" action="">
<input id="name" type="text" name="Name">
<input id="name" type="text" name="Age">
   <button type="submit" name="btn_login">Request</button>
</form>

reg.php(api)

$app->post('/createstudent', function () use ($app) {


    verifyRequiredParams(array('Name', 'Age'));

    //Creating a response array
    $response = array();

    //reading post parameters
    $name = $app->request->post('Name');
    $age = $app->request->post('Age');


    //Creating a DbOperation object
    $db = new DbOperation();

    //Calling the method createStudent to add student to the database
    $res = $db->createStudent($name,$age);

    //If the result returned is 0 means success
    if ($res == 0) {
        //Making the response error false
        $response["error"] = false;
        //Adding a success message
        $response["message"] = "You are successfully registered";
        //Displaying response
        echoResponse(201, $response);

    //If the result returned is 1 means failure
    } else if ($res == 1) {
        $response["error"] = true;
        $response["message"] = "Oops! An error occurred while registereing";
        echoResponse(200, $response);

    //If the result returned is 2 means user already exist
    } 
});

现在我想知道在提交 html 表单时我应该进一步做什么来将值发送到数据库

最佳答案

查看 WAMP 示例之一。 http://www.homeandlearn.co.uk/php/php1p3.html

关于php - 使用 php Rest api 将 Html 连接到 Mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42479495/

相关文章:

mysql - phpmyadmin中的关系数据库

mysql - 我们可以使用nodeJs中的rest API从数据库查询中返回数组吗

php - 我应该如何在我的 php 文件和 html 文件中包含我的 json 代码

php - Gmail Oauth API 获取消息数

php - 包含或需要的文件是通过互联网传输还是仅在服务器上访问?

css - div 未使用可用空间(div :inline-block, 位置:固定)

html - 如何禁用 Bootstrap 链接悬停背景颜色?

php - 如何更改主导航栏的颜色,我无法通过 CSS 或 PHP 访问它

php - API - 处理嵌套资源 (Laravel)

php - 持续运行的 Gearman Worker