我正在尝试编写脚本来创建一个名为ID的文件夹,但是,尽管在mysql中成功注册,但我无法理解为什么无法创建目录。下面的代码...希望对您有所帮助=)
<?php
require("config.php");
if (!empty($_POST)) {
$response = array("error" => FALSE);
$query = " SELECT 1 FROM users WHERE email = :email";
$query_params = array(':email' => $_POST['email'] );
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
$response["error"] = TRUE;
$response["message"] = "Database Error1. Please Try Again!";
die(json_encode($response));
}
$row = $stmt->fetch();
if ($row) {
$response["error"] = TRUE;
$response["message"] = "I'm sorry, this email is already in use";
die(json_encode($response));
}
else {
$query = "INSERT INTO users ( unique_id, name, email, encrypted_password, otp, created_at ) VALUES ( :uuid, :name, :email, :encrypted_password, :otp, NOW() )";
$otp = rand(100000, 999999);
$verified = 0;
$query_params = array(
':uuid' => uniqid('', true),
':name' => $_POST['name'],
':email' => $_POST['email'],
':encrypted_password' => password_hash($_POST['password'], PASSWORD_DEFAULT),
':otp' => $otp );
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
$response["error"] = TRUE;
$response["message"] = "Database Error2. Please Try Again!";
die(json_encode($response));
}
if ($stmt->execute($query_params)){
$query_params = array(':email' => $_POST['email']);
$query = "SELECT unique_id FROM users WHERE email = :email";
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
$unique_id = $result->fetchColumn();
$path = "/App/users/$unique_id";
if(!is_dir($path) ){
mkdir($path, 0777, true);
}
}
}
}
您需要更改文件夹创建块,因为当前它尝试第二次执行查询并失败:
<?php
require("config.php");
if (!empty($_POST)) {
$response = array("error" => FALSE);
$query = " SELECT 1 FROM users WHERE email = :email";
$query_params = array(':email' => $_POST['email'] );
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
$response["error"] = TRUE;
$response["message"] = "Database Error1. Please Try Again!";
die(json_encode($response));
}
$row = $stmt->fetch();
if ($row) {
$response["error"] = TRUE;
$response["message"] = "I'm sorry, this email is already in use";
die(json_encode($response));
}
else {
$query = "INSERT INTO users ( unique_id, name, email, encrypted_password, otp, created_at ) VALUES ( :uuid, :name, :email, :encrypted_password, :otp, NOW() )";
$otp = rand(100000, 999999);
$verified = 0;
$uuid = uniqid('', true);
$query_params = array(
':uuid' => $uuid,
':name' => $_POST['name'],
':email' => $_POST['email'],
':encrypted_password' => password_hash($_POST['password'], PASSWORD_DEFAULT),
':otp' => $otp );
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
$response["error"] = TRUE;
$response["message"] = "Database Error2. Please Try Again!";
die(json_encode($response));
}
if ($result){
$path = "/App/users/$uuid";
if(!is_dir($path) ){
mkdir($path, 0777, true);
}
}
}
}