无法插入数据库

时间:2017-01-03 05:14:07

标签: php mysqli

我想从我的Android应用程序插入数据,但我的android仍然无法插入数据库mysql。我的PHP代码有什么问题?

<?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";
}
?>

2 个答案:

答案 0 :(得分:0)

试试这个:

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";
}

答案 1 :(得分:-1)

我发现你的php api中没有允许任何标题。尝试将这些标题添加到PHP脚本的顶部

header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400');