gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 19:20:27 24 4
gpt4 key购买 nike

我有一个包含 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com