Ajax不起作用-已发布修复PHP
Html: php: 标题'Content-Type:application/json'Ajax不起作用-已发布修复PHP,php,ajax,pdo,Php,Ajax,Pdo,Html: php: 标题'Content-Type:application/json' <?php error_reporting(E_ALL); ini_set('display_errors', 1); $alert1 = null; $uname = filter_input(INPUT_POST, isset($_POST['uname'])); if ($uname === $_POST['uname'] ? $_POST['uname'] : ''){ try { $
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$alert1 = null;
$uname = filter_input(INPUT_POST, isset($_POST['uname']));
if ($uname === $_POST['uname'] ? $_POST['uname'] : ''){
try {
$conn = new PDO('mysql:host=127.0.0.1;dbname=users', 'root', 'redcardinal');
$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = 'SELECT * FROM `usernames` WHERE `uname` = :uname';
$stmt = $conn->prepare($query);
$stmt->bindParam(":uname",$uname, PDO::PARAM_STR, strlen($uname));
$stmt->execute();
$hello = array(array());
if ($hello = $stmt->fetch(PDO::FETCH_ASSOC) ? $hello != null : ''){
$alert1 = "This name is taken.";
echo json_encode($alert1);
$conn = null;
$query = null;
}
elseif ($hello === null) {
$alert2 = "This name is not taken.";
echo json_encode($alert2);
}
}
catch (PDOException $e) { echo json_encode($e);
$conn = null;
$query = null;
}
EDIT: WILL PUT ADDITIONAL elseif HERE.
}
?>
<?php
标题'Content-Type:application/json'
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$alert1 = null;
$uname = filter_input(INPUT_POST, isset($_POST['uname']));
if ($uname === $_POST['uname'] ? $_POST['uname'] : ''){
try {
$conn = new PDO('mysql:host=127.0.0.1;dbname=users', 'root', 'redcardinal');
$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = 'SELECT * FROM `usernames` WHERE `uname` = :uname';
$stmt = $conn->prepare($query);
$stmt->bindParam(":uname",$uname, PDO::PARAM_STR, strlen($uname));
$stmt->execute();
$hello = array(array());
if ($hello = $stmt->fetch(PDO::FETCH_ASSOC) ? $hello != null : ''){
$alert1 = "This name is taken.";
echo json_encode($alert1);
$conn = null;
$query = null;
}
elseif ($hello === null) {
$alert2 = "This name is not taken.";
echo json_encode($alert2);
}
}
catch (PDOException $e) { echo json_encode($e);
$conn = null;
$query = null;
}
EDIT: WILL PUT ADDITIONAL elseif HERE.
}
?>
<?php
我基本上按照建议更改了PHP,我能想到的唯一其他问题是html或ajax。谢谢你的if$hello=null{
您需要将其更改为if$hello==null{
关于第二个问题,有两件事是错误的
将if$uname!=null&&$alert1=null{更改为if$uname!=null&&$alert1==null{您需要使三个===代替1=
再次将else$alert1!=null{更改为elseif$alert1!=null{您需要执行另一个if语句,因此需要执行esleif而不是else
错误报告全部;
ini设置“显示错误”,1;
包括“check_name.php”
$uname=filter_inputINPUT_POST,isset$_POST['uname']
如果$uname!=null&&$alert1===null{
试一试{
$conn=new-PDO'mysql:host=127.0.0.1;dbname=users','root','password';
$conn->setAttributePDO::ATTR\u EMULATE\u PREPARES,false;
$conn->setAttributePDO::ATTR_ERRMODE,PDO::ERRMODE_异常
$uname=$_POST['uname']
$upassword=password\u hash$\u POST['upassword'],密码\u默认值
$stmt='插入用户名uname,upassword值:uname,:upassword';
$query=$conn->prepare$stmt;
$query->bindParam:uname,$uname,PDO::PARAM_STR,strlen$uname;
$query->bindParam:upassword,$upassword,PDO::PARAM_STR,strlen$upassword
$query->execute;
}
捕获$e{
打印错误!:.$e->getMessage;
死亡
}
}
elseif$alert1!=null{
echo json_Encode此名称已使用,请选择其他名称。;
}
$query=null;
$conn=null
?>
如果$uname=isset$\u POST['uname']?isset$\u POST['uname']:{如果$uname=isset$\u POST['uname']?$\u POST['uname'],可能应该是:{为了给你$unameA中的$unameA中的$unameA的值,很多ifs都是赋值=而不是比较==。如果这不是故意的,我想你会因此产生一些错误。from:赋值表达式的值就是赋值。也就是说,$a=3的值是3。这允许你做一些比较cky的事情。有时我会意外地感谢你,我已经按照你的建议更改了代码,但现在我相信我的AJAX调用有问题。我也会发布。谢谢
error_reporting(E_ALL);
ini_set('display_errors', 1);
include 'check_name.php';
$uname = filter_input(INPUT_POST, isset($_POST['uname']));
if ($uname != null && $alert1 === null) {
try {
$conn = new PDO ('mysql:host=127.0.0.1;dbname=users', 'root', 'redcardinal');
$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$uname = $_POST['uname'];
$upassword = password_hash(filter_input(INPUT_POST, isset($_POST['upassword'])), PASSWORD_DEFAULT);
$stmt = 'INSERT INTO `usernames` (`uname`, `upassword`) VALUES (:uname,:upassword)';
$query = $conn->prepare($stmt);
$query->bindParam(":uname",$uname, PDO::PARAM_STR, strlen($uname));
$query->bindParam(":upassword",$upassword, PDO::PARAM_STR, strlen($upassword));
$query->execute();
echo json_encode("Username/Password successfully created.");
}
catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
}
elseif ($alert1 != null) {
echo json_encode("Username/Password taken.");
}
$query = null;
$conn =null;
<?php
header('Content-Type: application/json');
error_reporting(E_ALL);
ini_set('display_errors', 1);
$alert1 = null;
if ($uname = isset($_POST['uname']) ? isset($_POST['uname']) : ''){
try {
$conn = new PDO('mysql:host=127.0.0.1;dbname=users', 'root', 'password');
$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = 'SELECT * FROM `usernames` WHERE `uname` = :uname';
$stmt = $conn->prepare($query);
$stmt->bindParam(":uname",$uname, PDO::PARAM_STR, strlen($uname));
$stmt->execute();
$hello = array(array());
if ($hello = $stmt->fetch(PDO::FETCH_ASSOC) && $hello != null){
$alert1 = "This name is taken.";
echo json_encode($alert1);
$conn = null;
$query = null;
}
if ($hello === null) {
$alert1 = "This name is not taken.";
echo json_encode($alert1);
}
}
catch (PDOException $e) { echo json_encode($e);
$conn = null;
$query = null;
$alert1 = null;
}
}