使用select获取基于特定月份的数据库数据


getting data on database based on specific months using select

我想从db中获取数据并将其显示到我的表中,但它只显示空白行和行条目的数量,没有任何信息?

 <form method="post" action="index.php">
<select name="month" id="month">
    <option></option>
    <option value="1">January</option>
    <option value="2">February</option>
    <option value="3">March</option>
    <option value="4">April</option>
    <option value="5">May</option>
    <option value="6">June</option>
    <option value="7">July</option>
    <option value="8">August</option>
    <option value="9">September</option>
    <option value="10">October</option>
    <option value="11">November</option>
    <option value="12">December</option>
</select>
<input type="submit" value="submit" name="submit"/>

我想问题就在这里,请帮忙。

   <?php
 $hostname="localhost";
  $username="root";
 $password="";
   $dbname="testproduct";

$db = new mysqli($hostname, $username, $password, $dbname);

$stmt = $db->stmt_init();
//
    if($stmt->prepare("SELECT fname,lname,checkin,checkout,rrate,reservefee,datepaid,modepayment,stats FROM tblguest WHERE MONTH(checkin)//checkin name of the row from database = ?")) {
    //
if(!$stmt->bind_param('s', $month)) {
    //if BIND fails, display an error
    printf("Errormessage: %s'n", $stmt->error);
}

$month = isset($_POST['month'])
      ? $db->real_escape_string($_POST['month'])//this 'month' is the select name
      : '';

if(!$stmt->execute()) {
    printf("Errormessage: %s'n", $stmt->error);
}

 if(!$stmt->bind_result($fname,$lname,$checkin,$checkout,$rrate,$reservefee,$datepaid,$modepayment,$stats))//name of db rows and also the only information I want to show on the web {
    printf("Errormessage: %s'n", $stmt->error);
}

完成编辑我的表,它现在工作,但我的编辑按钮中,它应该预填充数据从数据库现在显示空白文本框

 while($stmt->fetch()) {
    ?>
        <tr>
        <td><?php echo $fname; ?></td>
        <td><?php echo $lname; ?></td>
        <td><?php echo $checkin; ?></td>
        <td><?php echo $checkout; ?></td>
        <td><?php echo $rrate; ?></td>
        <td><?php echo $reservefee; ?></td>
        <td><?php echo $datepaid; ?></td>
        <td><?php echo $modepayment; ?></td>
        <td><?php echo $stats; ?></td>
        <td align="center">
        <a id="<?php echo $fname.$lname ?>" class="edit-link" href="#" title="Edit">
        <img src="edit.png" width="20px" />
        </a></td>
        <td align="center"><a id="<?php echo $fname.$lname ?> "class="delete-link" href="#" title="Delete">
        <img src="delete.png" width="20px" />
        </a></td>
        </tr>
        <?php
}
}
    ?>
    </tbody>
    </table>
    </div>
</div>

edit_form.php尝试echo $fname,文本框得到一个输入通知:未定义变量:fname

  <?php
 include_once 'dbconfig.php';
  if($_GET['edit_id'])
{
  $id = $_GET['edit_id'];   
  $stmt=$db_con->prepare("SELECT * FROM tblguest WHERE id=:id");
  $stmt->execute(array(':id'=>$id));    
  $row=$stmt->fetch();
 }
 ?>
 <style type="text/css">
  #dis{
    display:none;
  }
  </style>


  <div id="dis">
  </div>

   <form method='post' id='emp-UpdateForm' action='#'>
     <table class='table table-bordered'>
     <input type='hidden' name='id' value='<?php echo $id; ?>' />
     <tr>
         <td>First Name</td>
         <td><input type='text' name='fname' class='form-control' value='<?        php echo $fname; ?>' required></td>
       </tr>
        <tr>
         <td>Last Name</td>
          <td><input type='text' name='lname' class='form-control' value='<?php echo $row['lname']; ?>' required></td>
     </tr>
    <tr>
        <td>Contact Number</td>
        <td><input type='text' name='contactnum' class='form-control' value='<?php echo $row['contactnum']; ?>' required></td>
    </tr>
      <tr>
        <td>Email</td>
        <td><input type='text' name='email' class='form-control' value='<?php echo $row['email']; ?>' required></td>
    </tr>
    <tr>
        <td>Check In Date</td>
        <td><input type='date' name='checkin' class='form-control' value='<?php echo $row['checkin']; ?>' required></td>
    </tr>
    <tr>
        <td>Check Out Date</td>
        <td><input type='date' name='checkout' class='form-control' value='<?php echo $row['checkout']; ?>' required></td>
    </tr>
        <tr>
        <td>Room Rate</td>
        <td><input type='text' name='rrate' class='form-control' value='<?php echo $row['rrate']; ?>' required /></td>
    </tr> 
    <tr>
        <td>Reservation Fee</td>
        <td><input type='text' name='reservefee' class='form-control' value='<?php echo $row['reservefee']; ?>' required /></td>
    </tr> 
    <tr>
        <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;a.Date of Payment</td>
        <td><input type='date' name='datepaid' class='form-control'  value='<?php echo $row['datepaid']; ?>' required /></td>
    </tr> 
    <tr>
        <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;b.Mode of Payment</td>
        <td><select  name='modepayment' class='form-control' value='<?php echo $row['modepayment']; ?>'  required >
        <option value="Cash">Cash</option>
            <option value="Bank Deposit">Bank Deposit</option> 
            </select></td>
    </tr> 
    <tr>
        <td>Balance</td>
        <td><input type='text' name='balance' class='form-control'  value='<?php echo $row['balance']; ?>' required /></td>
    </tr> 
     <tr>
        <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;a.Date of Payment</td>
        <td><input type='date' name='balpayment' class='form-control'   value='<?php echo $row['balpayment']; ?>' required /></td>
    </tr> 
    <tr>
        <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;b.Mode of Payment</td>
        <td><select  name='balmodepay' class='form-control' value='<?php echo $row['balmodepay']; ?>' required >
        <option value="Cash">Cash</option>
            <option value="Bank Deposit">Bank Deposit</option> 
            </select></td>
    </tr> 
     <tr>
        <td>Status</td>
        <td><select  name='stats' class='form-control' value='<?php echo $row['stats']; ?>' required >
        <option value="Reserved">Reserved</option>
            <option value="Check In">Check In</option>
            <option value="Check Out">Check Out</option>
            <option value="Canceled">Canceled</option>  
            <option value="Done">Done</option>
            </select></td>
    </tr> 
    <tr>
        <td colspan="2">
        <button type="submit" class="btn btn-primary" name="btn-update" id="btn-update">
        <span class="glyphicon glyphicon-plus"></span> Save Updates
        </button>
        </td>
    </tr>
</table>

我不知道现在发生了什么

update.php

    <?php
 require_once 'dbconfig.php';

if($_POST)
{
    $id = $_POST['id'];
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $contactnum = $_POST['contactnum'];
    $email = $_POST['email'];
    $checkin = $_POST['checkin'];
    $checkout = $_POST['checkout'];
    $rrate = $_POST['rrate'];
    $reservefee = $_POST['reservefee'];
    $datepaid = $_POST['datepaid'];
    $modepayment = $_POST['modepayment'];
    $balance = $_POST['balance'];
    $balpayment = $_POST['balpayment'];
    $balmodepay = $_POST['balmodepay'];
    $stats = $_POST['stats'];
    $stmt = $db_con->prepare("UPDATE tblguest SET fname=:fn, lname=:ln, contactnum=:cn, email=:em, checkin=:ci, checkout=:co, rrate=:rr, reservefee=:rf, datepaid=:dp, modepayment=:mp, balance=:bl, balpayment=:bp, balmodepay=:bm, stats=:st  WHERE id=:id");
    $stmt->bindParam(":fn", $fname);
    $stmt->bindParam(":ln", $lname);
    $stmt->bindParam(":cn", $contactnum);
    $stmt->bindParam(":em", $email);
    $stmt->bindParam(":ci", $checkin);
    $stmt->bindParam(":co", $checkout);
    $stmt->bindParam(":rr", $rrate);
    $stmt->bindParam(":rf", $reservefee);
    $stmt->bindParam(":dp", $datepaid);
    $stmt->bindParam(":mp", $modepayment);
    $stmt->bindParam(":bl", $balance);
    $stmt->bindParam(":bp", $balpayment);
    $stmt->bindParam(":bm", $balmodepay);
    $stmt->bindParam(":st", $stats);
    $stmt->bindParam(":id", $id);
    if($stmt->execute())
    {
        echo "Successfully updated";
    }
    else{
        echo "Query Problem";
    }
}
  ?>

你应该得到错误显示,因为你的代码可能会给你未定义的偏移警告,而你没有看到它们。

您将从mysqli_stmt::fetch()手册中注意到,这返回一个布尔值。然后尝试使用$row,它是一个布尔数组。相反,您需要使用之前在mysqli_stmt::bind_result():

中分配的变量。
if(!$stmt->bind_result($fname,$lname,$checkin,$checkout,$rrate,$reservefee,$datepaid,$modepayment,$stats)) {
    printf("Errormessage: %s'n", $stmt->error);
}
while($stmt->fetch()) {
    ?>
        <tr>
        <td><?php echo $fname; ?></td>
        <td><?php echo $lname; ?></td>
        <td><?php echo $checkin; ?></td>
        <td><?php echo $checkout; ?></td>
        <td><?php echo $rrate; ?></td>
        <td><?php echo $reservefee; ?></td>
        <td><?php echo $datepaid; ?></td>
        <td><?php echo $modepayment; ?></td>
        <td><?php echo $stats; ?></td>
        <td align="center">
        <a id="<?php echo $fname." ".$lname; ?>" class="edit-link" href="#" title="Edit">
        <img src="edit.png" width="20px" />
        </a></td>
        <td align="center"><a id="<?php echo $fname." ".$lname; ?>" class="delete-link" href="#" title="Delete">
        <img src="delete.png" width="20px" />
        </a></td>
        </tr>
    <?php
}