如何在eclipse中使用php同时更新和插入数据库

时间:2015-04-10 14:09:28

标签: php mysql eclipse phpmyadmin wampserver

以下是我的eclipse部分代码和php文件。我想在单击拒绝按钮时在一个php文件中插入和更新数据库表。我的插入查询执行得很好,但它没有更新具有列名状态的表。我该怎么做?

Eclipse代码部分

JAVA

{
    // somecode here
    btndisapp=(Button)findViewById(R.id.btndisapp);
    btndisapp.setOnClickListener(new View.OnClickListener(){
    InputStream is=null;
    @Override
    public void onClick(View arg0){
        // TODO Auto-generated method stub
        //String phone=Integer.toString();
        String name=""+tvname.getText().toString();
        String purpose=""+tvpurpose.getText().toString();
        String tfrom=""+tvtfrom.getText().toString();
        String job=""+tvjob.getText().toString();
        String tto=""+tvtto.getText().toString();
        String dfrom=""+tvdfrom.getText().toString();
        String dto=""+tvdto.getText().toString();
        List<NameValuePair>nameValuePairs=new ArrayList<NameValuePair>(7);
        nameValuePairs.add(new BasicNameValuePair("name",name));
        nameValuePairs.add(new BasicNameValuePair("purpose",purpose));
        nameValuePairs.add(new BasicNameValuePair("tfrom",tfrom));
        nameValuePairs.add(new BasicNameValuePair("job",job));
        nameValuePairs.add(new BasicNameValuePair("tto",tto));
        nameValuePairs.add(new BasicNameValuePair("dfrom",dfrom));
        nameValuePairs.add(new BasicNameValuePair("dto",dto));
        //nameValuePairs.add(new BasicNameValuePair("item",item));

        try{
              HttpClient httpClient=new DefaultHttpClient();

            HttpPost httpPost=new HttpPost("http://10.0.2.2/test/adddisappod.php");
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            //ArrayList<NameValuePair> nameValuePairs1 = new ArrayList<NameValuePair>();

            //nameValuePairs.add(new BasicNameValuePair("id",id));

            HttpResponse response=httpClient.execute(httpPost);
            HttpEntity entity=response.getEntity();
            is=entity.getContent();
            String msg="Dispproved";
            Toast.makeText(getApplicationContext(),msg,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','');
if(!$con)
{
  die('Could not connect: '. mysql_error());
}
mysql_select_db("test",$con);

$name=$_POST['name'];
$job=$_POST['job'];
$purpose=$_POST['purpose'];
$dfrom=$_POST['dfrom'];
$tfrom=$_POST['tfrom'];
$dto=$_POST['dto'];
$tto=$_POST['tto'];
$ename=$_GET['name'];

mysql_query("insert into odattended 
               (ename, ejob, selectjob, purpose, datefrom, 
                dateto, timefrom, timetill, status)   
             values('{$name}', '{$job}', '{$job}', '{$purpose}', 
                    '{$dfrom}', '{$dto}', '{$tfrom}', '{$tto}', 
                    'disapproved')"
           );
mysql_query("update od 
               SET status='disapproved' 
             where ename='."$name."'");

mysql_close();
?>

1 个答案:

答案 0 :(得分:-1)

检查语法。正确的更新查询:

mysql_query("update od SET status='disapproved' where ename='$name'");