-2

I am not familiar with PHP,but I need to use PHP as backend for insert data into database from my android app using xamp local host.I got a PHP code but it shows the error "undefined index in line 4 (username)".I cant find the ERROR, so please help me, thanks.

here is my code

   <?php
    mysql_connect("localhost","root","");
    mysql_select_db("taskmanager");
    $name=null;
    $name=$_POST['username'];
    $ins="insert into userlog (username)values('$name')";
    mysql_query($ins);
    mysql_close();
   ?>
Carlos Robles
  • 10,610
  • 3
  • 39
  • 57
prince
  • 332
  • 3
  • 13
  • 1
    Check if you are really passing the `username` as a parameter from your android app. – Shankar Narayana Damodaran Feb 05 '14 at 07:38
  • ya am passing username – prince Feb 05 '14 at 07:38
  • try to pass $name as a static data ones, if that makes your query run then try with handling the post – sourabh kasliwal Feb 05 '14 at 07:40
  • 2
    Side note: You shouldn't use `mysql_*()` for new code - it's deprecated. Use `mysqli_*()` or `PDO`. You're also open to SQL Injection - make sure you escape your user input properly, or use prepared statements. –  Feb 05 '14 at 07:42
  • am using array list,to store my details..so any prblm with calling username ??? – prince Feb 05 '14 at 07:42
  • 1
    If PHP says $_POST does not include username, it doesn't. The error is most likely in the android app. – Joni Feb 05 '14 at 07:44
  • You are posting a few lines of code and they are FULL of catastrophical errors. Dude, start over and learn PHP from scratch! **You are doing it wrong.** – Sliq Feb 05 '14 at 07:48
  • Use this: print_r($_POST); if username is not included in it then it is android app problem – Adam Radomski Feb 05 '14 at 07:49

2 Answers2

2

Here is updated code

mysql_connect("localhost","root","");
mysql_select_db("taskmanager");    
$name= (isset($_POST['username'])) ? $_POST['username'] : '';
$ins="insert into userlog (username)values('$name')";
mysql_query($ins);
mysql_close();
Jignesh Patel
  • 1,053
  • 6
  • 10
1

Try this

if(isset($_POST['username']))
    {
        mysql_connect("localhost","root","");
        mysql_select_db("taskmanager");
        $name=$_POST['username'];
        $ins="insert into userlog (username)values('$name')";
        mysql_query($ins);
        mysql_close();
    }
else
{
 echo 'No POST request';
}
ghost
  • 467
  • 2
  • 6
  • 18