duange2971 2012-03-29 01:59
浏览 35

无法在变量中使用$ _POST()在PHP端存储值,但可以轻松地将其存储在数据库中

I am accepting values from android to php. My android code successfully sends data to php server, but my php code is unable to store the value of $_POST() variable in another variable.But when i directly put the $_POST() in insert query, the the value gets stored in the database.When i try var_dump($_POST()) it shows null. Can you pls help me , on how to get the $_POST() value in another variable.

Android Code

//send.java
package com.exg.zzz;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class send extends Activity {
EditText edit_uid,edit_pwd;
Button btn_login;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    edit_uid=(EditText) findViewById(R.id.edit_uid);
    edit_pwd=(EditText) findViewById(R.id.edit_pwd);
    btn_login=(Button) findViewById(R.id.btn_login);

    btn_login.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v){
            http_connection();
        }
    });
}

void http_connection()
{

    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);      
    nameValuePairs.add(new BasicNameValuePair("Password",edit_pwd.getText().toString()));
    nameValuePairs.add(new BasicNameValuePair("UID",edit_uid.getText().toString()));

    HttpClient httpclient1 = new DefaultHttpClient();
    HttpPost httppost1 = new HttpPost("http://10.0.2.2/userins1.php");
    Log.v("localhost", "executed");
    try {
        httppost1.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                        httpclient1.execute(httppost1);
                        } catch (ClientProtocolException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    Toast.makeText(this, "Added", Toast.LENGTH_SHORT).show();

}
}

PHP code

//userins1.php
<?php
$username = "root";
$password = "";
$hostname = "localhost";

//var_dump() display the data type and contents of the variable 
//but it displaying NULL for string data
var_dump($_POST[UID]);

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");

//select a database to work with
$selected = mysql_select_db("akshay",$dbhandle)
or die("Could not select examples");

//execute the SQL query and return records
//this qyery is successfully inserting the data
$sql="INSERT INTO login (uid, password) VALUES ('$_POST[UID]','$_POST[Password]')";

if (!mysql_query($sql,$dbhandle))
{
    die('Error: ' . mysql_error());
}

mysql_close($dbhandle);
?>
  • 写回答

2条回答 默认 最新

  • drduh44480 2012-03-29 02:24
    关注

    Array keys in PHP should be strings not constants.

    • $_POST[UID] becomes $_POST["UID"]
    • $_POST[Password] becomes $_POST["Password"]
    • So forth...

    Also, those values should be escaped first, you need to research SQL injection and mysql_real_escape_string().

    If the problem persists, put var_dump($sql) right after $sql=... or contact the program vendor.

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!