2014-03-01 2 views
0

mysql에서 PHP pdo로 데이터를 가져 오기 위해 쿼리를 실행하고 싶습니다.PHP PDO - 어디에서 선택하십시오 (입력 필드에서 값 가져 오기)

쿼리는 다음과 같이해야합니다 : 그래서 처음 SELECT * FROM akt_djubrenje where ID_akt = (I need to get value from html with ajax)...

내가 MySQL의 데이터를 가지고 : 내가 필요로하는 방법

<input id="akt_djubrenje" name="akt_djubrenje" type="text" placeholder="1" value="1" class="form-control input-md"> 

: 또한

CREATE TABLE IF NOT EXISTS `akt_djubrenje` (
    `ID` int(11) NOT NULL AUTO_INCREMENT, 
    `ID_akt` int(11) NOT NULL, 
    `hemija` varchar(30) NOT NULL, 
    `kol` int(11) NOT NULL, 
    `jmere` varchar(5) NOT NULL, 
    PRIMARY KEY (`ID`) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ; 

내가 값을 입력 필드에 HTML이를 mysql에서 데이터를 가져 오는 경우 ID_akt = $_POST['akt_djubrenje']

그래서 이 PHP의 PDO 파일을 의식 :

또한 내가 아약스와의 PHP 파일을 호출
try { 
     /* Establish the database connection */ 
     $conn = new PDO("mysql:host=localhost;dbname=$dbname", $username, $password); 
     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

     $statement->execute(array(':akt_djubrenje' => $_POST['akt_djubrenje'])); 

     $result = $conn->query('SELECT * FROM akt_djubrenje where ID_akt = :akt_djubrenje"'); 



     $rows = array(); 
     $table = array(); 
     $table['cols'] = array(

     array('label' => 'ID', 'type' => 'number'), 
     array('label' => 'Hemija', 'type' => 'string'), 
     array('label' => 'Kolicina', 'type' => 'number'), 
     array('label' => 'Jed.mere', 'type' => 'string') 

    ); 
     foreach($result as $r) { 
      $temp = array(); 
      // the following line will be used to slice the Pie chart 
      $temp[] = array('v' => (int) $r['ID']); 
     $temp[] = array('v' => (string) $r['hemija']); 
     $temp[] = array('v' => (int) $r['kol']); 
     $temp[] = array('v' => (int) $r['jmere']); 

      $rows[] = array('c' => $temp); 
     } 

    $table['rows'] = $rows; 

    $jsonTable = json_encode($table); 
    } catch(PDOException $e) { 
     echo 'ERROR: ' . $e->getMessage(); 
    } 
    echo $jsonTable; 

:

  function tabela() { 
       var json = $.ajax({ 
       url: 'getdjubrenje.php', // make this url point to the data file 
       dataType: 'json', 
       async: false 
      }).responseText; 


    var data = new google.visualization.DataTable(json); 


    visualization = new google.visualization.Table(document.getElementById('akt_djubrenje')); 
    visualization.draw(data, null); 
} 

그러나 나는 아무것도 얻을 해달라고?

누가 여기에서 문제가 될 수 있는지, 어떻게 해결할 수 있습니까? 나는 PHP 파일을 실행할 때

또한 내가 얻을 : Fatal error: Call to a member function execute() on a non-object in /home/agroagro/public_html/getdjubrenje.php on line 18

UPDATE : 쿼리에서 execute() 명령을 이동해야 enter image description here

답변

2

는 또한 쿼리

$result = $conn->query("SELECT * FROM akt_djubrenje where ID_akt = :akt_djubrenje"); 
$result->execute(array(':akt_djubrenje' => $_POST['akt_djubrenje'])); 
로 생성 된 객체를 사용할 필요가

문서에서

  • call PDOStatement::bindParam() to bind PHP variables to the parameter markers: bound variables pass their value as input and receive the output value, if any, of their associated parameter markers
  • or pass an array of input-only parameter values

학습하면, 서버 측에서 귀하의 아약스 전화

 $('#akt_djubrenje').on('click',function() { 
      var data=$(this).val(); 
      $.ajax({ 
       url: 'getdjubrenje.php', // make this url point to the data file 
       dataType: 'json', 
       data:{'akt_djubrenje':data}, 
       async: false, 
       success:function(json){ 
        var data = new google.visualization.DataTable(json); 
        visualization = new google.visualization.Table(document.getElementById('akt_djubrenje')); 
        visualization.draw(data, null); 
       } 
      }); 
     }); 

에서 서버 스크립트에 데이터를 전달하는 HTML 파일에서 전달 된 데이터를 검색 할 수 $_POST['akt_djubrenje']를 호출 할 필요가 here

+0

좋아요, 그렇지만 다시 할 수 있습니다. 아무 것도 볼 수 없습니다. jquery 아약스 코드에 문제가 있습니까? – gmaestro

+0

나는이 일을하지만 지금은 PHP 파일을 실행할 때 : 오류 : SQLSTATE [42000] : 구문 오류 또는 액세스 위반 : 1064 SQL 구문에 오류가 있습니다. 귀하의 MySQL 서버 버전에 해당하는 설명서를 확인하여 올바른 구문을 찾으십시오. 'akt_djubrenje' '1 번 근처에서 사용하십시오. – gmaestro

+0

업데이트 된 답변을 확인하십시오. 쿼리에서 따옴표를 변경 했으므로 따옴표를 더 사용했습니다. – Fabio

1

더.

해피 코딩 :

+0

다시 작동하지 않습니다 : http://agroagro.com/aktivnosti1.html (클릭 버튼을 새로 추가하고 탭 mehanizacija ... 정말 문제가 무엇인지 모르겠다 – gmaestro

+0

@ gmaestro : 서버 스크립트에서 성공적으로 수신 한 데이터 'akt_djubrenje'입니까? – dreamweiver

+0

예 ..... 저는 tabela_djubrenje 함수를 호출해야합니다.); button 클릭 여부에 대한 함수? – gmaestro

관련 문제