Job Sanjuan Job Sanjuan - 2 months ago 7
PHP Question

getting data on database based on specific months using select

I want to get data from db and show it to my table but it only shows blank rows and the number of row entries no information at all?

<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"/>




I think the problem is here please help

<?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);
}


done editing my table its working now but my edit button in which it should be pre-populated by data from data base is now showing blank textbox

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 tried echo $fname that textbox got an input of Notice: Undefined variable: 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>




I don't have any idea on what's happening now

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";
}
}

?>

Answer

You should get errors to display because your code is probably giving you undefined offset warnings and you're not seeing them.

As you'll notice from the mysqli_stmt::fetch() manual, this returns a boolean. You are then trying to use $row, which is a boolean as an array. Instead you need to use the variables you assigned previously in 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
}
Comments