-1

我正在构建一个应该响应 jQuery Ajax 调用的 PHP 函数,具体取决于提交的 id。可以有多个具有相同 ID 的表单,我无法弄清楚为什么它没有发回多个记录。PS:如果我在星期一之前不解决这个问题,我爸爸会杀了我......

我的PHP:

<?php
  if (!empty($_GET['id'])) {
    $id = $_GET['id'];
    try {
      $objDb = new PDO('mysql:host=localhost;dbname=blankett', 'root', 'root');
      $objDb->exec('SET CHARACTER SET utf8');
      $sql = "SELECT * 
              FROM `forms`
              WHERE `id` = '$id'";
      $statement = $objDb->prepare($sql);
      $list = $statement->fetchAll(PDO::FETCH_ASSOC);
      if (!empty($list)) {
        $out = array();
        foreach ($list as $row ) {
          $out[] = '<tr><td><a href="'.$row['link_form'].'">'.$row['name_form'].'</a></td> <td>'.$row['date_added'].'</td></tr>';
        }
        echo json_encode(array('error' => false, 'list' => $out));
      } else {
        echo json_encode(array('error' => true));
      }
    } catch(PDOException $e) {
      echo json_encode(array('error' => true));
    }
  } else {
    echo json_encode(array('error' => true));
  }
?>
4

1 回答 1

1

您错误地使用了准备好的语句。首先,您不应该将变量放在语句中。其次,您需要执行该语句。

这是来自 php.net 站点的示例:

$sql = 'SELECT name, colour, calories
FROM fruit
WHERE calories < :calories AND colour = :colour';
$sth = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$sth->execute(array(':calories' => 150, ':colour' => 'red'));
$red = $sth->fetchAll();
$sth->execute(array(':calories' => 175, ':colour' => 'yellow'));
$yellow = $sth->fetchAll();

以下是你的做法:

$sql = "SELECT * 
FROM `forms`
WHERE `id` = :formID";

$statement = $objDb->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$statement ->execute(array(':formID' => $id));
$list= $statement ->fetchAll();
于 2013-03-24T01:11:32.567 回答