我正在尝试使用Slim Framework和MySQL制作寄存器系统。基本上,我只是想使用HTML表单将数据发布到SQL数据库,但它不起作用。
我尝试使用API使用发布,这很好。请帮助,这是我的UNI项目。我也是Slim Framework和PHP/API的新手。
大爱。
我的HTML代码:---------------------------------------------------------------------------------------------------
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<form method="post" action="/api/customer/add" data-ajax='false' id="from1">
<label for="fname">First Name : <span>*</span></label>
<input type="text" name="first_name" id="fname" placeholder="First Name">
<label for="lname">Last Name : <span>*</span></label>
<input type="text" name="last_name" id="lname" placeholder="Last Name">
<label for="email">Phone: <span>*</span></label>
<input type="email" id="email" name="phone" placeholder="Email"/>
<a id="click" href="" class="ui-btn" data-theme="b" data-inline="true"> Submit</a>
<a id="showUsers" href="" class="ui-btn" data-theme="b" data-inline="true"> Show Users </a>
</form>
</body>
</html>
我的PHP代码:-----------------------------------------------------------------------------------------
<?php
use PsrHttpMessageServerRequestInterface as Request;
use PsrHttpMessageResponseInterface as Response;
$app = new SlimApp;
$app->post('/api/customer/add', function(Request $request, Response $response){
$first_name = $request->getParam('first_name');
$last_name = $request->getParam('last_name');
$phone = $request->getParam('phone');
$sql = "INSERT INTO users (first_name, last_name,phone) VALUES
(:first_name,:last_name,:phone)";
try{
$db = new db();
$db = $db->connect();
$stmt = $db->prepare($sql);
$stmt->bindParam(':first_name', $first_name);
$stmt->bindParam(':last_name', $last_name);
$stmt->bindParam(':phone', $phone);
$stmt->execute();
echo'{"notice": {"text": "Customer Added"}';
}
catch(PDOException $e){
echo '{"error": {"text": '.$e->getMessage().'}';
}
});
这不是答案,而是的一种手段。我包括IF及其否则语句,因此您可以知道问题到底在哪里。
我不知道这是否会非常影响它,但我也建议您移动 $ first_name,$ last_name,$ phone 和 $ sql 捕获。
我还建议将您的按钮作为提交按钮,因此<a id="click" href="" class="ui-btn" data-theme="b" data-inline="true"> Submit</a>
转向`submit
<?php
use PsrHttpMessageServerRequestInterface as Request;
use PsrHttpMessageResponseInterface as Response;
$app = new SlimApp;
$app->post('/api/customer/add', function(Request $request, Response $response) {
$first_name = $request->getParam('first_name');
$last_name = $request->getParam('last_name');
$phone = $request->getParam('phone');
$sql = "INSERT INTO users (first_name, last_name,phone) VALUES (:first_name,:last_name,:phone)";
try {
$db = new db();
if ($db = $db->connect()) {
$stmt = $db->prepare($sql);
$stmt->bindParam(':first_name', $first_name);
$stmt->bindParam(':last_name', $last_name);
$stmt->bindParam(':phone', $phone);
if ($stmt->execute()) {
echo'{"notice": {"text": "Customer Added"}';
} else {
echo "There was a problem adding customer.";
}
} else {
echo "There was a problem connecting to the databse.";
}
}
catch(PDOException $e){
echo '{"error": {"text": '.$e->getMessage().'}';
}
});
,所以你们是对的!这基本上是我的提交按钮没有提交任何内容。
加上我的文件路径在指向API方面是错误的。(Slimtest是我的项目名称(
我完全更改了HTML形式。
我觉得Slim在这里没有太多覆盖,所以病了我改变了什么。
在这里:
html代码:
<form method="POST" action="http://slimtest/api/customer/add">
<table>
<tr>
<td>Username:</td>
<td><input type="text" name="first_name" class="textInput" required></td>
</tr>
<tr>
<td>First Name:</td>
<td><input type="text" name="last_name" class="textInput" required></td>
</tr>
<tr>
<td>Surname:</td>
<td><input type="text" name="phone" class="textInput" required></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="register_btn" value="Register" class="buttonRegister"></td>
</tr>
</table>
</form>
php代码没有更改,但是是的,尝试发现任何错误会很好。
再次感谢伙计:(