I'm currently trying around with php and mysqli and facing a little problem right now.
I have a form containing 3 checkboxes, where the user can select a mandatory field. Therefore the database contains 3 attributes (binary; 0,1). A data set can contain betwenn 0 and 3 of these attributes.
These are the checkboxes:
<form method="post" action="handler.php">
<input type="hidden" name="attribute1" value="0" />
<input type="checkbox" name="attribute1" value="1" /> Attribute1 <br/>
<input type="hidden" name="attribute2" value="0" />
<input type="checkbox" name="attribute2" value="1" /> Attribute2 <br/>
<input type="hidden" name="attribute3" value="0" />
<input type="checkbox" name="attribute3" value="1" /> Attribute3 <br/><br/>
<input type="submit" value="Submit">
</form>
So the goal is that if I check attribute1, the result must contain all data sets with attribute1 = 1, but they can also contain attribute2 = 1 & attribute3 = 1.
When I check attribute1 right now I get the results for a1 = 1, a2= 0 and a3 = 0.
So I think this could be solved by using an array which only stores the checked checkboxes, so if attribute1 and attribute3 are checked, it should be $array = [attribute1 = 1, attribute3 = 1].
My current mysqli query:
$strAttribute1 = $_POST["attribute1"];
$strAttribute2 = $_POST["attribute2"];
$strAttribute3 = $_POST["attribute3"];
$query = "SELECT name, beschreibung
FROM uebersicht
WHERE attribute1 = $strAttribute1
AND attribute2 = $strAttribute2
AND attribute3 = $strAttribute3";
As a solution I thought about a query like:
... WHERE " . $array ."
Unfortunately I'm stuck at this point, which is the proper way to solve it with an array? Or is that the wrong idea?
Thanks for the help!
on page 1 i have a form, then on page 2 which is the processor file, i want to select records based on the checked checkboxes that were checked on page 1.
<form action="output.php" method="post">
<input type="checkbox" id="" class="" name="check_list[]" value="something" />
<input type="checkbox" id="" class="" name="check_list[]" value="something else" />
<input type="checkbox" id="" class="" name="check_list[]" value="yet another thing" />
<input type="checkbox" id="" class="" name="check_list[]" value="one more thing" />
<input type="checkbox" id="" class="" name="check_list[]" value="some name" />
<input type="checkbox" id="" class="" name="check_list[]" value="some other name" />
<input type="submit" value="Submit" name="submit">
</form>
the following foreach can display all the values of everything that was checked, but i don't know how to take it further into my sql select statement to select all the records that have a column field by that name.
foreach($_POST['check_list'] as $check) {
echo $check . '<br>';
}
lets say in a table called stuff there are these fields
id, first_title, second_title
so i want to do the following, but obviously this isn't the way to write it. this is the part i need help with.
SELECT * FROM stuff WHERE first_title = $check or second_title = $check
lets us further say that these records exist in the table...
id first_title second_title
-----------------------------------------
1 something something else
2 yet another thing one more thing
3 some name some other name
then lets say these checkboxes were checked:
<input type="checkbox" id="" class="" name="check_list[]" value="something" />
<input type="checkbox" id="" class="" name="check_list[]" value="one more thing" />
so what i want to happen is for my select statement to select record 1 and record 2 and not record 3, because "something" is in the first_title column of the first record, and "one more thing" is in the second_title of the second record, and nothing was checked that is in third record.
i hope i gave as much detail as is needed. let me know if you need further explanation.
Use the SQL IN operator to test if a column is in a list of values. Here's how to write it with MySQLI:
$in_str = implode(', ', array_map(function($title) use ($con) {
return "'" . $con->real_escape_string($title) . "'";
}, $_POST['check_list']));
$sql = "SELECT * FROM stuff WHERE first_title IN ($in_str) OR second_title IN ($in_str)";
$result = $con->query($sql);
try this dynamic where condition in your code
<?php
$arr_where = array();
foreach($_POST['check_list'] as $check) {
$arr_where[] = " first_name='$check' OR last_name='$check' ";
}
$where_text = implode("OR", $arr_where);
$sql = "SELECT * FROM stuff WHERE ".$where_text;
?>
I have form and php file, and I need to query color in array data from database.
data looks like
red,white mustang 1977
black,blue ford 2000
white,pink,blue opel 2003
and form looks like
<form method="POST" action="select.php">
<p><input type="radio" value="just" checked name="selection">just selected<input type="radio" name="selection" value="allinclude">all include </p>
<p><input type="checkbox" name="color[]" value="ON">red</p>
<p><input type="checkbox" name="color[]" value="ON">blue</p>
<p><input type="checkbox" name="color[]" value="ON">black</p>
<p><input type="checkbox" name="color[]" value="ON">white</p>
<p><input type="checkbox" name="color[]" value="ON">pink</p>
<p> </p>
<p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>
and I need 2 sql query, one of check all selected options together in data, and one of any of them in data
in my select.php
$selection= $_POST["selection"];
$color=implode(",",$_POST["color"]);
$sql= mysql_query("select model,year,color where color like '%".$color."%'");
When I select blue and white, sql returns no row.
If I select white, then returns 2 row
But I want to select all include radio box and select blue and white then return 3 rows
How can I write second sql for all included selection?
Here is new codes, but this is only query latest item in array, not all selected for 'just' section of selection form
$selection= $_POST["selection"];
$color=implode(",",$_POST["color"]);
if($selection=='just'){
$colorselect = explode(',', $color);
foreach($colorselect as $b)
{
$mycolor = trim($b);
$sql= mysql_query("select model,year,color where color like '%".$mycolor."%'");
}
}
else{
$sql= mysql_query("select model,year,color where color like '%".$color."%'");
}
If I understand your problem correctly, your first query gets all rows that include all of the colors selected - and you need a second query that will get all rows that include just one or more of the colors selected. In that case, you need your query to include a series of ORs that will check for each individual color in each row. (Note: your query seems to be missing a FROM statement. I have added this in, assuming your table is called cars).
Assuming $colors is an array of selected colors, eg: ['blue', 'white', 'red'], you could set up your query as follows:
$query = 'SELECT model,year,color FROM cars WHERE ';
$or = '';
foreach ($colors as $color) {
$query .= $or . "color LIKE '%" . $color . "%'";
$or = ' OR ';
}
This will effectively build a query that looks like this:
SELECT model,year,color
FROM cars
WHERE color LIKE '%blue%'
OR color LIKE '%white%'
OR color LIKE '%red%'
which will select all the rows from your example data - each row contains at least one of the colors selected. If you wanted to change this logic to get all rows that include all of the colors, simply change OR to AND - this will match rows that have all selected colors, regardless of the order in which they were selected.
Additional thoughts:
Storing comma separated values like blue,white is usually bad practice. Consider normalizing your data and use a system that includes something like a colors and car_colors table.
mysql_* functions are deprecated. Consider switching to mysqli_* or pdo
UPDATE:
Here is the complete system. This will create the appropriate query based on which radio button is selected:
(Assuming the sample data you provided is in a table called cars)
form:
<form method="POST" action="select.php">
<p><input type="radio" value="just" checked name="selection">just selected<input type="radio" name="selection" value="allinclude">all include </p>
<p><input type="checkbox" name="color[]" value="red">red</p>
<p><input type="checkbox" name="color[]" value="blue">blue</p>
<p><input type="checkbox" name="color[]" value="black">black</p>
<p><input type="checkbox" name="color[]" value="white">white</p>
<p><input type="checkbox" name="color[]" value="pink">pink</p>
<p> </p>
<p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>
select.php:
$selection = $_POST['selection'];
$colors = $_POST['color'];
$op = $selection == 'just' ? ' AND ' : ' OR ';
$query = 'SELECT model,year,color FROM cars WHERE ';
$and_or = '';
foreach ($colors as $color) {
$query .= $and_or . "color LIKE '%" . $color . "%'";
$and_or = $op;
}
$sql = mysql_query($query);
The effective HTML of Fred -ii-s comment is:
<form method="POST" action="select.php">
<p><input type="radio" value="just" checked name="selection">just selected<input type="radio" name="selection" value="allinclude">all include </p>
<p><input type="checkbox" name="colors[]" value="red">red</p>
<p><input type="checkbox" name="colors[]" value="blue">blue</p>
<p><input type="checkbox" name="colors[]" value="black">black</p>
<p><input type="checkbox" name="colors[]" value="white">white</p>
<p><input type="checkbox" name="colors[]" value="pink">pink</p>
<p> </p>
<p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>
And then in your PHP code you would just use the $_POST['colors'] array of data like any other array in PHP.
$colors = implode($_POST['colors'],',');
$sql= mysql_query("select model,year,color where color in (".$colors.")");
Your HTML:
<form method="POST" action="select.php">
<p><input type="radio" value="just" checked name="selection">just selected<input type="radio" name="selection" value="allinclude">all include </p>
<p><input type="checkbox" name="color[]" value="red">red</p>
<p><input type="checkbox" name="color[]" value="blue">blue</p>
<p><input type="checkbox" name="color[]" value="black">black</p>
<p><input type="checkbox" name="color[]" value="white">white</p>
<p><input type="checkbox" name="color[]" value="pink">pink</p>
<p> </p>
<p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>
Your PHP:
$colors = array("red", "blue", "black", "white", "pink");
// Make sure that only allowed colors make through - we don't want SQL injection
$selection = array();
foreach ($_POST["colors"] as $color)
{
if (in_array($color, $colors) === true)
{
$selection[] = $color;
}
}
// Now a bit of magic
$sql= mysql_query("select model,year,color from data where color like '%" . implode("%' " . ($_POST["selection"] === "just" ? "or" : "and") . " color like '%",$selection) . "%'");
Hi I'm quite new to php and I'm currently making a webpage similar to the ones used by supermarkets for stock management control as part of an assignment. I have the following form where the cashier would enter the product Id and the quantity of the item being purchased.
The form will then call another php file named cashsale.php which will take these inputs and update the tables in my database so that levels of stock on shelves in supermarkets are up to date with the new amounts (i.e. older qty - qty entered) and management can be advised when reorder is needed.
As it is the form works well, however I was advised to edit it in a way that a cashier can enter multiple products and quantities before submitting (i.e. the form will sort of show itself again) and allow the user to edit or remove any items before actually submitting the values to cashsale.php to manipulate the tables. I seem to be at a loss as to how this can be done.
I wanted to create a new button named "Add" which would display the form again, i.e. allow the user to check in more items, but I am confused as to how this can be done and also as to how I will be able to update tables then since I would be having more then just 2 inputs.
Can anyone help me on this please? Thanks in advance. The following is my html form:
center form action="cashsale.php" method ="post"
Product ID: <input name= "id" type="int" > <br>
Quantity:<input name="qty" type="int">
<input type="button" name = "Add" onclick="add">
<input type="Submit" name ="Submit" value = "Submit">
form center
I was not allowed to use html tags for form and center so I removed the < >. The following is some of the modifications done in the cashsale.php file just to give a clearer example.
$result = mysql_query("SELECT * FROM shelfingdetails where prodId =' " .$id. " '");
if (!$result){
die (mysql_error());
}
while ($row =mysql_fetch_array($result))
{
$qtyOnShelf= $row ['QtyOnShelf'];
$max=$row['max'];
$newQtyShelf=$qtyOnShelf-$qty;
}
$update=mysql_query("UPDATE shelfingdetails SET QtyOnShelf ='". $newQtyShelf. "' where prodId = '". $id. "';");
I hope someone can help. Thanks!
You just have to pass an array. For this you have to generate the inputs with PHP or javascript (I'm gona use jQuery to keep the code nice and simpe).
If you use PHP:
// PHP
<?php
if(isset($_POST['submit']) && $_POST['submit']){
//save data
} elseif(isset($_POST['Add']) && $_POST['Add']) {
$max = (int)$_POST['max']
} else { $max = 1; }
?>
<form action="" method="post">
<?php
for($i = 0;$i < $max;$i++){
?>
Product ID: <input name="id[]" type="int" > <br>
Quantity: <input name="qty[]" type="int">
<?php
}
?>
<input type="hidden" name="max" value="<?= $i; ?>" />
<input type="submit" name="Add" />
<input type="submit" name="Submit" value="Submit" />
</form>
If yo use Javascript:
//HTML
<form action="" method="post" id="form">
<div id="add">
Product ID: <input name="id[]" type="int" > <br>
Quantity: <input name="qty[]" type="int">
</div>
<input type="hidden" name="max" value="<?= $i; ?>" />
<input type="button" name="Add" onclick="addRow();" />
<input type="submit" name="Submit" value="Submit" />
</form>
// jQuery
function addRow(){
$("#add").append("<br />Product ID: <input name='id[]' type='int' >" +
"<br />Quantity: <input name='qty[]' type='int'>");
}
you have to use id[] and qty[] to pass them as an array and with the add button generate as many of them as you need. Like so:
Product ID: <input name="id[]" type="int" > <br>
Quantity: <input name="qty[]" type="int"> <br>
Product ID: <input name="id[]" type="int" > <br>
Quantity: <input name="qty[]" type="int"> <br>
Product ID: <input name="id[]" type="int" > <br>
Quantity: <input name="qty[]" type="int">
<input type="button" name = "Add" onclick="add">
Then on the backand use for loop to save all the data.
$max = count($_POST['id']);
$id = $_POST['id'];
$newQtyShelf = $_POST['qty'];
for($i = 0;$i < $max;$i++){
$update=mysql_query("UPDATE shelfingdetails
SET QtyOnShelf ='". (int)$newQtyShelf[i]. "'
WHERE prodId = '". (int)$id[i]. "';");
}
I just wanted to show you the idea, please don't use this specific code, because you should use mysqli instead of mysql and also mysqli_escape_string to make sure that the user not submits incorrect data.
I need help ... that too from scratch as now am learning php. just variable declaration i am learning.
I have created a form with 5 check boxes. when i select any 1 or any 2, 3.. or any combination, i should get the data which is already stored for that option in MySQL database.
My form is this:
<form method="post" action="search.php" name="search_form" onsubmit="return checkCheckBoxes(this);">
<input type="checkbox" name="search1" value="qwerty_keypad" id="search1">QWERTY Keypad
<input type="checkbox" name="search2" value="touch_screen" id="search2"> Touch Screen
<input type="checkbox" name="search3" value="usb" id="search3"> SUB Drive
<input type="checkbox" name="search4" value="mobile_tracker" id="search4">Mobile Tracker
<input type="checkbox" name="search5" value="Backup" id="search5">Phone backup on MMC
<input type="submit" value="Search" /> </form>
what i should write in search.php.
Please help me ... please
Thanks in advance
Use an array to submit the values
<input type="checkbox" name="search[connectivity]" value="usb" id="search3"> USB
<input type="checkbox" name="search[display]" value="touchscreen" id="search4">Touchscreen
Afterwards you build your query based on those values:
foreach($_POST['search'] as $k=> $search){
$where[]= $k." = '".mysql_real_escape_string($search)."'";
}
$query = "Select * from table where ".implode(' AND ',$where);