php - 从 Android 获取要存储到 php mysql 中的 DatePicker 值

标签 php android mysql date datepicker

大家好!

我目前正在研究如何使用 android 的日期选择器将日期值放入我的 php mysql 中。我的计划是从日期选择器获取数据,无论它的值是什么,它都会被放入 mysql 中。由于某种原因,没有错误,但每次我将其放入数据库时​​,它只是将值“0000-00-00”输入到 mysql 中。以下是我的java android代码。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_datetime);

    //setup strict mode policy
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    timePicker = (TimePicker) findViewById(R.id.timePicker);
    timePicker.setIs24HourView(true);
    datePicker = (DatePicker) findViewById(R.id.datePicker);
    //lblTime = (TextView) findViewById(R.id.lblTime);
    btnAddTime = (Button) findViewById(R.id.btnAddTime);

    btnAddTime.setOnClickListener(new View.OnClickListener() {

        InputStream is = null;
        @Override
        public void onClick(View v) {

            //String time = lblTime.getText().toString();
            //String time = timePicker.getCurrentHour() + ":" + timePicker.getCurrentMinute() + ":" + "00";
            String date = datePicker.getYear() + "-" + datePicker.getMonth() + 1 + "-" + datePicker.getDayOfMonth();


            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);

            nameValuePairs.add(new BasicNameValuePair ("date", date));

            try {

                //setting up the default http client
                HttpClient httpClient = new DefaultHttpClient();

                //setting up the http post method and passing the url in case
                //of online database and the ip address in case of localhost database
                //and the php file which serves as a link between the android app
                //and the database.
                HttpPost httpPost = new HttpPost("http://10.0.2.2/add_time.php");

                //passing the nameValuePairs inside the httppost
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                //getting the response
                HttpResponse response = httpClient.execute(httpPost);

                //setting up the entity
                HttpEntity httpEntity = response.getEntity();

                //setting up the content inside an input stream reader
                //lets define the input stream reader
                is = httpEntity.getContent();

                //Toast message if the data is successfully entered
                Toast.makeText(getApplicationContext(), "Date entered succesfully", Toast.LENGTH_LONG).show();
            }
            catch (ClientProtocolException e) {

                Log.e("ClientProtocol", "Log_tag");
                e.printStackTrace();
            }
            catch (IOException e) {
                Log.e("Log tag", "IOException");
                e.printStackTrace();

            }
        }

    });

以下是我的 php 代码:

<?php
error_reporting(E_ALL ^ E_DEPRECATED);

$con = mysql_connect('localhost', 'root', '');
mysql_select_db('testdb', $con);

$date = $_POST['date'];

mysql_query("Insert into datetable (date) values('{$date}')");


?>

如果有人可以帮助我,我将非常感激,谢谢!

最佳答案

首先你没有打电话

 datePicker.init(year, month, day, dateSetListener);

其次你还没有实现OnDateChangedListener

  private DatePicker.OnDateChangedListener dateSetListener = new DatePicker.OnDateChangedListener() {

        public void onDateChanged(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {

// Use these values to pass....

        }
    };

关于php - 从 Android 获取要存储到 php mysql 中的 DatePicker 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28796508/

相关文章:

javascript - 更改谷歌地图中的javascript不会更新它?

android - 如何隐藏/删除android中的标题栏

java - 暂停时如何恢复 MediaPlayer

php - 如何在PHP中获取变量内容的大小

PHP:将相当于用户在表单选择中的选择的数据库插入到变量中?

android - 如何在 android ListView 中设置页脚以动态拉伸(stretch)到底部?

SQL:如何选择每个客户最后购买的产品?

Mysql错误1066不唯一的表/别名

mysql - 在 J2EE/MySQL 后端使用 &lt;input type=file/>

php - 简单的 jQuery - php 通信