PDO查询仅在第一次运行

时间:2017-06-03 20:04:35

标签: php html pdo

我试图从数据库获取信息,但查询仅针对第一行运行! 我尝试过很多东西,但它没有用;我不知道为什么。 get方法在表上,由数据库

填充

问题出在第一个查询'SELECT CouID FROM CoursesC WHERE StuID = :ID ' 如果我将查询更改为'SELECT CouID FROM CoursesC',它可以正常工作!

我将数据呈现在模态<{p>中的<select>

表格代码

<form action="<?php $_SERVER['PHP_SELF']?>" method="GET" id="contact">

<table  id="studenttable">

  <tr>
  <th></th>
  <th>Student ID</th>
  <th>Student Name</th>
  <th>Student Passed Credits</th>
    <th>Action</th>
  </tr>
  <?php

    require('check.php');
$query = $conn->prepare('SELECT * FROM student WHERE AdvID=:id' );
$query->bindParam(":id",$_SESSION['userid']);
$query->execute();
$row = $query->fetch();

while($row = $query->fetch(PDO::FETCH_ASSOC)){
 ?>
  <tr>
  <td> <input type ="checkbox" name="checked[]" value="<?php echo $row['StuId'];?>"> </td>
  <td><?php echo $row['StuId']; ?></td>
  <td><?php echo $row['Name']; ?></td>
  <td> </td>
  <td> 

  <button  type="submit" name="submit" value="<?php echo $row['StuId'];?>" class="btn icon-btn btn-info" ><span class="glyphicon glyphicon-info-sign"></span>More Information</button>
  </td>
  </tr>    
<?php
}
      do {
    while ($query->fetch())
        ;
    if (!$query->nextRowset())
        break;
} while (true);
    $conn=null;
    $query->closeCursor();
?> 

</table>

</form>




<?php
if (isset($_GET['submit'])){
    global $CC;
    $count=1;
    global $data;
    $T=$_GET['submit'];
?>
<script>
    $('#CC').html("");
    $('#CC1').html("");
</script>
<?php
    require('check.php');
    $q= $conn->prepare('SELECT CouID FROM CoursesC WHERE StuID = :ID ');
    $q->bindValue(':ID', $_GET['submit'] );
    $q->execute();
    $data = $q->fetch();
    while($data = $q->fetch(PDO::FETCH_ASSOC)){
        $CC=$data['CouID'];
?>
<script>
    $('#CC').append("<option><? echo $count++; echo "- "; echo $CC;?></option>");
</script>
<?php
    }
    $q->closeCursor();
    $A=$CC;
    /*echo "<script> alert('<?php $T ?>')";*/

    $q1= $conn->prepare('SELECT CouID FROM Courses');
    $q1->execute();
    $data1 = $q1->fetch();
    while($data1 = $q1->fetch(PDO::FETCH_ASSOC)){
        if($CC!=$data1['CouID']){
?>
<script>
    $('#CC1').append("<option><? echo $count++; echo "- "; echo $data1['CouID'];?></option>");
</script>
<?php
        }
    }
    $q1->closeCursor();
?>
<script>
    $(window).load(function(){
        $('#myModal').modal('show');
    });
</script>
<?
}
?>
<?php
    if (isset($_GET['submit'])){
        global $CC;
        $count=1;
        global $data;
        $T=$_GET['submit'];
?>
<script>
     $('#CC').html("");
     $('#CC1').html("");
</script>
<?php
        require('check.php');
        $q= $conn->prepare('SELECT CouID FROM CoursesC WHERE StuID = :ID ');
        $q->bindValue(':ID', $_GET['submit'] );
        $q->execute();
        $data = $q->fetch();
        while($data = $q->fetch(PDO::FETCH_ASSOC)){
            $CC=$data['CouID'];
?>
<script>
     $('#CC').append("<option><? echo $count++; echo "- "; echo $CC;?></option>");
</script>
<?php
        }
        $q->closeCursor();
        $A=$CC;
        /*echo "<script> alert('<?php $T ?>')";*/

        $q1= $conn->prepare('SELECT CouID FROM Courses');
        $q1->bindParam(":id", $_GET['submit']);
        $q1->execute();
        $data1 = $q1->fetch();
        while($data1 = $q1->fetch(PDO::FETCH_ASSOC)){
            if($CC!=$data1['CouID']){
?>
<script>
     $('#CC1').append("<option><? echo $count++; echo "- "; echo $data1['CouID'];?></option>");
</script>
<?php
            }
        }
        $q1->closeCursor();
?>
<script>
    $(window).load(function(){
        $('#myModal').modal('show');
    });
</script>
<?
    }
?>
<?php
    if (isset($_GET['submit'])){
      global $CC;
        $count=1;
        global $data;
        $T=$_GET['submit'];
    ?>
    <script>
     $('#CC').html("");
     $('#CC1').html("");
    </script>
    <?php
    require('check.php');
    $q= $conn->prepare('SELECT CouID FROM CoursesC WHERE StuID = :ID ');
    $q->bindValue(':ID', $_GET['submit'] );
    $q->execute();
    $data = $q->fetch();
    while($data = $q->fetch(PDO::FETCH_ASSOC)){
        $CC=$data['CouID'];
?>
<script>
     $('#CC').append("<option><? echo $count++; echo "- "; echo $CC;?></option>");
</script>
<?php
    }
    $q->closeCursor();
    $A=$CC;
    /*echo "<script> alert('<?php $T ?>')";*/

    $q1= $conn->prepare('SELECT CouID FROM Courses');
    $q1->bindParam(":id", $_GET['submit']);
    $q1->execute();
    $data1 = $q1->fetch();
    while($data1 = $q1->fetch(PDO::FETCH_ASSOC)){
        if($CC!=$data1['CouID']){
?>
<script>
     $('#CC1').append("<option><? echo $count++; echo "- "; echo $data1['CouID'];?></option>");
</script>
<?php
        }
    }
    $q1->closeCursor();
?>
<script>
    $(window).load(function(){
        $('#myModal').modal('show');
    });
</script>
<?
}
?>

1 个答案:

答案 0 :(得分:0)

尝试更改此行:

$q= $conn->prepare('SELECT CouID FROM CoursesC WHERE StuID = :ID ');
$q->bindValue(':ID', $_GET['submit'] );

$q= $conn->prepare('SELECT CouID FROM CoursesC WHERE StuID IN( :ID ) ');
$q->bindValue(':ID', implode(",",$_GET['checked']) );

您的表单会返回一系列ID,而不仅仅是一个,因此您必须使用&#39; IN&#39;而不是&#39; =&#39;。

另外,请勿使用<?php $_SERVER['PHP_SELF']?>。使用页面名称,以防您在项目外部进行部署!