0

我正在尝试在我的 Android 应用程序上构建一个基本表单,该表单接收用户的姓名、号码和电子邮件地址并将其发送到我的网站。我在服务器端有一个基本的 php 脚本,用于将这些详细信息存储到一个简单的 csv 文件中。将详细信息发送到服务器的 Android 代码:

private void sendCredentials(String name, String number, String emailID) {

//code to send to server should begin here.
        HttpClient hc = new DefaultHttpClient();
        HttpPost hp = new HttpPost("http://www.mywebsite.com/takeDetails.php");

        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("name", name));
            nameValuePairs.add(new BasicNameValuePair("number", number));
            nameValuePairs.add(new BasicNameValuePair("email", emailID));
            hp.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            // Execute HTTP Post Request
            HttpResponse response = hc.execute(hp);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
}

在服务器端,php 脚本:

<?php

 $filename="userCredentials.txt";
 $name = $_POST['name'];
 $number = $_POST['number'];
 $email = $_POST['emailID'];
 $fp = fopen($filename, "a");
 $str = "new," . $name . "," . $number . "," $email . "n";
 fwrite($fp, $str);
 fclose($fp);
 $msg = "<h1> Your data has been saved. </h1>";
 echo $msg; ?>  

我的问题:
在我网站的文件管理器中,没有创建名为 userCredentials.txt 的文件。

我的问题:
1. 为什么没有创建 userCredentials.txt?
2.我以前从未在PHP中做过任何工作,那么上面的代码片段是正确的方法吗?

4

1 回答 1

1

连接时,您忘记了变量.前的(点) 。$email它适当地引起了PARSE_ERROR. 所以跳过了脚本。

改变:

$str = "new," . $name . "," . $number . "," $email . "n";

至:

$str = "new," . $name . "," . $number . "," . $email . "n";

如果要\n在行尾添加换行符,PHP_EOL则应"n"$str.

于 2013-04-19T03:39:37.693 回答