问题描述
我从 java 写了一个简单的帖子到我的 php 后端.运行脚本后,我得到了在文件名、文件大小等中传递的输出.
I write a simple post from java to my php backend.After I run the script I am getting the output that I am passing in filename, filesize etc..
int filesize=25;
String filetype=".txt";
String hash="sdfjksdfhljahe8wr897348957jsdfajlsdhfl48";
String email="[email protected]";
String filename="songs.txt";
String urlParameters = "filename="+filename+"&filesize="+filesize+"&filetype="+filetype+"&filehash="+hash+"&email="+email;
URL url = new URL("myurl");
HttpURLConnection hp=(HttpURLConnection)url.openConnection();
hp.setDoInput(true);
hp.setDoOutput(true);
hp.setInstanceFollowRedirects(false);
hp.setRequestMethod("POST");
hp.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
hp.setRequestProperty("charset", "utf-8");
hp.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
hp.setUseCaches (false);
DataOutputStream wr = new DataOutputStream(hp.getOutputStream ());
wr.writeBytes(urlParameters);
wr.flush();
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(hp.getInputStream()));
line = reader.readLine();
System.out.println(line);
wr.close();
reader.close();
hp.disconnect();
问题是我想将这些值插入到我在服务器上运行的 mysql 数据库中,所以我编写了以下 php 代码.
The problem is I want to insert these values into my mysql database running on server so I write the following php code.
<?php
include('dbc.php');
$filename=$_POST['filename'];
$filesize=$_POST['filesize'];
$filetype=$_POST['filetype'];
echo $POST['email'];
$filehash=$_POST['filehash'];
$email=$_POST['email'];
$query = "INSERT INTO files ( `email`, `file_name`, `file_ext`, `file_size`, 'file_hash') VALUES ( '$email', '$filename', '$filetype', '12584', 'filehash')";
$result=mysqli_query($dbc,$query);
if(mysqli_affected_rows($dbc) == 1)
{
// DO Nothing
}
echo $_POST['filehash'];
echo $_POST['email'];
echo $_POST['filename'];
echo $_POST['filesize'];
echo $_POST['filetype'];
?>
数据正在发布,我在我的 java 应用程序中得到了同样的数据,但没有在 mysql 数据库中输入数据.我有一个与上述相同列的表名文件.我试图从 mysql_error 得到错误,但我没有得到任何错误.
The data is being is posted and I am getting the same back in my java application but data is not being entered in mysql database. I have a table name files with the same columns described above. I tried to get error from mysql_error but I am not getting any error.
更新:问题解决了使用 ' 而不是 ` near file_hash 的愚蠢的 sql 语法错误
UPDATE:Problem solved silly sql syntax mistake using ' instead of ` near file_hash
推荐答案
在将变量输入数据库之前尝试检查它们.你确定它们不是空的?
Try to check your variables before entering them in the database. Are you sure they are not empty?
这篇关于从java发布到php并将其插入mysql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!