PHP PDO - 从哪里选择(从输入字段获取值)

时间:2014-03-01 13:39:56

标签: php jquery ajax pdo

我想运行查询以使用php pdo从mysql获取数据。

查询必须如下: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">

我需要从ID_akt = $_POST['akt_djubrenje']

的mysql中获取数据

所以我写这个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;

我也用ajax调用php文件:

          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

更新: enter image description here

2 个答案:

答案 0 :(得分:2)

您需要在查询下移动execute()命令,您还需要使用通过查询创建的对象

$result = $conn->query("SELECT * FROM akt_djubrenje where ID_akt = :akt_djubrenje");
$result->execute(array(':akt_djubrenje' => $_POST['akt_djubrenje']));

来自文档

  
      
  • 调用PDOStatement :: bindParam()将PHP变量绑定到参数标记:绑定变量将其值作为输入传递,并接收其相关参数标记的输出值(如果有)
  •   
  • 或传递一组仅输入参数值
  •   

了解详情here

答案 1 :(得分: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文件传递的数据。

快乐编码:)