MYSQL query based on multiple HTML Checkboxes - mysql

I have a table containing soil analysis, around 400k rows, with about 30 columns. All rows have a Year column, where it says something between 1997 and 2014.
I let the user do a MySQL query based on a couple of HTML-forms and one checkbox for each year.
If no checkbox is filled it searches through every year in the table.
My intention is to let the user be able to select a specific year or several with the help of the checkboxes.
I have copied what i have right now. (I've excluded the variables and stuff, excuse the swedish here and there)
The HTML-forms work fine. But i haven't figured how to take use of the checkboxes yet. I have no idea how to implement them in the MySQL query. I've tried googling but the question seems kinda specific in my case.
I'd be really happy to hear your opinion in the matter.
<form id="form" action="statisticsToMap.php" method="post">
<p>______Minimum_____________Maximum______</p>
<p>pH: <input type="text" name="min-ph" onkeypress="return isNumberKey(event)"> pH: <input type="text" name="max-ph" onkeypress="return isNumberKey(event)"></p>
<p>P-AL: <input type="text" name="min-p" onkeypress="return isNumberKey(event)"> P-AL: <input type="text" name="max-p" onkeypress="return isNumberKey(event)"></p>
<p>K-AL: <input type="text" name="min-k" onkeypress="return isNumberKey(event)"> K-AL: <input type="text" name="max-k" onkeypress="return isNumberKey(event)"></p>
<p>Mg-AL: <input type="text" name="min-mg" onkeypress="return isNumberKey(event)"> Mg-AL: <input type="text" name="max-mg" onkeypress="return isNumberKey(event)"></p>
<p>Lerhalt: <input type="text" name="min-ler" onkeypress="return isNumberKey(event)"> Lerhalt: <input type="text" name="max-ler" onkeypress="return isNumberKey(event)"></p>
<p>Sand-Grovmo: <input type="text" name="min-sgrovmo" onkeypress="return isNumberKey(event)"> Sand-Grovmo: <input type="text" name="max-sgrovmo" onkeypress="return isNumberKey(event)"></p>
<p>Mullhalt: <input type="text" name="min-mull" onkeypress="return isNumberKey(event)"> Mullhalt: <input type="text" name="max-mull" onkeypress="return isNumberKey(event)"></p>
<p>Klicka i vilka årtal som du vill söka efter: (tomma rutor söker alla årtal)</p>
<input type="checkbox" name="1997" value="1997"> 1997
<input type="checkbox" name="1998" value="1998"> 1998
<input type="checkbox" name="1999" value="1999"> 1999
<input type="checkbox" name="2000" value="2000"> 2000
<input type="checkbox" name="2001" value="2001"> 2001<br>
<input type="checkbox" name="2002" value="2002"> 2002
<input type="checkbox" name="2003" value="2003"> 2003
<input type="checkbox" name="2004" value="2004"> 2004
<input type="checkbox" name="2005" value="2005"> 2005
<input type="checkbox" name="2006" value="2006"> 2006<br>
<input type="checkbox" name="2007" value="2007"> 2007
<input type="checkbox" name="2008" value="2008"> 2008
<input type="checkbox" name="2009" value="2009"> 2009
<input type="checkbox" name="2010" value="2010"> 2010
<input type="checkbox" name="2011" value="2011"> 2011<br>
<input type="checkbox" name="2012" value="2012"> 2012
<input type="checkbox" name="2013" value="2013"> 2013
<input type="checkbox" name="2014" value="2014"> 2014
<input type="checkbox" name="2015" value="2015"> 2015
<input type="checkbox" name="2016" value="2016"> 2016<br>
<p>Begränsa antal resultat:</p> <input type="text" name="limitrows" onkeypress="return isNumberKey(event)"> (Standard är 2000 rader)<br><br>
<table>
<tr>
<input type="submit" value="Sök i databas"onclick="submitForm('')">
<input type="submit" value="Visa resultat på karta" onclick="submitForm('statisticsToMap.php')">
</form>
</tr>
</table>
$sql = "SELECT `kundnr`, `Year`, `Provnr`, `pH`, `P_AL`, `P_HCl`, `K_AL`, `K_HCl`, `Mg_AL`,
`Cu_HCl`, `K_Mg_kvot`, `Bor`, `Ca_AL`, `Total_lerhalt`, `Sand_grovmo`, `Mullhalt`
FROM `analyser`
WHERE
(IFNULL(`pH`, '0') BETWEEN $minph AND $maxph)
AND (IFNULL(`P_AL`, '0') BETWEEN $minpal AND $maxpal)
AND (IFNULL(`K_AL`, '0') BETWEEN $minkal AND $maxkal)
AND (IFNULL(`Mg_AL`, '0') BETWEEN $minmg AND $maxmg)
AND (IFNULL(`Total_lerhalt`, '0') BETWEEN $minler AND $maxler)
AND (IFNULL(`Mullhalt`, '0') BETWEEN $minmull AND $maxmull)
AND (IFNULL(`Sand_grovmo`, '0') BETWEEN $minsgrovmo AND $maxsgrovmo)
LIMIT 0,$limitrows";
/Simon

Wouldn't that simply be the case where you will dynamically build a YEAR IN (..) clause based on the year selection? E.g. when 2008, 2010 and 2012 were selected:
SELECT * FROM SOIL_DATA WHERE YEAR IN (2008, 2010, 2012)

Related

HTML Pattern for phone numbers

This is my first time writing a pattern for HTML phone numbers. I am based in Singapore. In my country phone numbers starts with 8 or 9 and has 9 digits.
This is my html pattern.
Can someone take look and let me know what went wrong?
I don't see any resources out there for including certain numbers only.
pattern: ([8|9]{1}[0-9]{8})
below is my full code
<h3>Sign Up</h3>
<label htmlFor="username" className="sr-only">Username: </label>
<input type="text"
name="username"
minLength= '5'
maxLength= '10'
onChange={onChange}
className="form-control"
placeholder="Enter Username" required />
<label htmlFor="password" className="sr-only">Password: </label>
<input type="password"
name="password"
minLength= '8'
pattern = "(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}"
title = "Must contain at least one number and one uppercase and lowercase letter, and at least 8 or more characters"
onChange={onChange}
className="form-control"
placeholder="Enter Password" />
<label htmlFor="email" className="sr-only">Email: </label>
<input type="text"
name="email"
onChange={onChange}
pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,}$"
className="form-control"
placeholder="Email" required />
<label htmlFor="mobile" className="sr-only">Mobile Number: </label>
<input type="number"
name="mobile"
onChange={onChange}
className="form-control"
pattern="[8-9]{1}[0-9]{8}"
placeholder="Mobile Number" />
<button className="btn btn-lg btn-primary btn-block"
type="submit">Sign Up</button>
The answer is that pattern is not a valid attribute on input when type=number. Also the pipe character inside []'s doesn't mean "or". Either change the input type to another value, or use min/max.
<input type="text" inputmode="numeric"
name="mobile"
onChange={onChange}
className="form-control"
pattern="[89][0-9]{8}"
placeholder="Mobile Number" />
or
<input type="number"
name="mobile"
onChange={onChange}
className="form-control"
min=800000000
max=999999999
step=1
placeholder="Mobile Number" />
Phone number starting with 8-9 and remaing 9 digit with 0-9" required
<input type="text" class="form-control" readonly="readonly" value="" name="enquiry_mobile" placeholder="Enter mobile number *" pattern="[8-9]{1}[0-9]{8}">
If you expect that using pattern on input will prevent you from entering incorrect data base on that pattern, you're wrong.
You have to submit the form for the browser to validate. The first form bellow will submit, the second one will not.
<form>
<input value="891234567" type="tel" id="phone" name="phone" pattern="([89][0-9]{8})" />
<button>Submit</button>
</form>
<form>
<input value="121234567" type="tel" id="phone" name="phone" pattern="([89][0-9]{8})" />
<button>Submit</button>
</form>
Edit:
As #Robert McKee pointed in the comment below, the first pattern used on this answer is incorrect. Updated pattern to ([89][0-9]{8}).

How to put a checkout date and prevent the user from changing it?

I would like to produce something similar to the checkin and checkout for a hotel reservation system. So far, I'm allowing the user to enter the booking date (which is the check in). However, I would like to implement an unedited input with the value of checkin plus number of nights.
So, after the user has selected the booking date and the number of nights, it would add them together and display the checkout date on the side (but the user can't change it). After the duration has finished, the user can book the same hotel again, without telling the user that they have already booked that hotel.
<body>
<form action="" method="POST">
<fieldset>
<legend>Personal Details: </legend>
<label for="phone">Phone:</label><input type="tel" name="phone" id="phone" required placeholder="Please enter in your phone number" pattern="[0-9]{4}[0-9]{3}[0-9]{3}" title="Please enter in a phone number in this format:#### ### ###">
<select name="country" required >
<option value=""></option>
<option value="US">US</option>
<option value="UK">UK</option>
<option value="AUS">AUS</option>
</select>
</fieldset>
<br>
<fieldset>
<legend>Booking Details: </legend>
<label for="date">Booking date: </label><input id="date" type="date" name="date" min="2017-01-04">
<label for="numberOfRooms">Number of Rooms:</label><input id="NumberOfRooms" type="number" name="numberOfRooms" min="1" max="4">
<label for="numberOfNights">Number of Nights:</label><input id="NumberOfNights" type="number" name="numberOfNights" min="1" max="60">
<p>Do you require breakfast?</p>
<label for="yesBreakfast">Yes:</label><input id="yesBreakfast" type="radio" name="meals" value="yesBreakfast">
<label for="noBreakfast">No:</label><input id="noBreakfast" type="radio" name="meals" value="noBreakfast">
<br>
<!--<label for="balcony">Do you require a balcony?</label><input id="balcony" type="checkbox" name="balcony" value="yes" checked>-->
<br>
<button name="submit" type="submit">Submit</button>
</fieldset>
</form>
</body>
</html>
database tables for the book and the hotel
Book table:
id(int 11)
username(varchar 30)
hotel_id(int11)
phone(varchar 50)
date(datetime)
num_rooms(int 60)
num_nights(int 4)
Table for hotel
id(int 11)
Hotel_name(text)
Hotel_location(text)
Hotel_price(int 11)
image(varchar 500)
description(text)
I suggest you use momentjs to handle date related stuff.
I added change event on both date and NumberOfNights to call updateCheckout(), in that function will handle the checkout date
hope it helps
$(document).ready(function() {
$('#date, #NumberOfNights').on("change", updateCheckout);
$('#price, #NumberOfNights, #NumberOfRooms').on("change", updateTotalPrice);
function updateCheckout() {
var bookingDate = $('#date').val();
var numOfNights = $('#NumberOfNights').val();
if (bookingDate != '' && numOfNights != '') {
var new_date = moment(bookingDate, "YYYY-MM-DD").add(numOfNights, 'days').format("YYYY-MM-DD");
$('#checkout_date').text(new_date);
} else {
$('#checkout_date').text('N/A');
}
}
function updateTotalPrice() {
var price = $('#price').val();
var numOfNights = $('#NumberOfNights').val();
var NumberOfRooms = $('#NumberOfRooms').val();
if (price != '' && numOfNights != '' && NumberOfRooms != '') {
var total_price = +price * +numOfNights * +NumberOfRooms;
$('#total_price').text('$' + total_price);
} else {
$('#total_price').text('N/A');
}
}
});
#checkout_date,
#total_price {
color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<body>
<form action="" method="POST">
<fieldset>
<legend>Personal Details: </legend>
<label for="phone">Phone:</label>
<input type="tel" name="phone" id="phone" required placeholder="Please enter in your phone number" pattern="[0-9]{4}[0-9]{3}[0-9]{3}" title="Please enter in a phone number in this format:#### ### ###">
<select name="country" required>
<option value=""></option>
<option value="US">US</option>
<option value="UK">UK</option>
<option value="AUS">AUS</option>
</select>
</fieldset>
<br>
<fieldset>
<legend>Booking Details: </legend>
<label for="date">Booking date: </label>
<input id="date" type="date" name="date" min="2017-01-04">
<label for="numberOfRooms">Number of Rooms:</label>
<input id="NumberOfRooms" type="number" name="numberOfRooms" min="1" max="4">
<label for="numberOfNights">Number of Nights:</label>
<input id="NumberOfNights" type="number" name="numberOfNights" min="1" max="60">
<label for="price">Single Room Price: </label>
<input id="price" type="number" min="0">
<br>
<br>
<label>Checkout Date:</label>
<span id="checkout_date"> N/A</span>
<p>Do you require breakfast?</p>
<label for="yesBreakfast">Yes:</label>
<input id="yesBreakfast" type="radio" name="meals" value="yesBreakfast">
<label for="noBreakfast">No:</label>
<input id="noBreakfast" type="radio" name="meals" value="noBreakfast">
<br>
<!--<label for="balcony">Do you require a balcony?</label><input id="balcony" type="checkbox" name="balcony" value="yes" checked>-->
<br>
<label>Total Price:</label>
<span id="total_price"> N/A</span>
<br><br>
<button name="submit" type="submit">Submit</button>
</fieldset>
</form>
</body>
</html>

Cannot store multiple radio button values in php and mysql

I want to store radio button values for each question to my database. I ran it and it stores only for the first question. I know that it may be wrong using if and elseif. The user answers in all questions how can I store them all in mysql table? Does it need to use for loop?
saveunit.php
<?php
include'connect.php';
if(isset($_POST['u1'])){
$u1 = $_POST['u1'];
mysql_query("INSERT INTO unit_form (u1) VALUES ('$u1')");
}
elseif(isset($_POST['u2'])){
$u2 = $_POST['u2'];
mysql_query("INSERT INTO unit_form (u2) VALUES ('$u2')");
}
?>
unitform.php
<form action="saveunit.php" method="POST">
U1 I have found the unit intellectually interesting and stimulating : <br />
1 <input type="radio" name="u1" value="1"> 2 <input type="radio" name="u1" value="2"> 3 <input type="radio" name="number" value="u1"> 4 <input type="radio" name="u1" value="4"> 5 <input type="radio" name="u1" value="5"> <br /> <br />
U2 I have gained knowledge that I consider valuable : <br />
1 <input type="radio" name="u2" value="1"> 2 <input type="radio" name="u2" value="2"> 3 <input type="radio" name="u2" value="3"> 4 <input type="radio" name="u2" value="u2"> 5 <input type="radio" name="u2" value="5"> <br /> <br />
U3 I have acquired skills and abilities that I consider valuable : <br />
1 <input type="radio" name="u3" value="1"> 2 <input type="radio" name="u3" value="2"> 3 <input type="radio" name="u3" value="3"> 4 <input type="radio" name="u3" value="4"> 5 <input type="radio" name="u3" value="5"> <br /> <br />
<input type = "Submit" name = "Submit1" value = "Submit">
<input type="reset" name="reset" value="Clear">
I dont think that you need an if and an else if. It stores the u1 because it always execute the first if(), that's why it always store the first data . What you need to do is to have all the units in one if().
Try this...
if(isset($_POST['u1'])){
$u1 = $_POST['u1'];
$u2 = $_POST['u2'];
$u3 = $_POST['u3'];
$u4 = $_POST['u4'];
$u5 = $_POST['u5'];
mysql_query("INSERT INTO unit_form (u1,u2,u3,u4,u5) VALUES ('$u1','$u2','$u3','$u4','$u5')");
}

Integrating Neteller to my Asp.net website

I am banging my head for 2 days to integrate neteller into my website but could not get it,
Returned response in xml contain error saying Invalid merchantid/merchant key ,how can i get them?
<form method="post" action="https://test.api.neteller.com/netdirect">
<input type="text" name="version" value=" 4.1">
<input type="text" name="amount" size="10" value="3443" maxlength="10">
<input type="text" name="currency" value="USD" size="10" maxlength="3">
<input type="text" name="net_account" size="20" value="" maxlength="100">
<input type="text" name="secure_id" size="10" value="" maxlength="6">
<input type="hidden" name="merchant_id" value="43646">
<input type="hidden" name="merch_key" value="456453">
<input type="hidden" name="merch_transid" value="46436436" maxlength="50">
<input type="hidden" name="language_code" value="EN">
<input type="hidden" name="merch_name" value="fdghdfhgf">
<input type="hidden" name="merch_account" value="436346" maxlength="50">
<input type="hidden" name="custom_1" value="test123" maxlength="50">
<input type="hidden" name="custom_2" value="test123" maxlength="50">
<input type="hidden" name="custom_3" value="test123" maxlength="50">
<button type="submit" name="submit">Make Transfer</button>
</form>
At first You should not put your merchant credentials in hidden inputs for security reasons. Everyone can see your sensitive data. Make a POST on server side not client side.
merchant API key You can generate on merchant neteller panel in the Developer tab.

Create array from submitted values

How do I create 2 HTML arrays from value pairs on form submit?
from <input type="text" name="val_a1" value="1">
to <input type="text" name="val_b1" value="2">
from <input type="text" name="val_b1" value="3">
to <input type="text" name="val_b1" value="4">
from <input type="text" name="val_c1" value="5">
to <input type="text" name="val_c1" value="6">
to look like
array[1,3,5] and array[2,4,6]
Do I need to five unique field names, like in my example or just keep val_a and val_b?
Use brackets in the field names to have the input value return an array:
from <input type="text" name="from[0]" value="1">
to <input type="text" name="to[0]" value="2">
from <input type="text" name="from[1]" value="3">
to <input type="text" name="to[1]" value="4">
from <input type="text" name="from[2]" value="5">
to <input type="text" name="to[2]" value="6">
Note that the keys (0, 1, 2) are optional and could be anything you want (or none at all), but I used them so it would make more sense once you get the return values. You should receive from and to as an array when submitting the form now.
AFAIK you can have several query values with the same name. So it would then depend on what backend you are using to parse the querystring. Common practice is to name several fields that should be grouped together name[], and most backends will turn that into an array then. So try both methods out on your backend and check how they are treated!
if you just want an array you can do it like this
from <input type="text" name="from[]" value="1">
to <input type="text" name="to[]" value="2">
from <input type="text" name="from[]" value="3">
to <input type="text" name="to[]" value="4">
from <input type="text" name="from[]" value="5">
to <input type="text" name="to[]" value="6">
you don't need to number them they will be in order they are recieved
but you can go into nesting too see this post for more details