0

Here's my code to connect my android studio in my database using these code and and it is running without an error.

        TextView textView = findViewById(R.id.t1);
        EditText sname = findViewById(R.id.et1);
        EditText salary = findViewById(R.id.et2);
        Button b1 = findViewById(R.id.b1);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String url = "http://127.0.0.1:80/New%20folder/new.php:name=" + sname.getText().toString() + "salary=" + salary.getText().toString();

                StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {

                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                // Handle error
                            }
                        });
            }
        });


    }
}

My question is here in my PhP code

<?php

    $mysqli = new mysqli("127.0.0.1","root","","try");
    
    // Check connection
    if ($mysqli -> connect_error) {
      echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
    }
    else
    {
    echo "dasds";
    $s = $_GET['sname'];
    $u = $_GET['salary'];
    
    $res=mysql_query($con, "Insert into information (name,salary) values('$s','$u')");
    }
    $conn->close();
    ?>

Here's the error. How should I fix this?

Warning: Undefined array key "sname" in C:\xampp\htdocs\New folder\new.php on line 12

Warning: Undefined array key "salary" in C:\xampp\htdocs\New folder\new.php on line 13

Fatal error: Uncaught Error: Call to undefined function mysql_query() in C:\xampp\htdocs\New folder\new.php:15 Stack trace: #0 {main} thrown in C:\xampp\htdocs\New folder\new.php on line 15

  • 1
    1) search on Google to learn what a URL querystring looks like (you need ? at the start, not :, and there needs to be an & between each parameter) and 2) stop using mysql_query because it was deleted from PHP several years ago. I recommend using mysqli or PDO for data access instead - looks like maybe you tried to use mysqli_query and made a typo...but also make sure you learn how to use the prepared statements and parameters feature to ensure your data is secure when dealing with input from external sources – ADyson Nov 04 '21 at 07:19
  • 2
    **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Nov 04 '21 at 11:41
  • Thanks, I've change the code using the PDO. However, I'm still getting this error: ============================================= Warning: Undefined array key "sname" in C:\xampp\htdocs\New folder\new.php on line 19 Warning: Undefined array key "salary" in C:\xampp\htdocs\New folder\new.php on line 20 ================================== How do I connect this: String url = "http://127.0.0.1:80/New%20folder/new.php:name=" + sname.getText().toString() + "salary=" + salary.getText().toString(); to my Php? – John Clark Labasan Nov 05 '21 at 12:02

0 Answers0