简体   繁体   English

php脚本在localserver上运行良好,但在远程服务器上不运行

[英]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.

相关问题 带有cURL的PHP​​脚本可在一台服务器上运行,而不能在另一台服务器上运行 - PHP Script with cURL works on one server…does not work on other server php头文件在localserver(垃圾服务器)上运行良好,但在联机服务器(000webhost)上运行不正常 - php header file working well on localserver (wamp server) but not working well on online server (000webhost) curl是否在远程服务器上执行PHP脚本? - Does curl execute the PHP script on the remote server? 使用PHP将表内容从localserver传输到远程服务器DB的代码 - Code for transfering the table content from localserver to remote server DB using PHP PHP上传脚本可在本地工作,但不能在远程服务器上工作 - PHP upload script works locally, however not on remote server php脚本不起作用。 但是如果我将其包含在其他文件中,则可以正常工作吗? - php script does not work. but if I include it in other file, it works? 多个查询在phpmyadmin中插入但在php脚本中不起作用 - mutiple insert Query Works in phpmyadmin but does not work in php script MySQL在PHPmyadmin上的终端中工作,但在php脚本中不工作? - MySQL works in terminal on PHPmyadmin but does not work in php script? MySQL查询GROUP在控制台中工作,在PHP脚本中不起作用 - MySQL query GROUP works in console, does not work in PHP script 从PHP调用的Shell脚本不起作用,但可以在命令行中使用 - Shell script called from PHP does not work, but it works in command line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM