how to query a value and add it to the date - php

I have a problem, you see I borrow a book getting the book_id and the date borrow is set to the date today, but each book has a days_id value that I need to add to the day of the borrow_date now I'm having trouble how can I get that date_id from the book_id I chosen and add it inside my borrow_date so that I can generate my due_date automatically
here is my PHP code
<?php
include 'connect.php';
$librarian_id = $_POST['librarian_id'];
$member_id = $_POST['member_id'];
$book_id = $_POST['book_id'];
$date_borrow = $_POST['date_borrow'];
$status_id = $_POST['status_id'];
$days = "SELECT days_id, book_id FROM book_setup
WHERE book_id = '$book_id' AND days_id = " . $row['book_id'] . " ";
$result = mysqli_query($conn, $days);
$due_date = date('Y-m-d', strtotime($date_borrow. ' + $result days'));
if ($librarian_id == '') {
echo "librarian is empty";
}
else if ($member_id == '') {
echo "member id is empty";
}
else if ($book_id == '') {
echo "book is empty";
}
else if ($date_borrow == '') {
echo "date borrow is empty";
}
else if ($status_id == '') {
echo "status is empty";
}
else if ($due_date == '') {
echo "due date is empty";
}
else {
$sql = " INSERT INTO borrow_book (librarian_id, member_id,
book_id, date_borrow, due_date, status_id)
VALUES
('$librarian_id', '$member_id', '$book_id',
'$date_borrow', '$due_date', '$status_id' ) ";
if ($conn->query($sql) === TRUE)
{
$message = "Borrowing book successful";
echo "<script type='text/javascript'>alert('$message');</script>";
}
else
{
$message = "Borrowing book failed Failed";
echo "<script type='text/javascript'>alert('$message');</script>";
}
}
$conn->close();
?>
and this is my HTML code
<?php
include 'php/header.php';
require_once 'php/connect.php';
?>
<form method="POST" action="php/borrow.php">
<h1>Borrow Book</h1>
Transacted by:
<br>
<div>
<select name="librarian_id">
<option>Select Librarian
<?php
$sql = "SELECT * FROM librarian";
$result = mysqli_query($conn, $sql);
while ($row =mysqli_fetch_assoc($result)) {
echo "<option value='" . $row['librarian_id'] ."'>"
. $row['Fname'] . " " . $row['Lname'] . "</option>";
}
?>
</option>
</select><br><br>
</div>
Select Member
<br>
<div>
<select name="member_id">
<option>Select member
<?php
$sql = "SELECT * FROM members";
$result = mysqli_query($conn, $sql);
while ($row =mysqli_fetch_assoc($result)) {
echo "<option value='" . $row['member_id'] ."'>"
. $row['Fname'] . " " . $row['Lname'] . "</option>";
}
?>
</option>
</select><br><br>
</div>
<div>
<select name="book_id">
<option>Select a Book
<?php
$sql = "SELECT book_setup.book_id, book.book_id, book.title FROM book_setup
INNER JOIN book ON book_setup.book_id = book.book_id WHERE book_setup.status_id = '1' ";
$result = mysqli_query($conn, $sql);
while ($row =mysqli_fetch_assoc($result)) {
echo "<option value='" . $row['book_id'] . "'>"
. $row['title'] . "</option>";
}
?>
</option>
</select><br><br>
</div>
Borrow Date:
<br><br>
<input name="date_borrow" value="<?php echo date('Y-m-d'); ?>" readonly>
<br><br>
Status:
<div>
<select name="status_id">
<option>
<?php
$sql = "SELECT * FROM borrower_status";
$result = mysqli_query($conn, $sql);
while ($row =mysqli_fetch_assoc($result)) {
echo "<option value='" . $row['status_id'] ."'>"
. $row['status'] . "</option>";
}
?>
</option>
</select><br><br>
</div>
<br>
<br>
<button class="button" type="submit">Submit</button>
<button type="reset" value="Reset">Reset</button>
</form>
this is my database for borrowing book
and this is where I get the book that is available to borrow

Do this:
$res = mysqli_query($con, " SELECT days_id FROM book_setup WHERE book_id = '$book_id' ");
$days = mysqli_fetch_assoc($res)['days_id'];

Related

How can I add an empty value in a selection tag?

I have made a form that lets the user to select a year and place they want. The select tag for year is as below:
<strong>Tahun: </strong>
<?php
echo ('<select name="tahun">');
for($i = date("Y"); $i < date("Y")+11; $i++)
{
echo ('<option value= "'.$i.'">'.$i.'</option>');
}
echo ('</select>');
?>
And code for place selection tag as below:
<strong>Tempat: </strong>
<?php
echo ('<select name="tempat">');
$query = "SELECT *FROM bilik WHERE kosong = 'tak'";
$results = $mysqli->query($query) or die($mysqli->error._LINE_);
while($row = $results->fetch_assoc()){
echo ('<option value= "'.$row['nama'].'">'.$row['nama'].'</option>');
}
echo ('</select>');
?>
Now, what I want to do is add empty value with a message "Please choose one" for both selection tag. So, can someone help me how to add that value and message in this tag?
If you wanted to add select options to your code, this is how it should be:
<?php
echo ('<select name="tempat">');
$query = "SELECT *FROM bilik WHERE kosong = 'tak'";
$results = $mysqli->query($query) or die($mysqli->error . _LINE_);
echo ('<option>Please choose one </option>'); // Added this here
while ($row = $results->fetch_assoc()) {
echo ('<option value= "' . $row['nama'] . '">' . $row['nama'] . '</option>');
}
echo ('</select>');
?>
When you add default option with empty value then you should add as <option value="">Select...</option>
<strong>Tahun: </strong>
<?php
echo '<select name="tahun">';
echo '<option value="">Please Choose One</option>';
for($i = date("Y"); $i < date("Y")+11; $i++)
{
echo '<option value= "'.$i.'">'.$i.'</option>';
}
echo '</select>';
?>
and
<?php
$query = "SELECT *FROM bilik WHERE kosong = 'tak'";
$results = $mysqli->query($query) or die($mysqli->error . _LINE_);
echo '<select name="tempat">';
echo '<option value="">Please choose one</option>'; //Please choose one
while ($row = $results->fetch_assoc()) {
echo '<option value= "' . $row['nama'] . '">' . $row['nama'] . '</option>';
}
echo '</select>';
?>

how to Echo only the index of my $row

Hi I have a $row2 and I only need to echo the first index but there's something wrong with my syntax. $row2 has 5 values and I only need to echo the first.
<select class="input form-control" id="Intervention1" name="Intervention1" required>
<?php
$diag = " SELECT walkindetails.IntID, intervention.IntName FROM walkindetails INNER JOIN intervention on walkindetails.IntID = intervention.IntID WHERE TID = '{$row['TID']}'";
$diagarray = mysqli_query($conn, $diag);
$intarray = array();
if ($conn->error) {
die("Query failed: " . $conn->error);
}
while($row2 = mysqli_fetch_array($diagarray)) {
echo " <option value=". $row2['IntName'] ." style='display: none;'>". $row2['IntName'] . "</option>";
}
?>
</select>
You got two possiblities now:
Limiting the PHP output:
<select class="input form-control" id="Intervention1" name="Intervention1" required>
<?php
$diag = " SELECT walkindetails.IntID, intervention.IntName FROM walkindetails INNER JOIN intervention on walkindetails.IntID = intervention.IntID WHERE TID = '{$row['TID']}'";
$diagarray = mysqli_query($conn, $diag);
$intarray = array();
if ($conn->error) {
die("Query failed: " . $conn->error);
}
$row2 = mysqli_fetch_array($diagarray);
echo " <option value=". $row2['IntName'] ." style='display: none;'>". $row2['IntName'] . "</option>";
?>
This just fetches the array one time and outputs it
Limiting the query (recommended, your database needs to support LIMIT):
<?php
$diag = " SELECT walkindetails.IntID, intervention.IntName FROM walkindetails INNER JOIN intervention on walkindetails.IntID = intervention.IntID WHERE TID = '{$row['TID']}' LIMIT 1";
$diagarray = mysqli_query($conn, $diag);
$intarray = array();
if ($conn->error) {
die("Query failed: " . $conn->error);
}
while($row2 = mysqli_fetch_array($diagarray)) {
?>
<?php echo " <option value=". $row2['IntName'] ." style='display: none;'>". $row2['IntName'] . "</option>"; ?>
<?php
}
?>
Now the query only outputs one dataset
<?php
$myVar = 1; // temporary variable for inner if statement
while($row2 = mysqli_fetch_array($diagarray)) {
if($myVar) {
echo "<option value=". $row2['IntName'] ." style='display: none;'>". $row2['IntName'] . "</option>";
}
$myVar = 0; // if will never evaluate to true again
}
?>
Not as pretty as limiting the output, but will echo only when the if statement evaluates to true. It will only the first time around.

Option get data from DB

I have got this table in my Database:
**train_information**
train_id
country_id
user_id
train_name
tare_weight
number_of_bogies
number_of_axles
wheel_diameter_min
wheel_diameter_max
And then i have 2 .php pages (1 is only for classes)
Selector.php
<!--Selector-->
<form name='form' id='selector'>
<?php
$selector = $database->selector();
?>
<select onchange= "mySelection()" id ="selector" name="selector">
<option selected="true" disabled="disabled">Selecteer een trein</option>
<?php
foreach ($selector as $selector) {
print "<h3>" . $selector['train_id'] . "==" . $selector_id . "</h3>";
if ($selector['train_id'] == $selector_id) {
echo "<option value=" . $selector['train_id'] . " selected='selected'> " . $selector['train_name'] . "</option>";
} else {
echo "<option value=" . $selector['train_id'] . "> " . $selector['train_name'] . "</option>";
} }
?>
</select>
</form>
And then the class.php
function selector() {
$sql = "SELECT train_id, train_name FROM train_information";
$sth = $this->pdo->prepare($sql);
$sth->execute();
return $sth->fetchAll();
}
The selector shows the train names in the selection. so that is good.
But i want that when i select it, and press a button like: Select. That it select the information of that selected train and shows it on the page.
And i don't only want to show 1, but if i would like to select a other train after that, it shows both trains information on the page.
How do i do this?
You can do following
Add this Method to class.php
function select_train_details($trainId) {
$sql = "SELECT * FROM train_information where train_id = " . $trainId;
$sth = $this->pdo->prepare($sql);
$sth->execute();
return $sth->fetchAll();
}
Add Following to selector.php
<form name='form' id='selector'>
<?php
$selector = $database->selector();
if (isset($_POST['selector'])) {
$selectorDetail = $database->select_train_details();
}
?>
<select onchange= "this.form.submit" id ="selector" name="selector">
<option selected="true" disabled="disabled">Selecteer een trein</option>
<?php
foreach ($selector as $selector) {
print "<h3>" . $selector['train_id'] . "==" . $selector_id . "</h3>";
if ($selector['train_id'] == $selector_id) {
echo "<option value=" . $selector['train_id'] . " selected='selected'> " . $selector['train_name'] . "</option>";
} else {
echo "<option value=" . $selector['train_id'] . "> " . $selector['train_name'] . "</option>";
}
}
?>
</select> </form> <?php if (isset($selectorDetail)) {
echo "<pre>";
$selectorDetail; } ?>

get user selected option var from $_post - php

<script>
//this script show / hide lanes depend on user school choice
$(document).ready(function () {
toggleFieldsA();
toggleFieldsB();
toggleFieldsC();
toggleFieldsD();
toggleFieldsE();
//this will call our toggleFields function every time the selection value of School field changes
$("#school").change(function () {
toggleFieldsA();
toggleFieldsB();
toggleFieldsC();
toggleFieldsD();
toggleFieldsE();
});
});
function toggleFieldsA() {
if ($("#school").val() == 'School of Economics')
$("#a").show();
else
$("#a").hide();
}
function toggleFieldsB() {
if ($("#school").val() == 'School of Computer Science')
$("#b").show();
else
$("#b").hide();
}
function toggleFieldsC() {
if ($("#school").val() == 'School of Behavioral Sciences')
$("#c").show();
else
$("#c").hide();
}
function toggleFieldsD() {
if ($("#school").val() == 'School of Government and Politics')
$("#d").show();
else
$("#d").hide();
}
function toggleFieldsE() {
if ($("#school").val() == 'School of Nursing')
$("#e").show();
else
$("#e").hide();
}
</script>
<!---this part is course registration form using jquery mobile, javascript, html, and php---->
</head>
<body>
<?php
if (!empty($_POST)) {
//getting other details from the order form (later using files - now only demo capabilities)
$school_name = $_POST[school_name];
echo $school_name;
$lane_name = $_POST[lane_name];
echo $lane_name;
$year = $_POST[year];
echo $year;
} else {
?>
<div id='school_div'>
<span><label>School</label></span>
<?php
$sql = "SELECT school_name FROM school";
$result = mysql_query($sql);
echo "<select id='school' name='school_name'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['school_name'] ."'>" . $row['school_name'] ."</option>";
}
echo "</select>";
?>
</div>
<!---End of School part---->
<div id='a'>
<span><label>Lane</label></span>
<?php
$sql = "SELECT lane_name FROM lane WHERE `lane_school_id` = 1 ORDER BY `lane_name` ASC";
$result = mysql_query($sql);
echo "<select name='lane_name'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['lane_name'] ."'>" . $row['lane_name'] ."</option>";
}
echo "</select>";
?>
</div>
<div id='b'>
<span><label>Lane</label></span>
<?php
$sql = "SELECT lane_name FROM lane WHERE `lane_school_id` = 2 ORDER BY `lane_name` ASC";
$result = mysql_query($sql);
echo "<select name='lane_name'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['lane_name'] ."'>" . $row['lane_name'] ."</option>";
}
echo "</select>";
?>
</div>
<div id='c'>
<span><label>Lane</label></span>
<?php
$sql = "SELECT lane_name FROM lane WHERE `lane_school_id` = 3 ORDER BY `lane_name` ASC";
$result = mysql_query($sql);
echo "<select name='lane_name'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['lane_name'] ."'>" . $row['lane_name'] ."</option>";
}
echo "</select>";
?>
</div>
<div id='d'>
<span><label>Lane</label></span>
<?php
$sql = "SELECT lane_name FROM lane WHERE `lane_school_id` = 4 ORDER BY `lane_name` ASC";
$result = mysql_query($sql);
echo "<select name='lane_name'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['lane_name'] ."'>" . $row['lane_name'] ."</option>";
}
echo "</select>";
?>
</div>
<div id='e'>
<span><label>Lane</label></span>
<?php
$sql = "SELECT lane_name FROM lane WHERE `lane_school_id` = 5 ORDER BY `lane_name` ASC";
$result = mysql_query($sql);
echo "<select name='lane_name'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['lane_name'] ."'>" . $row['lane_name'] ."</option>";
}
echo "</select>";
?>
</div>
<!---End of Lane part---->
/*
I am trying to get user selected option from a form but when i try to get the info by using $_post i'am getting the lest option. i am using hide and show to filter the relevant "lanes" base on "school" choice
I'm getting always 'BA in Nursing'
*/
If I have understood your question correctly, you are trying to send the select values to the php script. For this you will need an html form, if you're not interested in A
<form method='POST'><!-- PUT THE PHP that creates the select options here!--></form>
Also, it's best to use quotes to access the $_POST super global array associative indexes:
$school_name = $_POST['school_name'];
$lane_name = $_POST['lane_name'];
$year = $_POST['year'];
Also, you could try print_r($_POST) to see what values you are receiving from the post!
In addition, the mysql extension is deprecated. It would be best if you were to switch to mysqli or PDO instead!

How to submit a form which contains mysql generated dropdown lists

Ive got a property website im working on for a school project http://www.holidayaviemore.com
I have three dropdown lists which have all been generated from a mysql database.
I have added a submit button so that when the options have been selected the database info will display..this is where I am really stuck!
Im not sure what code of functions are needed to diplay the content on the page or on another page..any ideas? This is the code for the dropdownn boxes and form I have so far..
EDIT
Basically I want the options selected to be displayed on a webpage http://www.holidayaviemore.com shows the dropdown lists
<p>Select Options From Below to find property</p>
<form action="#" name="form" id="form" method="post">
<?php
$sql = "SELECT DISTINCT availability FROM properties";
$result = mysql_query($sql);
echo "<select name='availability'><option value=''>- Availability?--</option>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='". $row['availability']. "'>" . $row['availability'] ."</option>";
}
echo "</select>";
?>
<?php
$sql = "SELECT bedrooms FROM properties LIMIT 10";
$result = mysql_query($sql);
echo "<select name='bedrooms'><option value=''>--Please Choose Bedrooms Bedrooms--</option>";
$count = 1;
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['bedrooms'] . "'> " . $count . "</option>";
$count++;
}
echo "</select>";
?>
<?php
$sql = "SELECT sleeps_min FROM properties LIMIT 12";
$result = mysql_query($sql);
echo "<select name='sleeps_min'><option value=''>--Please Choose Guests--</option>";
$count = 1;
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['sleeps_min'] . "'> " . $count . "</option>";
$count++;
}
echo "</select>";
?>
<input type="submit" name="" value="GO" />
</form>
I think you need this. You need to see if form already submitted and add selected in option tag where it needed.
<p>Select Options From Below to find property</p>
<form action="#" name="form" id="form" method="post">
<?php
$sql = "SELECT DISTINCT availability FROM properties";
$result = mysql_query($sql);
echo "<select name='availability'><option value=''>- Availability?--</option>";
while ($row = mysql_fetch_array($result)) {
if(isset($_POST['availability']) and $row['availability'] == $_POST['availability']){
echo "<option value='". $row['availability']. "' selected>" . $row['availability'] ."</option>";
}
else{
echo "<option value='". $row['availability']. "'>" . $row['availability'] ."</option>";
}
}
echo "</select>";
?>
<?php
$sql = "SELECT bedrooms FROM properties LIMIT 10";
$result = mysql_query($sql);
echo "<select name='bedrooms'><option value=''>--Please Choose Bedrooms Bedrooms--</option>";
$count = 1;
while ($row = mysql_fetch_array($result)) {
if(isset($_POST['bedrooms']) and $row['bedrooms'] == $_POST['bedrooms']){
echo "<option value='" . $row['bedrooms'] . "' selected> " . $count . "</option>";
}
else{
echo "<option value='" . $row['bedrooms'] . "'> " . $count . "</option>";
}
$count++;
}
echo "</select>";
?>
<?php
$sql = "SELECT sleeps_min FROM properties LIMIT 12";
$result = mysql_query($sql);
echo "<select name='sleeps_min'><option value=''>--Please Choose Guests--</option>";
$count = 1;
while ($row = mysql_fetch_array($result)) {
if(isset($_POST['sleeps_min']) and $row['sleeps_min'] == $_POST['sleeps_min']){
echo "<option value='" . $row['sleeps_min'] . "' selected> " . $count . "</option>";
}
else{
echo "<option value='" . $row['sleeps_min'] . "'> " . $count . "</option>";
}
$count++;
}
echo "</select>";
?>
<input type="submit" name="" value="GO" />
</form>

Categories