Skip to main content

$con = mysqli_connect("localhost", "root", "", "matgic");

$con = mysqli_connect("localhost", "root", "", "matgic");

    $id_user = $_POST["id_user"];
    $username = $_POST["username"];
    $score = $_POST["score"];
    
    
    $statement = mysqli_prepare($con, "UPDATE user SET score=? WHERE id_user = ? AND username =?");


    mysqli_stmt_bind_param($statement, "isi",$id_user, $username,$score);
    mysqli_stmt_execute($statement);

    mysqli_stmt_store_result($statement);
    

    $response = array();
    $response["success"] = false; 
    
    
    while(mysqli_stmt_fetch($statement))
    {
        $response["success"] = true; 
        $response["user_id"] = $user_id; 
        $response["name"] = $name;
        $response["username"] = $username;
        $response["password"] = $password;
        $response["date"] = $date;
        $response["email"] = $email;
        $response["score"] = $score;
        }
    
    echo json_encode($response);

public class ScoreRequest extends StringRequest {

public class ScoreRequest extends StringRequest
{

    private static final String Score_Request_URL="http://192.168.1.221/Score.php";//declaramos una constante
    private Map<String, String> params;

    //constructor de la clase
    public ScoreRequest (int id_user, String username, int score, Response.Listener<String> listener)
    {
        super(Request.Method.POST,Score_Request_URL,listener, null);
        params= new HashMap<>();
        params.put("id_user", id_user+"");
        params.put("username", username);
        params.put("score", score+"");


    }//fin de constructor



    @Override
    public Map <String, String> getParams()
    {
        return params;
    }//fin de método Map


}//fin de clase

}//fin de clase

btnno.setOnClickListener(new View.OnClickListener() {

btnno.setOnClickListener(new View.OnClickListener()
    {

        @Override
        public void onClick(View view)
        {
            int scoreaux;
            scoreaux=Integer.parseInt(score);
            scoreaux+=1;
            final String usernameaux=username;
            final int useraux=Integer.parseInt(userid);


            Response.Listener<String> responseListener= new Response.Listener<String>()
            {
                @Override
                public void onResponse(String response)
                {
                    try
                    {
                        JSONObject jsonResponse=new JSONObject(response);
                        boolean success= jsonResponse.getBoolean("success");
                        if(success)
                        {



                           int user=jsonResponse.getInt("id_user");
                           String nameuser=jsonResponse.getString("username");
                           int scoreb=jsonResponse.getInt("score");


                            Intent intent=new Intent (Digits.this, Digits2.class);

                           intent.putExtra("id_user", user);
                            intent.putExtra("username", nameuser);
                           intent.putExtra("score", scoreb);




                            Digits.this.startActivity(intent);


                        }
                        else
                        {
                            AlertDialog.Builder builder=new AlertDialog.Builder(Digits.this);
                            builder.setMessage("User or Id incorrect")
                                    .setNegativeButton("Retry", null)
                                    .create().show();
                        }
                    }

                    catch (JSONException e)
                    {
                        e.printStackTrace();
                    }
                }//fin de método onResponse
            };//fin de Response.Listener<String>

            ScoreRequest scoreRequest= new ScoreRequest(useraux,usernameaux,scoreaux, responseListener);
            RequestQueue queue= Volley.newRequestQueue(Digits.this);
            queue.add(scoreRequest);

        }//fin de onclick
    });//fin de setOnClickListener

    //fin de método btnno

public class Digits2 extends AppCompatActivity { String username; int userid, score;

public class Digits2 extends AppCompatActivity
{
    String username;
    int userid, score;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_digits2);

        Intent intent =getIntent();
        score=intent.getIntExtra("score",-1);
        username=intent.getStringExtra("username");
        userid=intent.getIntExtra("id_user",-1);
    }
}

}

Notice: Undefined index: id_user in C:\xampp\htdocs\Score.php on line 4

Notice: Undefined index: username in C:\xampp\htdocs\Score.php on line 5

Notice: Undefined index: score in C:\xampp\htdocs\Score.php on line 6 {"success":false}

Notice: Undefined index: id_user in C:\xampp\htdocs\Score.php on line 4

Notice: Undefined index: username in C:\xampp\htdocs\Score.php on line 5

Notice: Undefined index: score in C:\xampp\htdocs\Score.php on line 6 {"success":false}

$con = mysqli_connect("localhost", "root", "", "matgic");

$id_user = $_POST["id_user"];
$username = $_POST["username"];
$score = $_POST["score"];


$statement = mysqli_prepare($con, "UPDATE user SET score=? WHERE id_user = ? AND username =?");


mysqli_stmt_bind_param($statement, "isi",$id_user, $username,$score);
mysqli_stmt_execute($statement);

mysqli_stmt_store_result($statement);


$response = array();
$response["success"] = false; 


while(mysqli_stmt_fetch($statement))
{
    $response["success"] = true; 
    $response["user_id"] = $user_id; 
    $response["name"] = $name;
    $response["username"] = $username;
    $response["password"] = $password;
    $response["date"] = $date;
    $response["email"] = $email;
    $response["score"] = $score;
    }

echo json_encode($response);

public class ScoreRequest extends StringRequest {

private static final String Score_Request_URL="http://192.168.1.221/Score.php";//declaramos una constante
private Map<String, String> params;

//constructor de la clase
public ScoreRequest (int id_user, String username, int score, Response.Listener<String> listener)
{
    super(Request.Method.POST,Score_Request_URL,listener, null);
    params= new HashMap<>();
    params.put("id_user", id_user+"");
    params.put("username", username);
    params.put("score", score+"");


}//fin de constructor



@Override
public Map <String, String> getParams()
{
    return params;
}//fin de método Map

}//fin de clase

btnno.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View view)
    {
        int scoreaux;
        scoreaux=Integer.parseInt(score);
        scoreaux+=1;
        final String usernameaux=username;
        final int useraux=Integer.parseInt(userid);


        Response.Listener<String> responseListener= new Response.Listener<String>()
        {
            @Override
            public void onResponse(String response)
            {
                try
                {
                    JSONObject jsonResponse=new JSONObject(response);
                    boolean success= jsonResponse.getBoolean("success");
                    if(success)
                    {



                       int user=jsonResponse.getInt("id_user");
                       String nameuser=jsonResponse.getString("username");
                       int scoreb=jsonResponse.getInt("score");


                        Intent intent=new Intent (Digits.this, Digits2.class);

                       intent.putExtra("id_user", user);
                        intent.putExtra("username", nameuser);
                       intent.putExtra("score", scoreb);




                        Digits.this.startActivity(intent);


                    }
                    else
                    {
                        AlertDialog.Builder builder=new AlertDialog.Builder(Digits.this);
                        builder.setMessage("User or Id incorrect")
                                .setNegativeButton("Retry", null)
                                .create().show();
                    }
                }

                catch (JSONException e)
                {
                    e.printStackTrace();
                }
            }//fin de método onResponse
        };//fin de Response.Listener<String>

        ScoreRequest scoreRequest= new ScoreRequest(useraux,usernameaux,scoreaux, responseListener);
        RequestQueue queue= Volley.newRequestQueue(Digits.this);
        queue.add(scoreRequest);

    }//fin de onclick
});//fin de setOnClickListener

//fin de método btnno

public class Digits2 extends AppCompatActivity { String username; int userid, score;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_digits2);

    Intent intent =getIntent();
    score=intent.getIntExtra("score",-1);
    username=intent.getStringExtra("username");
    userid=intent.getIntExtra("id_user",-1);
}

}

Notice: Undefined index: id_user in C:\xampp\htdocs\Score.php on line 4

Notice: Undefined index: username in C:\xampp\htdocs\Score.php on line 5

Notice: Undefined index: score in C:\xampp\htdocs\Score.php on line 6 {"success":false}

