嘿我有一个需要注册的Android应用程序我有PHP代码,当我在本地服务器上使用它时工作正常
它在远程服务器上不起作用。
/*
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
}
?>
插入部分在远程服务器上不起作用,但它可以很好地在本地服务器上运行。请任何帮助将不胜感激我认为问题是在插入查询中所以任何帮助将不胜感激,因为我是新来的PHP
答案 0 :(得分:1)
您的问题是您很可能正在尝试运行这样的查询:
$query = "INSERT INTO users(username,password,email) VALUES ('$user','$password','$email') ";
实际上你应该绑定那些用户输入参数:
$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);
}
现在发生任何错误?你的查询运行正常吗?请提供更多信息,以便我们更好地为您提供帮助。
<强>旁注强>
同时启用错误报告:
ini_set('display_errors', 1);
error_reporting(E_ALL);
帮助调试此问题。