简体   繁体   中英

How to Update and Insert database at same time using php in eclipse

Following is my eclipse part code and php file. I want to insert and update the database tables in one php file when disapprove button is clicked. My insert query executes just fine but it don't update the table with column name status. How do I do that?

Eclipse code part

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 File

<?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();
?>

Check your syntax. correct update query:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM