public static void executeMultipartPost(String url, String id, String field1, String field2){ try { HttpClient client = new DefaultHttpClient(); HttpPost poster = new HttpPost(url); MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); entity.addPart("name", new StringBody(field1)); entity.addPart("id", new StringBody(id)); entity.addPart("text", new StringBody(field2)); poster.setEntity(entity ); client.execute(poster, new ResponseHandler<Object>() { public Object handleResponse(HttpResponse response) throws ClientProtocolException, IOException { HttpEntity respEntity = response.getEntity(); String responseString = EntityUtils.toString(respEntity); // do something with the response string return null; } }); } catch (Exception e){ //do something with the error } } 

This is my function for adding a comment.

the problem is that it does not add

my php file

  <?php try{ $handler = new PDO('mysql:host=***;dbname=***', '***', '***' ); $handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); }catch(Exception $e){ echo $e->getMessage(); die(); } $name = $_POST["name"]; $page_id = $_POST["id"]; $text_comment = $_POST["text"]; $equery = $handler -> query("INSERT INTO `Commnets` (`name`, `page_id`, `text_comment`) VALUES ('$name', '$page_id', '$text_comment')"); $records = array(); 

I think the problem is in php

can you write me right?

  • 2
    1. Change the password to the base; 2. You can not collect a request to the database of unshielded field values; 3. Look in the server log and post the error that gives PHP - tutankhamun
  • @tutankhamun can not be changed. There external connections are closed. I tried. - MAXOPKA
  • one
    @MAXOPKA I also tried. But this does not mean that I will not be able to access the database if I’m found to be a "hosting neighbor". - tutankhamun
  • add file_put_contents('test.log',var_export($_POST,true)) in php and check if the post comes up with a request. - Naumov

1 answer 1

I will explain all the same in more detail:

 <?php try { // Тут вы опубликовали атрибуты доступа к базе. Так делать не стОит. // Поскольку на этом сайте хранятся версии редактируемого контента // у вас остается один выход - поменять пароль (может даже и имя) $handler = new PDO('mysql:host=***;dbname=***', '***', '***' ); $handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (Exception $e){ echo $e->getMessage(); die(); } $name = $_POST["name"]; $page_id = $_POST["id"]; $text_comment = $_POST["text"]; // В запросе вы возможно опечатались и вместо Commnets следует написать // Comments. Кроме того сделаем подготовленный запрос и внедрим в него // значения через плейсхолдеры и обработку запроса сделаем внутри блока try try { $equery = $handler->prepare("INSERT INTO `Commnets` (`name`, `page_id`, `text_comment`) VALUES (:name, :page_id, :text_comment)"); $equery->bindValue(':name', $name); $equery->bindValue(':page_id', $page_id); $equery->bindValue(':text_comment', $text_comment); $equery->exec(); } catch (Exception $e){ echo $e->getMessage(); die(); } $records = array();