0

I want to insert data from my android application, but my android still can't insert to database mysql. whats wrong with my php code?

<?php

define('HOST','mysql.idhostinger.com');
define('USER','*********');
define('PASS','*********');
define('DB','**********');
$con = mysqli_connect(HOST,USER,PASS,DB);

// Inisiasi Variable
$result = "";
if (isset($_POST['nama'])&&($_POST['email'])){
$username = $_POST['nama'];
$email = $_POST['email'];
$password = $_POST['password'];
$tanggal = $_POST['tanggal'];
}
$query = 'INSERT INTO register(username, email, tanggal_lahir, password) VALUE($username, $email, $tanggal, $password)';
if($con->query($query)){
echo "Sukses";
}
else {
echo "Gagal";
}
?>
avinash
  • 1,744
  • 24
  • 40
Denny Kurniawan
  • 168
  • 1
  • 3
  • 21

2 Answers2

0

Try this:

define('HOST','mysql.idhostinger.com');
define('USER','*********');
define('PASS','*********');
define('DB','**********');
$con = mysqli_connect(HOST,USER,PASS,DB);

// Inisiasi Variable
$result = "";
if (isset($_POST['nama'])&&($_POST['email'])){
$username = $_POST['nama'];
$email = $_POST['email'];
$password = $_POST['password'];
$tanggal = $_POST['tanggal'];
}

//This seems wrong approch this code you should write inside above if condition
//What if user didn't entered email and name will you insert blank values?

$query = "INSERT INTO register(username, email, tanggal_lahir, password) VALUES ('".$username."', '".$email."', '".$tanggal."', '".$password."')";
if($con->query($query)){
echo "Sukses";
}
else {
echo "Gagal";
}
Akshay
  • 700
  • 9
  • 23
  • maybe, my android code is wrong. i will ask in another question, thank you – Denny Kurniawan Jan 03 '17 at 05:34
  • I'll bet you (also) went and posted your answer based on [this comment](http://stackoverflow.com/questions/41436809/cant-insert-to-database#comment70079565_41436809). Read [my reply](http://stackoverflow.com/questions/41436809/cant-insert-to-database#comment70093395_41436809) to it. Just because that comment received upvotes doesn't mean it's correct and the ones who upvoted that, don't know MySQL. – Funk Forty Niner Jan 03 '17 at 12:39
  • @Fred-ii- In this kinda questions no one needs to see comments it's simple to answer and literally I didn't even show your comments my another purpose to post this as an answer was he was going with wrong approach as I mentioned in code with comment. I am sorry if you felt that I copied answer you gave in comment. – Akshay Jan 04 '17 at 06:13
  • @Fred-ii- I gave the answer before you commented so there is no chance to copy anything. – Akshay Jan 04 '17 at 06:16
  • @Akshay Look at the variables in the values; what's missing? Hint: they're string literals. – Funk Forty Niner Jan 04 '17 at 12:09
  • @Fred-ii- `INSERT INTO register(`username`, `email`, `tanggal_lahir`, `password`) VALUES ('".$username."', '".$email."', '".$tanggal."', '".$password."')";` – Akshay Jan 05 '17 at 04:12
  • @Akshay exactly. ;-) and you need to edit your answer using that syntax. – Funk Forty Niner Jan 05 '17 at 12:03
  • BTW Thanks Edited @Fred-ii- :D – Akshay Jan 05 '17 at 12:28
-1

I see you are not allowing any headers in your php api. Try adding these headers to the top of your PHP script

header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400');
sam
  • 688
  • 1
  • 12
  • 35
  • after i adding headers on my php, my android not respond for click. but thanks for your answer – Denny Kurniawan Jan 03 '17 at 06:13
  • @DennyKurniawan Are you sure there are not any syntax error. Try adding `header('Access-Control-Allow-Origin: *');` – sam Jan 03 '17 at 06:41
  • thanks for your answer, now problem solved. i am just adding $query = 'INSERT INTO register(username,email,tanggal_lahir,password) VALUES($username,$email,$tanggal,$password)'; – Denny Kurniawan Jan 03 '17 at 08:21