簡體   English   中英

使用 PDO 將數據提取到表中導致沒有價值

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

我有個問題。 我想將與用戶相關的所有任務提取到一個表中。 但是,結果是“0 結果”。 我確實嘗試了 echo print_r($allTask​​),結果是 Array() 1。我不知道是什么問題,因為在不完整的數據庫中,有幾個任務,但是當我獲取它時什么都沒有顯示。 有人可以向我解釋一下嗎? 感謝您的幫助

注意:我將 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';
  }

 }
  ?>

使用占位符值時,請絕對確保您沒有引入任何其他語法。 我可以看到owner=':owner'這是不正確的。 占位符周圍不應有引號。

這應該是:

'... owner=:owner'

PDO 負責轉義。 引號只是礙事。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM