2016-07-02 11 views
1

私はアプリの検索フォームを作成しましたが、倍数になるはずの時点で結果を取り戻しています。私はこれが何かばかげていると確信して、誰かが私が間違ってやっていることを教えてくれるかなと思っていました。ここでPDO MySQLクエリは1つの結果セットを返すだけです

は完全なコードです:一目で

<?php 

// php search data in mysql database using PDO 
// set data in input text 

$TaskId = ""; 
$ClientId=""; 
$TaskName = ""; 
$TaskDescription = ""; 
$TaskStartAt = ""; 


if(isset($_POST['Find'])) 
{ 
    // connect to mysql 
try { 
    $pdoConnect = new PDO("mysql:host=localhost;dbname=tt","root","root"); 
} catch (PDOException $exc) { 
    echo $exc->getMessage(); 
    exit(); 
} 

// id to search 
//$TaskId = $_POST['TaskId']; 
$ClientId = $_POST['ClientId']; 
// date to search 
//$DateCreated = $_POST['DateCreated']; 

// mysql search query 
$pdoQuery = "SELECT * 
FROM tasks t 
left join users u using (UserId) 
left join clients cl using (ClientId) 
WHERE t.isdeleted = 0 and ClientId = :ClientId"; 

$pdoResult = $pdoConnect->prepare($pdoQuery); 

//set your id to the query id 

$pdoExec = $pdoResult->execute(array(":ClientId"=>$ClientId)); 


if($pdoExec) 
{ 
     // if id exist 
     // show data in inputs 
    if($pdoResult->rowCount()>0) 
    { 
     echo '<table>'; 
     foreach 
     ($pdoResult as $rows) 
     { 
      //$TaskId = $row['TaskId']; 
      $ClientId = $rows['ClientId']; 
      // $TaskName = $row['TaskName']; 
      // $TaskDescription = $row['TaskDescription']; 
     } 
     echo '</table>'; 
    } 
     // if the id not exist 
     // show a message and clear inputs 

    }else{ 
    echo 'ERROR Data Not Inserted'; 
    } 
} 


?> 


<!DOCTYPE html> 
<html> 
<head> 
    <title>Task Tracker</title> 
    <link rel="stylesheet" href="css/table.css" type="text/css" /> 

<link rel="stylesheet" href="assets/demo.css"> 
<link rel="stylesheet" href="assets/header-fixed.css"> 
<link href='http://fonts.googleapis.com/css?family=Cookie' rel='stylesheet' type='text/css'> 
<script type="text/javascript"> 
//Display the Month Date and Time on login. 
function display_c(){ 
var refresh=1000; // Refresh rate in milli seconds 
mytime=setTimeout('display_ct()',refresh) 
} 

function display_ct() { 
var strcount 
var x = new Date() 
document.getElementById('ct').innerHTML = x; 
tt=display_c(); 
} 
</script> 

</head> 
<body> 

<header class="header-fixed"> 
<div class="header-limiter"> 

    <h1><a href="#">Task Tracker</a></h1> 




    <nav> 
     <a href="dashboard.php" class =>Dashboard</a> 
     <a href="addtask.php" class=>Task Management</a> 
    <a href="configuration.php" class =>Configuration</a> 
    <a href="logout.php" class =>Logout</a> 
    <a href="search.php" class ="selected">Reports & Analytics</a> 

    </nav> 
    </nav> 

    </div> 
    </header> 

    <title> Query a task</title> 

    <meta charset="UTF-8"> 

    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 

    </head> 

    <form action="search.php" method="post"> 

     <!-- Enter a Task Id : <input type="text" name="TaskId" value=""> <br><br> --> 
     Enter a Client Id : <input type="text" name="ClientId" value="<?php echo $ClientId;?>"><br><br> 



     <input type="submit" name="Find" value="Find Data"> 

     <br> </br> 


     <table border="0"> 
     <tr COLSPAN=2 BGCOLOR="lightblue"> 
     <td>Id</td> 
     <td>Client</td> 
     <td>Task Name</td> 
     <td>Task Description</td> 
     <td>Hours</td> 
     <td>Date Created</td> 
     <td>Who Completed Task</td> 
    </tr> 
    <?php  
    { 

    if($pdoResult->rowCount()>0) 

    { 
    echo "<tr>". 
     "<td>".$rows["TaskId"]."</td>". 
     "<td>".$rows["ClientName"]."</td>". 
     "<td>".$rows["TaskName"]."</td>". 
     "<td>".$rows["TaskDescription"]."</td>". 
     "<td>".$rows["Hours"]."</td>". 
     "<td>".$rows["DateCreated"]."</td>". 
     "<td>".$rows["UserName"]."</td>". 
     "</tr>"; 
    } 

    else{ 
     echo 'No data associated with this Id'; 
    } 

} 
?> 

</table> 
    </form> 

</body> 

</html> 
+0

結果セットに対してフェッチが実行されていますか? – frz3993

+0

@ frz3993はい、クライアントIDのエントリに対して複数の結果がある場合でも問題ありません – user130045

+1

あなたのコードに 'fetch()'や 'fetchAll()'は表示されません。 – frz3993

答えて

4

、あなたがあまりにも多くのあなたの機能を分割したことだけであるように思われます。

ページの上部で、データベース接続を確立し、結果セットを取得します。次に、PDO Statementオブジェクトを通じて要素foreachをエコーし​​、現在の行の内容を変数$rowsに割り当てます。注:現在の行の内容

さらに下のページに、あなたecho個々のフィールド、あなたがこの外のごforeachループを行う - ブタ$rows['field']を使用。 $rowsは、ループがループするたびに再生成されるため、ループの完了後に変数を破棄しないため、結果セットの最後の行を含む変数で最後が終了します。

個々の行の内容を実際に印刷する場所を、文オブジェクトを反復してフィールドを取得するループを配置する必要があります。一方、ユーザーの入力が全く入力されていない場合にのみ、これをすべて実行する必要があるため、最初の条件の正の分岐の内側にある必要があります。$_POST['Find']が設定されているかどうかをチェックします。以下のバージョン。

変数$resultsを空の文字列に割り当てることから始めます。これは、ユーザーがフォームをまったく送信しなかった場合に出力する値です。 $_POST['Find']が空でない場合、データベースを検索し、結果セットを反復し、このループでHTML文字列を作成し、その結果を$results変数に格納します。行が返されないか、または​​呼び出しが完全に失敗した場合は、例外ハンドラによって処理される例外がスローされます(プロジェクト全体に対して中央レベルで定義する必要があります)、表示される汎用エラーメッセージを渡しますユーザに提供する。

私はまた、関連ビットをより明確にするために余分なものと多くのコメントを剥ぎ取り、それを明確にそれがループ内人口だから、それは行が含まれていることを確認するために$row$rows変数の名前を変更しました。注すべてではありません。

<?php 

// Set the global exception handler—should of course be done 
// in a global boilerplate file, rather than for each file 
set_exception_handler('your_exception_handler_here'); 
$results = ""; 

if(!empty($_POST['Find'])) 
{ 
    $pdoConnect = new PDO("mysql:host=localhost;dbname=tt","root","root"); 
    $pdoConnect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    $ClientId = $_POST['ClientId']; 

    $pdoQuery = "SELECT * 
    FROM tasks t 
    left join users u using (UserId) 
    left join clients cl using (ClientId) 
    WHERE t.isdeleted = 0 and ClientId = :ClientId"; 

    $pdoResult = $pdoConnect->prepare($pdoQuery); 
    $pdoExec = $pdoResult->execute(array(":ClientId"=>$ClientId)); 

    if($pdoResult->rowCount()>0) 
    { 
     $results = '<table border="0"> 
      <tr COLSPAN=2 BGCOLOR="lightblue"> 
       <td>Id</td> 
       <td>Client</td> 
       <td>Task Name</td> 
       <td>Task Description</td> 
       <td>Hours</td> 
       <td>Date Created</td> 
       <td>Who Completed Task</td> 
      </tr>'; 

     foreach ($pdoResult as $row) 
     { 
      $ClientId = $row['ClientId']; 
      $results .= "<tr>". 
       "<td>".$row["TaskId"]."</td>". 
       "<td>".$row["ClientName"]."</td>". 
       "<td>".$row["TaskName"]."</td>". 
       "<td>".$row["TaskDescription"]."</td>". 
       "<td>".$row["Hours"]."</td>". 
       "<td>".$row["DateCreated"]."</td>". 
       "<td>".$row["UserName"]."</td>". 
       "</tr>"; 
     } 
      $results .= "</table>"; 
    } else { 
     $return = '<span class="error_message">No data associated with this Id</span>'); 
    } 
} 
?> 


<!DOCTYPE html> 
<html> 
<head> 
    <title>Task Tracker</title> 
</head> 

<body> 
    <title>Query a task</title> 
    <form action="search.php" method="post"> 
     Enter a Client Id : <input type="text" name="ClientId" value="<?php echo $ClientId;?>"><br><br> 
     <input type="submit" name="Find" value="Find Data"> 
    </form> 

    <?php 
     echo $results; 
    ?> 

</body> 
</html> 
+0

一般的には良い答えですが、OPのロジックが不足しています。ネストされた状態は意味をなさないので、「入力データを表示する」というコメントが間違って配置されています –

+0

@YourCommonSense私はあなたにあまりよく従っていません。ネストされた条件は、私が見る限り完全な意味を持ちます。クエリの実行が成功したかどうかを確認し、返された行があるかどうかを確認します。何か不足していますか? 「データを入力に表示する」というのはあまり意味がありませんが、それは問題ではありませんでした。データは両方の表の行に表示されます。 –

+0

この状態が出力するエラーメッセージには説明がありません。どちらの方法でも、$ pdoExec変数をチェックすることは余計であり、避けなければなりません。 –

関連する問題