简体   繁体   English

使用 PDO 将数据提取到表中导致没有价值

[英]Fetch data into table using PDO result in no value

I have a problem.我有个问题。 I want to fetch all the tasks related to the user into a table.我想将与用户相关的所有任务提取到一个表中。 However, the result is '0 results'.但是,结果是“0 结果”。 I did try to echo print_r($allTask), and the result is Array() 1. I don't know what is the problem because, in the incomplete database, there are several tasks, but nothing shows up when I fetch it.我确实尝试了 echo print_r($allTask​​),结果是 Array() 1。我不知道是什么问题,因为在不完整的数据库中,有几个任务,但是当我获取它时什么都没有显示。 Can someone explain it to me?有人可以向我解释一下吗? Thank you for your help感谢您的帮助

Note: I put the fetch query inside the if(isset($_POST['submit']) because I want every time the user adds the new task and click submit. The incomplete database will be updated as well to display the new task added to the table注意:我将 fetch 查询放在 if(isset($_POST['submit']) 中,因为我希望每次用户添加新任务并单击提交时。不完整的数据库也将更新以显示添加的新任务到桌子上

<?php
session_start();
require 'connect.php';
if (isset($_POST['submit'])) {
$owner = $_SESSION['name'];
$title=$_POST['title'];
$description=$_POST['description'];
$due_date=$_POST['due_date'];
$time=$_POST['time'];
$state=0;
$insertQuery="INSERT INTO incomplete (owner, title, description, due_date, time, state)
VALUES (:owner, :title, :description, :due_date, :time, :state)";
$preparedInsertStatement = $conn->prepare($insertQuery);
$preparedInsertStatement->bindValue(':owner', $owner);
$preparedInsertStatement->bindValue(':title', $title);
$preparedInsertStatement->bindValue(':description', $description);
$preparedInsertStatement->bindValue(':due_date', $due_date);
$preparedInsertStatement->bindValue(':time', $time);
$preparedInsertStatement->bindValue(':state', $state);
$valueInsert=$preparedInsertStatement->execute();
if($valueInsert){
    echo 'Insert is done';
}
else{
    echo 'Insert is not successful';
} 
$displayQuery="SELECT * FROM incomplete where owner=':owner'";
$displayTask= $conn->prepare($displayQuery);
$displayTask->bindValue(':owner', $owner);
$displayTask->execute();
$allTask=$displayTask->fetchAll();
echo print_r($allTask);
if(count($allTask) > 0)
{
  echo "<table border=\"1\"><tr><th>ID</th><th>Title</th><th>Description</th><th>Due 
Date</th><th>Time</th></tr>";
foreach ($allTask as $row) {
    echo "<tr><td>".$row["id"]."</td><td>".$row["title"]."</td><td>".$row["description"]."</td><td>".$row["due_date"]."</td><td>".$row["time"]."</td></tr>";
}

  }else{
      echo '0 results';
  }

 }
  ?>

When using placeholder values be absolutely sure you haven't introduced any additional syntax.使用占位符值时,请绝对确保您没有引入任何其他语法。 I can see owner=':owner' which is incorrect.我可以看到owner=':owner'这是不正确的。 The placeholder should not have quotes around it.占位符周围不应有引号。

This should be:这应该是:

'... owner=:owner'

PDO takes care of escaping. PDO 负责转义。 The quotes are just in the way.引号只是碍事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM