2013-03-21 65 views
-1

這是我的php代碼。如何從數據庫檢索信息並在表格中顯示?

<?php 
session_start(); 
foreach($_POST AS $key => $val) { 
$_SESSION[$key]=$val; 
} 

mysql_connect('localhost', 'bikec_user', '[email protected]'); 
mysql_select_db('bikecats_database'); 

$cnetid=$_POST['cnetid']; 
$cpassword=$_POST['cpassword']; 


$cnetid = stripslashes($cnetid); 
$cpassword = stripslashes($cpassword); 
$cnetid = mysql_real_escape_string($cnetid); 
$cpassword = mysql_real_escape_string($cpassword); 

$sql="SELECT RentalID, BikeID, RentalStartDate, RentalEndDate 
    FROM rental 
    WHERE CustTxStateNetID = '$cnetid'"; 
$result=mysql_query($sql) OR die(mysql_error()); 
$row=mysql_fetch_assoc($result); 

$rentalid=$row['RentalID']; 
$bikeid=$row['BikeID']; 
?> 

這是html代碼。它應該是顯示從數據庫中檢索到的查詢,但由於某種原因,當我運行它時,表格變爲空白。我知道我只是迴應兩個變數,但即使這些變數是空的。

 <div class="span9"> 
      <h2>My Account</h2> 
      <p><strong>My Rentals</strong></p> 
     <table class="table table-striped"> 
      <thead> 
       <tr> 
       <th>Rental ID</th> 
       <th>Bike ID 
       <th>Check-Out Date</th> 
       <th>Return Date</th> 
       </tr> 
      </thead> 
      <tbody> 
       <tr> 
       <td><?php echo $rentalid ?></td>  
       <td><?php echo $bikeid ?></td> 
       <td></td> 
       <td></td> 
       </tr> 
       <tr> 
       <td></td> 
       <td></td> 
       <td></td> 
       <td></td> 
       </tr> 
      </tbody> 
     </table><br> 
     </div><!-- end span --> 
+0

使用[tag:PDO]進行連接。 'mysql_ *'已被棄用。 – hjpotter92 2013-03-21 01:02:41

+0

嘗試var_dump($ row)查看它返回的內容..也使用PDO – 2013-03-21 01:03:19

+0

您需要確認sql是否首先返回結果。 – Jesse 2013-03-27 23:20:14

回答

0

試試這個:

--------------------------- EDITED -------- ---------------------------------

<?php 
    session_start(); 



    if(isset($_POST['cnetid'])){ 
    mysql_connect('localhost', 'bikec_user', '[email protected]'); 
    mysql_select_db('bikecats_database'); 
    $cnetid = mysql_real_escape_string($_POST['cnetid']); 
    $cpassword = mysql_real_escape_string($_POST['cpassword']); 
    $table = "<table class='table table-striped' > 
       <thead> 
       <tr> 
        <th>Rental ID</th> 
        <th>Bike ID 
        <th>Check-Out Date</th> 
        <th>Return Date</th> 
       </tr> 
       </thead> 
       <tbody> 
       "; 
    $sql="SELECT RentalID, BikeID, RentalStartDate, RentalEndDate 
    FROM rental 
    WHERE CustTxStateNetID = '$cnetid'"; 
    $body = ""; 
    $result=mysql_query($sql) OR die(mysql_error()); 
    while ($row=mysql_fetch_assoc($result)) 
    { 
    $body = $body." <tr><td>".$row['RentalID']."</td> 
        <td>".$row['BikeID']."</td> 
        <td>".$row['RentalStartDate']."</td> 
        <td>".$row['RentalEndDate']."</td></tr> "; 
    } 
    $table = $table.$body." 
       <tr> 
        <td></td> 
        <td></td> 
        <td></td> 
        <td></td> 
       </tr> 
       </tbody> 
      </table>" ; 

    } 
    else 
    { 
    $table = "No data.."; 
    } 

    ?> 


    <div class="span9"> 
     <h2>My Account</h2> 
     <p><strong>My Rentals</strong></p> 
     <?php echo $table; ?> 
     <br> 
    </div> 

PS:發現,更正和代碼測試錯誤xD

Saludos;)

+0

我嘗試了新的代碼,但我的PHP代碼得到一個錯誤:($行= mysql_fetch_assoc($結果)){ $ 身體= $體「​​$行[ 'RentalID']>。 ​​$行[ 'BikeID'] ​​$行[ 'RentalStartDate'] ​​$行[ 'RentalEndDate']「; } syntax error,unexpected T_ENCAPSED_AND_WHITESPACE,expect T_STRING or T_VARIABLE or T_NUM_STRING in – user2193249 2013-03-21 02:17:50

+0

給我行號錯誤... – Hackerman 2013-03-21 11:54:07

+0

我發現錯誤....代碼在我的本地主機上工作...答覆編輯... saludos – Hackerman 2013-03-21 12:00:29

相關問題