[英]php script works well on the localserver but does not work on the remote server
Hey I have an android app that needs registeration i have php code that works fine when i am using it on the local server however 嘿,我有一个需要注册的android应用,我有php代码,当我在本地服务器上使用它时,它工作正常
it does not work on the remote server. 它在远程服务器上不起作用。
/*
Our "config.inc.php" file connects to database every time we include or require
it within a php script. Since we want this script to add a new user to our db,
we will be talking with our database, and therefore,
let's require the connection to happen:
*/
require("config.inc.php");
//if posted data is not empty
if (!empty($_POST)) {
if (empty($_POST['username']) || empty($_POST['email'])) {
$response["success"] = 0;
$response["message"] = "Please Enter Both a Username and Password.";
die(json_encode($response));
}
$query = " SELECT 1 FROM users WHERE username = :user";
//now lets update what :user should be
$query_params = array(
':user' => $_POST['username']
);
//Now let's make run the query:
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
$response["success"] = 0;
$response["message"] = "Database Error1. Please Try Again!";
die(json_encode($response));
}
$row = $stmt->fetch();
if ($row) {
$response["success"] = 0;
$response["message"] = "I'm sorry, this username is already in use";
die(json_encode($response));
}
$user = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$query = "INSERT INTO users(username,password,email) VALUES ('$user','$password','$email') ";
try {
$stmt = $db->prepare($query);
$result = $stmt->execute();
}
catch (PDOException $ex) {
// For testing, you could use a die and message.
//die("Failed to run query: " . $ex->getMessage());
//or just use this use this one:
$response["success"] = 0;
$response["message"] = "Database Error2. Please Try Again!";
die(json_encode($response));
}
$response["success"] = 1;
$response["message"] = "Username Successfully Added!";
echo json_encode($response);
//for a php webservice you could do a simple redirect and die.
//header("Location: login.php");
//die("Redirecting to login.php");
} else {
?>
<h1>Register</h1>
<form action="register.php" method="post">
Username:<br />
<input type="text" name="username" value="" />
<br /><br />
Password:<br />
<input type="text" name="email" value="" />
<br /><br />
<input type="submit" value="Register New User" />
</form>
<?php
}
?>
The insert part does not work on the remote server however it does work on the local server perfectly well. 插入部分在远程服务器上不起作用,但是在本地服务器上则很好。 Please any help will be appreciated i think that the problem is in the insert query so any help will be appreciated as i am new to php
请任何帮助将不胜感激,我认为问题出在插入查询中,所以任何帮助将不胜感激,因为我是php新手
Your issue is that you most likely are trying to run the query like this: 您的问题是您很可能试图像这样运行查询:
$query = "INSERT INTO users(username,password,email) VALUES ('$user','$password','$email') ";
When in fact you should be binding those user input params like this: 实际上,您应该像这样绑定那些用户输入参数:
$query = "INSERT INTO users(username,password,email) VALUES (:user, :password, :email) ";
//now lets update what :user should be
$query_params = array(
':user' => $_POST['username'],
':password' => $_POST['password'],
':email' => $_POST['email'],
);
//Now let's make run the query:
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
print_r($ex);
}
Now do any errors occur? 现在是否发生任何错误? does your query run properly?
您的查询是否正常运行? Please provide more information to allow us to better help you.
请提供更多信息,以便我们更好地为您提供帮助。
Sidenote 边注
Also turn on error reporting: 同时打开错误报告:
ini_set('display_errors', 1);
error_reporting(E_ALL);
To help debug this issue. 为了帮助调试此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.