2013-03-21 62 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

相关问题