$con = mysqli_connect("localhost", "root", "", "matgic");

    $id_user = $_POST["id_user"];
    $username = $_POST["username"];
    $score = $_POST["score"];
    
    
    $statement = mysqli_prepare($con, "UPDATE user SET score=? WHERE id_user = ? AND username =?");


    mysqli_stmt_bind_param($statement, "isi",$id_user, $username,$score);
    mysqli_stmt_execute($statement);

    mysqli_stmt_store_result($statement);
    

    $response = array();
    $response["success"] = false; 
    
    
    while(mysqli_stmt_fetch($statement))
    {
        $response["success"] = true; 
        $response["user_id"] = $user_id; 
        $response["name"] = $name;
        $response["username"] = $username;
        $response["password"] = $password;
        $response["date"] = $date;
        $response["email"] = $email;
        $response["score"] = $score;
        }
    
    echo json_encode($response);
public class ScoreRequest extends StringRequest
{

    private static final String Score_Request_URL="http://192.168.1.221/Score.php";//declaramos una constante
    private Map<String, String> params;

    //constructor de la clase
    public ScoreRequest (int id_user, String username, int score, Response.Listener<String> listener)
    {
        super(Request.Method.POST,Score_Request_URL,listener, null);
        params= new HashMap<>();
        params.put("id_user", id_user+"");
        params.put("username", username);
        params.put("score", score+"");


    }//fin de constructor



    @Override
    public Map <String, String> getParams()
    {
        return params;
    }//fin de método Map


}//fin de clase
btnno.setOnClickListener(new View.OnClickListener()
    {

        @Override
        public void onClick(View view)
        {
            int scoreaux;
            scoreaux=Integer.parseInt(score);
            scoreaux+=1;
            final String usernameaux=username;
            final int useraux=Integer.parseInt(userid);


            Response.Listener<String> responseListener= new Response.Listener<String>()
            {
                @Override
                public void onResponse(String response)
                {
                    try
                    {
                        JSONObject jsonResponse=new JSONObject(response);
                        boolean success= jsonResponse.getBoolean("success");
                        if(success)
                        {



                           int user=jsonResponse.getInt("id_user");
                           String nameuser=jsonResponse.getString("username");
                           int scoreb=jsonResponse.getInt("score");


                            Intent intent=new Intent (Digits.this, Digits2.class);

                           intent.putExtra("id_user", user);
                            intent.putExtra("username", nameuser);
                           intent.putExtra("score", scoreb);




                            Digits.this.startActivity(intent);


                        }
                        else
                        {
                            AlertDialog.Builder builder=new AlertDialog.Builder(Digits.this);
                            builder.setMessage("User or Id incorrect")
                                    .setNegativeButton("Retry", null)
                                    .create().show();
                        }
                    }

                    catch (JSONException e)
                    {
                        e.printStackTrace();
                    }
                }//fin de método onResponse
            };//fin de Response.Listener<String>

            ScoreRequest scoreRequest= new ScoreRequest(useraux,usernameaux,scoreaux, responseListener);
            RequestQueue queue= Volley.newRequestQueue(Digits.this);
            queue.add(scoreRequest);

        }//fin de onclick
    });//fin de setOnClickListener

    //fin de método btnno
public class Digits2 extends AppCompatActivity
{
    String username;
    int userid, score;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_digits2);

        Intent intent =getIntent();
        score=intent.getIntExtra("score",-1);
        username=intent.getStringExtra("username");
        userid=intent.getIntExtra("id_user",-1);
    }
}

Notice: Undefined index: id_user in C:\xampp\htdocs\Score.php on line 4

Notice: Undefined index: username in C:\xampp\htdocs\Score.php on line 5

Notice: Undefined index: score in C:\xampp\htdocs\Score.php on line 6 {"success":false}

Origen Enlace
Sergio
  • 103
  • 1
  • 10

Actualización de Base de datos mysql con php

Tengo un archivo Score.php para realizar update a un campo llamado score de tipo entero en una base de datos mysql:

$con = mysqli_connect("localhost", "root", "", "matgic");

$id_user = $_POST["id_user"];
$username = $_POST["username"];
$score = $_POST["score"];


