2014-03-01 70 views
0

我想運行查詢從PHP獲取數據從MySQL的Pdo。PHP的PDO - 選擇從哪裏(從輸入字段獲得值)

查詢必須是這樣的:SELECT * FROM akt_djubrenje where ID_akt = (I need to get value from html with ajax)...

所以首先我有一個MySQL數據:

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:

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

我需要怎麼從mysql中取數據,其中ID_akt = $_POST['akt_djubrenje']

所以我w RITE這個PHP PDO文件:

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; 

也是我稱之爲PHP文件使用Ajax:

  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: 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

瞭解更多here

+0

好的,那很好,但我再也看不到任何東西。是一個問題到那裏的jQuery的Ajax代碼? – gmaestro

+0

我這樣做,但現在當我運行PHP文件我得到:錯誤:SQLSTATE [42000]:語法錯誤或訪問衝突:1064您的SQL語法中有錯誤;檢查與您的MySQL服務器版本相對應的手冊,以便在第1行的':akt_djubrenje''附近使用正確的語法。 – gmaestro

+0

檢查我更新的答案,我在查詢中更改了引號,還有一個額外的雙引號 – Fabio

1

你需要從你的Ajax調用

 $('#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); 
       } 
      }); 
     }); 

數據傳遞到服務器腳本在服務器端,調用$_POST['akt_djubrenje']檢索從HTML文件傳遞的數據。

快樂編碼:)

+0

再次不工作:http://agroagro.com/aktivnosti1.html(點擊按鈕添加新的,然後標籤mehanizacija ...真的不知道什麼是問題 – gmaestro

+0

@gmaestro:數據'akt_djubrenje'在您的服務器腳本成功接收? – dreamweiver

+0

是的我認爲.....我必須調用函數tabela_djubrenje( );按鈕點擊或不按鈕的功能? – gmaestro