基于id返回数据

时间:2018-03-05 21:21:23

标签: php

我试图根据ID返回数据,但是,我遇到了一些问题,我已经通过手动输入一个ID来测试它,这将返回我期望的值,当我尝试这样做时,动态?我无法让它返回相关的ID,所以我试图在我的块开头添加唯一ID以查看是否有帮助,它似乎没有改变,我要去哪里错误?或者是否有一个我可以阅读更多的教程来帮助我这个? 状态

<?php 
require ("Database.php");
class Status
{
    private 
    $sessionId,
    $db;

    public function socialStatus()
    {
        $db = new Database;

        $query = "SELECT  f.followingId, s.postDate ,s.status, u.username, u.profileImgPath, u.username, s.statId 
        FROM followers AS f
        JOIN status AS s ON s.userId = f.userId 
        JOIN users AS u ON u.userId = :sessionId
        WHERE s.userId = :sessionId AND f.followingId = :sessionId ORDER BY postDate DESC";   
        $stmt = $db->prepare($query);  
        $stmt->bindValue(':sessionId',  $_SESSION["userId"]);    
        $stmt->execute();   
        return $stmt;
    }
}

回复

<?php 

class Reply
{
    private 
    $sid,
    $db;

    public function socialReply()
    {
        $db = new Database;
        $sid = (isset($_POST['sid']) ? $_GET['sid'] : null);
        $query = "SELECT s.statId, r.reply, r.dates, u.username
                  FROM replies AS r
                  JOIN status AS s ON s.statId = r.statId 
                  JOIN users AS u ON u.userId = r.userId
                  WHERE s.statId = :sid
                ";   
        $stmt = $db->prepare($query);  
        $stmt->bindValue(':sid',  $sid);    
        $stmt->execute();   
        return $stmt;
    }

}

输出

    <div class ="socialContent">
        <div class="socialShare">
            <form class="shareStyle" id="social">
                <ul class="socialImagesControl">
                    <li>
                        <label for="file-upload">
                            Add picture
                        </label>
                        <input type="file">  
                    </li>
                    <li>
                        <label for="file-upload">
                            Add Album
                        </label>
                        <input type="file">  
                    </li>
                </ul>
                <label>
            <textarea class="socialStart" placeholder="Share your news" name="messages"></textarea>
            <span class="error"></span>
            </label>
            <input type="submit" value="Enter" class="socialSubmit">
            </form>
        </div>
</div>

<div class ="socialStatus">
<?php

require ("classes/Status.php");
require ("classes/Reply.php");
$status = new Status;
$replys = new reply;
$results = $status->socialStatus(); 
foreach($results as $status){
  ?>
  <input type="text" name="sid" value="<?php print $status["statId"]; ?>">  
<div class="reports">
        <ul>
            <li class="reportRight"><input type='checkbox'  style='display: none'  class="reporting"  id=reportings><label for=reportings>&#x2026;</label>
            <div class="reportlist">
                <ul class="reportPosition">
                    <li>spam</li>
                    <li>abuse</li>
                </ul>   
            </div>        
             </li>
        </ul>
    </div>
    <figure>
        <a href="index.php"><img src="<?php print $status['profileImgPath'];?>"></a>

       <figcaption><a href="index.php"> <?php  print $status["username"]; ?></a></figcaption>
    </figure>
    <div class="usermessage">

   <pre><?php 
   print $status["status"]; ?></pre>
    <div class="times">Posted<?php  print " " . $status["postDate"]; ?></div>
    </div>

   <div class="answers">
   <div class ="socialStatusAnswers">
   <?php
    $answers = $replys->socialReply(); 
    foreach($answers as $ars){
    ?>
<div class="reports">
        <ul>
            <li class="reportRight"><input type='checkbox'  style='display: none'  class="reporting"  id=reportings><label for=reportings>&#x2026;</label>
            <div class="reportlist">
                <ul class="reportPosition">
                    <li>spam</li>
                    <li>abuse</li>
                </ul>   
            </div>        
             </li>
        </ul>
    </div>

    <figure>
       <a href=""><img src="assests/images/coffee.jpg" alt="Picture of a cup of coffee"></a>    
       <figcaption><a href=""><?php print $ars["username"]; ?></a></figcaption>
    </figure>
    <div class="usermessage">
        <?php print $ars["reply"]; ?>
    </div>
   </div>

   <div class="socialShare">
            <form class="shareStyle" id="replying">
                <ul class="socialImagesControl">
                    <li>
                        <label for="file-upload">
                            Add picture
                        </label>
                        <input type="file">  
                    </li>
                </ul>
    <?php 
    }
    ?>      
            <input type="text" name="sid" value="<?php print $status["statId"]; ?>">    
    <?php 
    }
    ?>
            <textarea class="socialStart" placeholder="Add a comment" name="addComment"></textarea>
            <input type="submit" value="Enter" class="socialSubmit">
            </form>
        </div>
</div>

</div>
</div>

2 个答案:

答案 0 :(得分:1)

据我所知,执行查询后实际上并没有获取任何行。假设您正在使用PDO尝试类似以下内容来返回查询中的所有行:

$stmt->execute();
$results = $stmt->fetchAll();

return $results;

否则,您将尝试迭代返回的PDO语句。

为PDO启用例外以帮助调试,并检查错误日志/显示错误。

$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

同时调整您的函数以接受并使用说id的参数。这将使您的代码更容易测试。

答案 1 :(得分:-1)

检查以下代码行:$sid = (isset($_POST['sid']) ? $_GET['sid'] : null);