0

So currently i made menu to send a data from android to mysql database, but when i press send button that error always come, here is my php code(addKomen.php)

<?php 
if($_SERVER['REQUEST_METHOD']=='POST'){

    //Getting values
    $nama = $_POST['nama'];
    $email = $_POST['email'];
    $isi = $_POST['isi'];

    //Creating an sql query
    $sql = "INSERT INTO email (nama,email,isi) VALUES ('$nama','$email','$isi')";

    //Importing our db connection script
    require_once('dbConnect.php');

    //Executing query to database
    if(mysqli_query($con,$sql)){
        echo 'Added Successfully';
    }else{
        echo 'Could Not Add';
    }

    //Closing the database 
    mysqli_close($con);
}

?>

So the problem is in my android code, because i type different variable thanks for all the answer and then the problem is in my android code. Best regards

  • 4
    **Your code is open to SQL injections.** What error ? – Mayank Pandeyz Aug 20 '17 at 07:37
  • undefined index: nama – Syamsul Maarif Aug 20 '17 at 07:42
  • Then you're not posting the data correctly, which means that the issue is in your android code, not the PHP part. You should show us _all_ the relevant code. – M. Eriksson Aug 20 '17 at 07:42
  • Is id_email always 0? – Peter M Aug 20 '17 at 07:43
  • 2
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – hassan Aug 20 '17 at 07:44
  • NEVER TAKE USER SENT DATA AND INSERT IT INTO SQL DIRECTLY. Always do it through bind variables. Its both more efficient (the db can optimize the query) and its secure. I could trash your database in 30 characters with this code. – Gabe Sechan Aug 20 '17 at 07:47
  • so what should i do? do you have any example of it? it'll be pleasure – Syamsul Maarif Aug 20 '17 at 08:02

1 Answers1

0

You are not posting the data correctly, and to resolve the undefined index: nama use isset() to check whether the index you are trying to get is exist or not like:

if( isset( $_POST['nama'] ) )
{
    // This block will execute only if nama have some value in it.
}
Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59