gpt4 book ai didi

php - Slim POST 方法不会发送到 MySQL

转载 作者:行者123 更新时间:2023-11-29 19:09:50 25 4
gpt4 key购买 nike

我遇到了一个很奇怪的问题。我没有收到任何错误、控制台消息,因此一切都以 200 状态返回。

使用 Slim 框架将数据 POST 到我的数据库中。当我通过 urli 访问已经存在的数据时,一切都在那里,所以它工作正常,但它只是没有将其发布到 MySQL 中。然而,它确实将输入的数据放入 ULR 中。有人可以指出更改或我要去哪里吗?

PS。不要因为密码没有被散列而 panic ,这不是一个实时项目。

Index.php 片段:

>           
> <form class="form-horizontal" name="register_form" data-toggle="validator" role="form">
>
> <div class="form-group">
> <div class="input-group margin-bottom-sm">
> <span class="input-group-addon"><i class="glyphicon glyphicon-user" aria-hidden="true"></i></span>
> <input class="form-control" type="text" id="fname" name="fname" placeholder="First Name">
> </div>
> </div></br>
> <div class="form-group">
> <div class="input-group margin-bottom-sm">
> <span class="input-group-addon"><i class="glyphicon glyphicon-user" aria-hidden="true"></i></span>
> <input class="form-control" type="text" id="lname" name="lname" placeholder="Last Name">
> </div>
> </div></br>
> <div class="form-group">
> <div class="input-group margin-bottom-sm">
> <span class="input-group-addon"><i class="glyphicon glyphicon-calendar" aria-hidden="true"></i></span>
> <input class="form-control" type="date" id="dob" name="dob" placeholder="Date of Birth DD/MM/YYYY">
> </div>
> </div></br>
>
> <div class="form-group">
> <div class="input-group margin-bottom-sm">
> <span class="input-group-addon"><i class="glyphicon glyphicon-info-sign" aria-hidden="true"></i></span>
> <input class="form-control" type="text" id="school" name="school" placeholder="Enter School Name">
> </div>
> </div></br>
>
> <div class="form-group">
> <div class="input-group margin-bottom-sm">
> <span class="input-group-addon"><i class="glyphicon glyphicon-pencil" aria-hidden="true"></i></span>
> <input class="form-control" type="text" id="username" name="username" placeholder="Choose Username">
> </div>
> </div></br>
>
> <div class="form-group">
> <div class="input-group margin-bottom-sm">
> <span class="input-group-addon"><i class="glyphicon glyphicon-lock" aria-hidden="true"></i></span>
> <input class="form-control" type="password" id="pass" name="pass" placeholder="Choose Password">
> </div>
> </div></br>
>
> <div class="form-group">
> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
>
> <button id="register" class="btn btn-primary">Register</button>
> </div></br>
>
> </form>

PHP:

<?php

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require 'Slim\Slim.php';
\Slim\Slim::registerAutoloader();
use Slim\Slim;
$app= new Slim();
$app->get('/users','getUsers');
$app->post('/users','addUser');
$app->run();

// Adding a user = for registration process
function addUser(){
$request = Slim::getInstance()->request();
$user = json_decode($request->getBody());
$sql = "INSERT INTO user(fname, lname, dob, school, username, pass) VALUES (:fname, :lname, :dob, :school, :username, :pass)";
try {
$db = getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam("fname",$user->fname);
$stmt->bindParam("lname",$user->lname);
$stmt->bindParam("dob",$user->dob);
$stmt->bindParam("school",$user->school);
$stmt->bindParam("username",$user->username);
$stmt->bindParam("pass",$user->pass);
$stmt->execute();
$user->UserID=$db->lastInsertID();
$db = null;
responseJson(json_encode($user),201);
}catch(PDOException $e){
responseJson('{"error":{"text":'.$e->getMessage().'}}', 500);
}
}

JS:

$(document).ready(function(){
$("#register").click(function(){
var users=new users($("#fname").val(),
$("#lname").val(),
$("#dob").val(),
$("#school").val(),
$("#username").val(),
$("#pass").val());
$.ajax({
type:'POST',
dataType:"json",
url:"api.php/users",
data:JSON.stringify(users),
success: showResponse,
error: showError
});
});
});

function users(fname, lname, dob, school, username, pass){
this.fname=fname;
this.lname=lname;
this.dob=dob;
this.school=school;
this.username=username;
this.pass=pass;
}

function showResponse(responseData){
console.log(responseData);
}
function showError(){
alert("Sorry Kasia, but something went wrong. Fix it!")
}

最佳答案

users 是一个函数而不是一个对象,我认为你最好放弃该函数并在注册单击中内联执行它..

$(document).ready(function(){
$("#register").click(function(){

var users={};
users.fname=$("#fname").val();
users.lname=$("#lname").val();
users.dob=$("#dob").val();
users.school=$("#school").val();
users.username=$("#username").val();
users.pass=$("#pass").val();

$.ajax({
type:'POST',
dataType:"json",
url:"api.php/users",
data:JSON.stringify(users),
success: showResponse,
error: showError
});
});
});

关于php - Slim POST 方法不会发送到 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43174324/

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