简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM