【问题标题】:PHP PDO - select from where (get value from input field)PHP PDO - 从哪里选择(从输入字段中获取值)
【发布时间】:2014-03-01 13:39:55
【问题描述】:

我想使用 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">

我需要如何从 mysql where ID_akt = $_POST['akt_djubrenje'] 获取数据

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

更新:

【问题讨论】:

    标签: php jquery ajax pdo


    【解决方案1】:

    您需要在查询下移动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

    【讨论】:

    • 好的,没关系,但我还是看不到任何东西。那里的jquery ajax代码有问题吗?
    • 我这样做了,但是现在当我运行 php 文件时,我得到: 错误:SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 ':akt_djubrenje"' 附近使用正确的语法
    • 检查我更新的答案,我更改了查询中的引号,你有一个额外的双引号
    • 好的,可以说 php pdo 文件现在很好,但我又看不到表:agroagro.com/aktivnosti1.html(单击添加新按钮,然后单击选项卡 mehanizacija)...
    • 这简直是绝妙的“答案”和绝妙的编辑。以及精彩的投票
    【解决方案2】:

    您需要通过 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文件传递过来的数据。

    快乐编码:)

    【讨论】:

    • 再次不工作:agroagro.com/aktivnosti1.html(点击按钮添加新,然后标签 mehanizacija ...真的不知道是什么问题
    • @gmaestro: 数据akt_djubrenje在你的服务器脚本接收成功了吗?
    • 是的,我想.....我必须调用函数 tabela_djubrenje ();是否在按钮单击时起作用?
    • @gmaestro: 是的,你需要调用持有上述ajax调用的函数。
    • 我用: $( "#otvori" ).click(function() { tabela_djubrenje(); });但我还是什么都看不到……现在有什么问题?
    猜你喜欢
    • 2020-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-23
    相关资源
    最近更新 更多