$statement = mysqli_prepare($con, "UPDATE user SET score=? WHERE id_user = ? AND username =?");


mysqli_stmt_bind_param($statement, "isi",$id_user, $username,$score);
mysqli_stmt_execute($statement);

mysqli_stmt_store_result($statement);


$response = array();
$response["success"] = false; 


while(mysqli_stmt_fetch($statement))
{
    $response["success"] = true; 
    $response["user_id"] = $user_id; 
    $response["name"] = $name;
    $response["username"] = $username;
    $response["password"] = $password;
    $response["date"] = $date;
    $response["email"] = $email;
    $response["score"] = $score;
    }

echo json_encode($response);

La clase en android studio que invoca a ese archivo php es:

public class ScoreRequest extends StringRequest {

private static final String Score_Request_URL="http://192.168.1.221/Score.php";//declaramos una constante
private Map<String, String> params;

//constructor de la clase
public ScoreRequest (int id_user, String username, int score, Response.Listener<String> listener)
{
    super(Request.Method.POST,Score_Request_URL,listener, null);
    params= new HashMap<>();
    params.put("id_user", id_user+"");
    params.put("username", username);
    params.put("score", score+"");


}//fin de constructor



@Override
public Map <String, String> getParams()
{
    return params;
}//fin de método Map

}//fin de clase

también tengo el activity2.java donde necesito que cuando presione un Button llamado btnno realice la actualización en la base de datos del campo llamado score, y al mismo tiempo me conduzca a otro activity2 con el campo score actualizado ya que le sumo 1.

el codigo del método del Button (btnno) en activity es:

btnno.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View view)
    {
        int scoreaux;
        scoreaux=Integer.parseInt(score);
        scoreaux+=1;
        final String usernameaux=username;
        final int useraux=Integer.parseInt(userid);


        Response.Listener<String> responseListener= new Response.Listener<String>()
        {
            @Override
            public void onResponse(String response)
            {
                try
                {
                    JSONObject jsonResponse=new JSONObject(response);
                    boolean success= jsonResponse.getBoolean("success");
                    if(success)
                    {



                       int user=jsonResponse.getInt("id_user");
                       String nameuser=jsonResponse.getString("username");
                       int scoreb=jsonResponse.getInt("score");


                        Intent intent=new Intent (Digits.this, Digits2.class);

                       intent.putExtra("id_user", user);
                        intent.putExtra("username", nameuser);
                       intent.putExtra("score", scoreb);




                        Digits.this.startActivity(intent);


                    }
                    else
                    {
                        AlertDialog.Builder builder=new AlertDialog.Builder(Digits.this);
                        builder.setMessage("User or Id incorrect")
                                .setNegativeButton("Retry", null)
                                .create().show();
                    }
                }

                catch (JSONException e)
                {
                    e.printStackTrace();
                }
            }//fin de método onResponse
        };//fin de Response.Listener<String>

        ScoreRequest scoreRequest= new ScoreRequest(useraux,usernameaux,scoreaux, responseListener);
        RequestQueue queue= Volley.newRequestQueue(Digits.this);
        queue.add(scoreRequest);

    }//fin de onclick
});//fin de setOnClickListener

//fin de método btnno

y el codigo de activity2 es:

public class Digits2 extends AppCompatActivity { String username; int userid, score;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_digits2);

    Intent intent =getIntent();
    score=intent.getIntExtra("score",-1);
    username=intent.getStringExtra("username");
    userid=intent.getIntExtra("id_user",-1);
}

}

cuando ejecuto el php en el navegador me devuelve esto:

Notice: Undefined index: id_user in C:\xampp\htdocs\Score.php on line 4

Notice: Undefined index: username in C:\xampp\htdocs\Score.php on line 5

Notice: Undefined index: score in C:\xampp\htdocs\Score.php on line 6 {"success":false}

El problema es que cuando ejecuto la aplicación no ingresa al success porque coge el else y devuelve el AlertDialog con "User o id incorrect", por lo poco que entiendo la consulta esta retornando false, pero no se la razón, de antemano gracias por su valioso tiempo.