簡體   English   中英

php腳本在localserver上運行良好,但在遠程服務器上不運行

[英]php script works well on the localserver but does not work on the remote server

嘿,我有一個需要注冊的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新手

您的問題是您很可能試圖像這樣運行查詢:

$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);

為了幫助調試此問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM