-1

I am writing code for login and register application. Below is the android code & php script for register. The values are not storing in mysql database table (my_table).

When i open "http://fooddoof.freevar.com/Register.php" in brower i am getting the "Parse error: syntax error, unexpected '$username' (T_VARIABLE) in /home/vhosts/fooddoof.freevar.com/Register.php on line 8" error.

Android Java Script:

public class StoreUserDataAsyncTask extends AsyncTask<Void,Void,Void> {
    User user;
    GetUserCallback userCallback;

    public StoreUserDataAsyncTask(User user,GetUserCallback userCallback){
        this.user=user;
        this.userCallback=userCallback;
    }

    @Override
    protected Void doInBackground(Void... params) {
        ContentValues contentValues=new ContentValues();
        contentValues.put("username",user.username);
        contentValues.put("password",user.password);
        contentValues.put("email",user.email);

        try {
            URL url = new URL(SERVER_ADDRESS + "Register.php");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(CONNECTION_TIMEOUT);
            conn.setReadTimeout(CONNECTION_TIMEOUT);
            conn.setRequestMethod("POST");
            conn.setDoInput(true);
            conn.setDoOutput(true);

            OutputStream out = conn.getOutputStream();;
            BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(out,"UTF-8"));
            writer.write(String.valueOf(params));
            writer.flush();
            writer.close();
            out.close();

        } catch (java.io.IOException e) {
            e.printStackTrace();
        }

        return null;
    }

PHP script:

<?php
    $con=mysqli_connect("localhost","862322","varna123","862322")
    $username = $_POST["username"];
    $password = $_POST["password"];
    $email= $_POST["email"];
    $statement = mysqli_prepare($con, "INSERT INTO my_table (username, password, email) VALUES (?, ?, ?)");
    mysqli_stmt_bind_param($statement, "sss",$username, $password, $email);
    mysqli_stmt_execute($statement);
    mysqli_stmt_close($statement);
    mysqli_close($con);  ?>

Please help if there is any wrong with this code.

shianra
  • 11
  • 1
  • 6

2 Answers2

0

Add semicolon(;) at the end of the line (Every PHP statements end with semicolon).

$con=mysqli_connect("localhost","862322","varna123","862322");

Mohan S Gopal
  • 233
  • 3
  • 12
  • But when i run this app in Nexus 4 real device , when i try to enter the values in Register form i am getting the below error in Logcat: E/AmanattoDataUpdaterHelper﹕ Failed to get contextual suggestions. .Also, I am keep on getting the below error: Failed to remove cdma broadcast subscription for MID range 4100 to 4100 from client com.android.cellbroadcastreceiver – shianra Jul 10 '15 at 10:22
  • I am resolved all the syntax issues. But still the data is not stored in mysql. Please help me on this. – shianra Jul 16 '15 at 06:59
0
  1. Please correct mysqli connection by putting a semi-colon at the end.

  2. If there are not checks to validate post inputs then you will surely get errors on browser.

Use PHP function isset —

Determine if a variable is set and is not NULL. bool isset ( mixed $var [, mixed $... ] )

See here

Here is the updated code:

<?php
    $con=mysqli_connect("localhost","862322","varna123","862322");
    if(isset($_POST)) {
    $username = $_POST["username"];
    $password = $_POST["password"];
    $email= $_POST["email"];
    $statement = mysqli_prepare($con, "INSERT INTO my_table (username, password, email) VALUES (?, ?, ?)");
    mysqli_stmt_bind_param($statement, "sss",$username, $password, $email);
    mysqli_stmt_execute($statement);
    mysqli_stmt_close($statement);
    mysqli_close($con);
    } 
?>

You could add more isset checks to check the username etc.

Satish Saini
  • 2,880
  • 3
  • 24
  • 38
  • Still the values are not storing in mysql DB, register form is in progress for long time. Please help if there is any wrong with the code. – shianra Jul 15 '15 at 19:19
  • It seems like its not making connection to 862322 port. Please check your connection settings again. – Satish Saini Jul 17 '15 at 10:15