我对JSON很新,目前我一直在尝试插入数据库,目前我得到正确的消息,所有输入正确的验证通过(验证仍然需要我知道的一些工作)但是我无法得到要插入数据库,任何建议都会很棒。
HTML 权力的游戏社交
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script>
$(document).ready(function() {
$("form").on("submit", function(event) {
event.preventDefault();
$("span.error").empty()
$("span.success").empty()
$.getJSON('registerForm.php', $(this).serialize(), function(data) {
if (!data.errors) {
$(".success").append(data.message) // deal with a no-error response ( all is good)
}else{
$.each(data.errors,function(i,datum){
$("[name='"+datum.name+"']").next().html(datum.error)
})
}
});
});
});
</script>
</head>
<body>
<span class="success"></span>
<form action="" method="POST">
<div class="formControl">
<input type="input" name="username" placeholder="Username" value="">
<span class="error"> </span>
</div>
<div class="formControl">
<input type="text" name="email" placeholder="E-mail" value="">
<span class="error"></span>
</div>
<div class="formControl">
<input type="password" name="password" placeholder="Password">
<span class="error"> </span>
</div>
<div class="formControl">
<input type="password" name="repeatPassword" placeholder="Confirm Password">
<span class="error"> </span>
</div>
<div class="formControl">
<input type="hidden" name="code" value="<?php echo substr(str_shuffle("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 1).substr(md5(time()),1); ?>">
<span class="error"> </span>
</div>
<input type="submit" value="Submit">
</form>
</body>
</html>
PHP
<?php
require_once'connection.php';
header('Content-Type: application/json');
$errors = [];
$username = trim($_GET['username']);
$email = trim($_GET['email']);
$password = trim($_GET['password']);
$repeatPassword = trim($_GET['repeatPassword']);
$errors = [];
if(filter_var($username, FILTER_VALIDATE_REGEXP,["options"=> [ "regexp" => "/.{6,25}/"]]) === FALSE){
$errors[]= ["name"=>"username","error"=>"invalid Id (6 to 25 characters)"];
}
if(filter_var($email,FILTER_VALIDATE_EMAIL) === FALSE) {
$errors[]= ["name"=>"email","error"=>"invalid Email"];
}
if(filter_var($password, FILTER_VALIDATE_REGEXP,["options"=> [ "regexp" => "/.{6,25}/"]]) === FALSE){
$errors[]= ["name"=>"password","error"=>"invalid password (6 to 25 characters)"];
}
if($password !== $repeatPassword){
$errors[]= ["name"=>"repeatPassword","error"=>"passwords don't match"];
}
if (count($errors) === 0) {
$salt= uniqid(mt_rand(), true);
$options=['salt'=>$salt, 'cost'=>12];
// everything is OK, the browser should send us to the next page
$sql = "INSERT INTO username (username,password, eMail ,joinedDate, active, activecode) VALUES (:username, :password, :email ,NOW(), 0, :code)";
$query = $db->prepare($sql);
$query->execute();
$json[] = array("username" => $username, "password" => $password, "email" => $email, "code" => $code);
// $json = json_encode($json);
// $json = file_get_contents('php://input');
$obj = json_decode($json,true);
echo json_encode(["message"=>"Please view your email account to activate your account"]);
}else{
echo json_encode(["errors"=>$errors]);
}
?>
删除前的第一次尝试。
当我尝试使用echo json_encode(["message"=>"Please view your email account to activate your account"]);
时,这不起作用我不知道为什么
在if (count($errors) === 0) {}
内,它无效。
header('Content-Type: application/json');
$errors = [];
$username = trim($_GET['username']);
$email = trim($_GET['email']);
$password = trim($_GET['password']);
$repeatPassword = trim($_GET['repeatPassword']);
$code = $_GET['code'];
$query = $db->prepare("SELECT username.username FROM username WHERE username.username = :username LIMIT 1");
$query->bindValue(':username', $username, PDO::PARAM_STR);
$query->execute();
if ( $query->rowCount() > 0 ) {
$response=1;
$errors[]= ["name"=>"username","error"=>"Username taken"];
}
if(filter_var($username, FILTER_VALIDATE_REGEXP,["options"=> [ "regexp" => "/.{3,25}/"]]) === FALSE){
$errors[]= ["name"=>"username","error"=>"invalid Id (3 to 25 characters)"];
}
if(preg_match('/[^a-z_\-0-9]/i', $username))
{
$errors[]= ["name"=>"username","error"=>"invalid Id (Usernames may not contain symbols)"];
}
if(filter_var($email,FILTER_VALIDATE_EMAIL) === FALSE) {
$errors[]= ["name"=>"email","error"=>"invalid Email"];
}
$emailQ = $db->prepare("SELECT username.eMail FROM username WHERE username.eMail = :email LIMIT 1");
$emailQ->bindValue(':email', $email, PDO::PARAM_STR);
$emailQ->execute();
if ( $query->rowCount() > 0 ) {
$response=1;
$errors[]= ["name"=>"email","error"=>"Email registered"];
}
if(filter_var($password, FILTER_VALIDATE_REGEXP,["options"=> [ "regexp" => "/.{6,25}/"]]) === FALSE){
$errors[]= ["name"=>"password","error"=>"invalid password (6 to 25 characters)"];
}
if(!preg_match("/(?=[a-z]*[0-9])(?=[0-9]*[a-z])([a-z0-9-]+)/i",$password)) {
$errors[]= ["name"=>"password","error"=>"Password must contain numbers and letters"];
}
if($password !== $repeatPassword){
$errors[]= ["name"=>"repeatPassword","error"=>"passwords don't match"];
}
$salt= uniqid(mt_rand(), true);
$options=['salt'=>$salt, 'cost'=>12];
if (count($errors) === 0) {
// everything is OK, the browser should send us to the next page
$sql = "INSERT INTO username (username,password, eMail ,joinedDate, active, activecode) VALUES (:username, :password, :email ,NOW(), 0, :code)";
$query = $db->prepare($sql);
$query->execute(array(
':username'=> $username,
':password'=> $cryptpwd=crypt($password,'$2y$12$'.$salt.'$'),
':email'=> $email,
':code'=> $code
));
echo $message = '
http://gotsocial.co.uk/active.php?activecode='.$code.'.
';
$to = $email;
$subject = 'Game of Thrones Social';
$from = "register@gotsocial.co.uk";
$result = mail($to, $subject, $message, "From: $from");
echo json_encode(["message"=>"Please view your email account to activate your account"]);
}
echo json_encode($errors);
这部分是我的问题,没有它,我的成功消息会显示出来,我的成功消息不会显示。
echo $message = '
http://gotsocial.co.uk/active.php?activecode='.$code.'.
';
$to = $email;
$subject = 'Game of Thrones Social';
$from = "register@gotsocial.co.uk";
$result = mail($to, $subject, $message, "From: $from");
答案 0 :(得分:0)
此处不涉及JSON。问题是你正在使用SQL绑定参数(这很好),但在执行它时不会传入任何值。
$sql = "INSERT INTO username (username,password, eMail ,joinedDate, active, activecode) VALUES (:username, :password, :email ,NOW(), 0, :code)";
$query = $db->prepare($sql);
$query->execute();
最后$query->execute();
是问题所在。执行需要每个:username
,:password
等的键/值对...否则它将插入NULL
。为此,请使用normal PHP array。
$query->execute(array(
':username' => $username,
':password' => $password,
':email' => $email,
':code' => $code
));
如果$query->execute()
没有错误,则表示您的数据库架构在每个字段中都允许NULL值。您可能不希望这样,因为您发现它隐藏了错误。您应该更改架构并将NOT NULL
添加到每个字段并删除所有默认值。