我有mysql表" nhanvien",它有三个字段(id,maso,hoten) 我想插入表nhanvien。 我有PHP代码。
/*
* Following code will create a new product row
* All product details are read from HTTP Post Request
*/
function customError($errno, $errstr) {
echo "<b>Error:</b> [$errno] $errstr";
}
//set error handler
set_error_handler("customError");
// array for JSON response
$response = array();
//connect to database
$link = mysqli_connect("localhost", "root", "", "nhanvien");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
// check for required fields
if (isset($_POST['id']) && isset($_POST['maso']) && isset($_POST['hoten'])) {
$id=$_POST['id'];
$Maso=$_POST['maso'];
$Hoten=$_POST['hoten'];
// mysql inserting a new row
$result = mysqli_query($link,"INSERT INTO nhanvien(id, Maso,Hoten) VALUES('$id', '$Maso', '$Hoten')");
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "nhanvien successfully created.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
<html>
<body>
<form action="<?php $_PHP_SELF ?>" method="POST">
id: <input type="text" name="id" />
maso: <input type="text" name="maso" />
hoten: <input type="text" name="hoten" />
<input type="submit" />
</form>
</body>
</html>
我的测试成功。但是: 我有php文件:creat_nhanvien.php
<?php
/*
* Following code will create a new product row
* All product details are read from HTTP Post Request
*/
function customError($errno, $errstr) {
echo "<b>Error:</b> [$errno] $errstr";
}
//set error handler
set_error_handler("customError");
// array for JSON response
$response = array();
//connect to database
$link = mysqli_connect("localhost", "root", "", "nhanvien");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
// check for required fields
if (isset($_POST['id']) && isset($_POST['maso']) && isset($_POST['hoten'])) {
$id=$_POST['id'];
$Maso=$_POST['maso'];
$Hoten=$_POST['hoten'];
// mysql inserting a new row
$result = mysqli_query($link,"INSERT INTO nhanvien(id, Maso,Hoten) VALUES('$id', '$Maso', '$Hoten')");
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "nhanvien successfully created.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
我有一个代码android。
try {
Map<String, Object> para = new LinkedHashMap<>();
para.put("id", "10");
para.put("maso", "A10");
para.put("hoten", "nguyen van u");
System.out.println(para);
StringBuilder postData = new StringBuilder();
for (Map.Entry<String, Object> param : para.entrySet()) {
if (postData.length() != 0) postData.append('&');
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postData.append('=');
poststData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
String myPara = postData.toString();
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
//System.out.println(postDataBytes.toString().getBytes());
URL murl = new URL(url);
HttpURLConnection httpURLConnection = HttpURLConnection)murl.openConnection();
//httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data");
httpURLConnection.setRequestProperty("Content-Length", "" + Integer.toString(postDataBytes.toString().getBytes().length));
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
//httpURLConnection.connect();
DataOutputStream out = new DataOutputStream(httpURLConnection.getOutputStream());
//httpURLConnection.getOutputStream().write(postDataBytes.toString().getBytes());
out.writeBytes(postDataBytes.toString());
out.flush();
out.close();
}catch (Exception e) {
e.printStackTrace();
}
代码不工作。看来我不能发送参数到php文件或? 你能帮我吗。 非常非常非常非常感谢你。 对不起,我不擅长英语。