2014-03-01 71 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