here I have a table which its data come from database (Mysql DBMS)
In this table I can delete a book one by one but what I need is to delete multiple Book in once
Something like this
But Actually I have no Idea How to do That Can you guys do me a favor how to do that plz I will be thankful
Create a list of checkboxes with the same name using [] notation, but different values, ids of records presumably:
<input type="checkbox" name="record_id[]" value="42" />
<input type="checkbox" name="record_id[]" value="43" />
<input type="checkbox" name="record_id[]" value="44" />
<input type="checkbox" name="record_id[]" value="45" />
After you select some of them, they will be passed to a server as $_POST['record_id'] array.
Do a foreach:
foreach ($_POST['record_id'] as $id) {
// delete record with $id here
}
Or implode'em, for example:
$sql = "DELETE FROM `mytable` WHERE id IN (" . implode(', ', $_POST['record_id']) . ")";
Related
I have been searching this for the last couple of hours and there are a few very similar questions and answers but none are quite working for my situation. What I'm trying to do is insert a list of songs set to the same value for two columns, one per row, and with a couple of options set via radio buttons. The latter is the reason why I can't just write all of the songs in a textarea and split them.
This is what the form looks like (obviously not fully designed)
There are only three there now while I get it working, in reality an unlimited amount may be added via javascript.
And the code:
<form action="" method="post">
<label>Session ID</label>
<input type="text" name="session-id"><br>
<label>Songs</label><br>
<input type="text" name="song-key[]"><input type="checkbox" name="is-partial[]"><input type="checkbox" name="is-jam[]"><br>
<input type="text" name="song-key[]"><input type="checkbox" name="is-partial[]"><input type="checkbox" name="is-jam[]"><br>
<input type="text" name="song-key[]"><input type="checkbox" name="is-partial[]"><input type="checkbox" name="is-jam[]"><br>
<input type="text" name="update-date" value="<?php echo date(" Y-m-d H:i:s ");?>" hidden readonly><br>
<input type="submit" name="submit" value="Submit">
</form>
The desired result, assuming the ID has been set and a unique song entered for each of the them, and whatever variety on the radio buttons, would be three table rows. session-id and update-date would all be the same value, the rest would be unique based on entry.
This is what I currently have, but it only inserts the last of the three songs.
for ($i=0; $i < count($_POST['song-key']); $i++ ) {
$sessionkey = mysqli_real_escape_string($connection, $_POST["session-key"]);
$songkey = mysqli_real_escape_string($connection, $_POST["song-key"][$i]);
$partial = mysqli_real_escape_string($connection, isset($_POST['is-partial'][$i])) ? 1 : 0;
$jam = mysqli_real_escape_string($connection, isset($_POST['is-jam'][$i])) ? 1 : 0;
$updated = mysqli_real_escape_string($connection, $_POST["update-date"]);
$sql = "INSERT INTO session_songs (session_key, song_key, is_partial, is_jam, last_updated)
VALUES ('$sessionkey', '$songkey', '$partial', '$jam', '$updated')";
}
What do I need to change to ensure all three (or more) are entered?
If you alter your input names, you can get a more useful $_POST array.
<input type="text" name="songs[0][key]">
<input type="checkbox" name="songs[0][is-partial]">
<input type="checkbox" name="songs[0][is-jam]"><br>
<input type="text" name="songs[1][key]">
<input type="checkbox" name="songs[1][is-partial]">
<input type="checkbox" name="songs[1][is-jam]"><br>
<input type="text" name="songs[2][key]">
<input type="checkbox" name="songs[2][is-partial]">
<input type="checkbox" name="songs[2][is-jam]"><br>
With specified keys, the checkbox type inputs will match up properly with the text inputs, where they way you have it now, they will not*, because only checked checkboxes are submitted to PHP. (Try only checking "is-partial" in the second and "is-jam" in the third row, and then var_dump($_POST), and you'll see that they both have index 0.)
If your form is structured like that, you can insert your records using a foreach loop instead of a for loop.
foreach ($_POST['songs'] as $song) {
$key = $song['key'];
$partial = isset($song['is-partial']) ? 1 : 0;
$jam = isset($song['is-jam']) ? 1 : 0;
// do the insert inside the loop rather than just building the SQL
}
The reason you're currently only getting the last one is that you're defining the SQL string inside the loop, but without executing the statement in the loop, you'll only get the values from the last iteration of the loop. Move your query execution inside the loop and you should get all three rows.
*unless they are all checked
I'm creating some simple search page for my little project. Its about list of Multifunctional devices and options on devices.
I have table devices and fields with device options:
option_hdd
option_fax
option_df (document feeder)
option_duplex
and so on... totally 8 option fields.
If device contain options in field is 1 if not 0.
I created small form with check boxess for all this option with GET method.
<form action="search.php" method="get" name="search">
<input name="HDD" type="checkbox" value="1" /> HDD<br />
<input name="Fax" type="checkbox" value="1" /> Fax<br />
<input name="DF" type="checkbox" value="1" /> DF<br />
...
<input name="Search" type="submit" />
</form>
I try to create some query but i don't get exact results.
If user want to check for devices which contain hdd and fax option he will check that two checkboxes and hit search, query need to return results with devices that contain checked options (not essential for other options)
i try with fallowing query's i found googling :)
SELECT * FROM devices WHERE option_hdd = '$_GET[HDD]' OR option_fax = '$_GET[Fax]' OR option_finisher = '$_GET[Df]' ......'
and also
SELECT * FROM devices WHERE option_hdd = '$_GET[HDD]' AND (option_fax = '$_GET[Fax]' OR option_finisher = '$_GET[Df]' ......);
but i do'n get wanted results... but this last is the most close-up...
Example i have 5 devices in DB for testing
All 5 devices have Hdd and 2 devices have Fax option
In form i check hdd and fax and i get result of all 5 devices, but correct results must be 2... cause only two devices have fax option...
Can somebody help me and give me correct query :)
Thanks
I suggest using 'AND' but...
If the checkbox is checked, the value will be 1. If the checkbox is not checked the value will be empty.
So you code will be like this:
SELECT * FROM devices WHERE option_hdd = '1' AND option_fax = ' ' AND option_finisher = '1';
Which will give you a wrong resultset.
Try this instead:
$hdd = (!empty($_GET['HDD'])) ? $_GET['HDD'] : 0;
$fax = (!empty($_GET['Fax'])) ? $_GET['HDD'] : 0;
$df = (!empty($_GET['DF'])) ? $_GET['HDD'] : 0;
$sql = "SELECT * FROM devices WHERE option_hdd = '".$hdd."' AND option_fax = '".$fax."' AND option_finisher ='".$df."'";
Another option is to build your query. Change the name of the checkboxes like in the example below:
<form action="" method="get" name="search">
<input name="option[HDD]" type="checkbox" value="1" /> HDD<br />
<input name="option[Fax]" type="checkbox" value="1" /> Fax<br />
<input name="option[DF]" type="checkbox" value="1" /> DF<br />
...
<input name="Search" type="submit" />
</form>
And see the phpexample:
$sql = 'SELECT * FROM devices';
if(isset($_GET['option']) && count($_GET['option']) > 0){
$c = 0;
$sql .= ' WHERE';
foreach ($_GET['option'] AS $key => $option) {
$sql .= ($c == 0) ? ' option_'.$key.'='.$option : ' AND option_'.$key.'='.$option;
$c++;
}
}
Another solution is to use this trick:
<input name="HDD" type="hidden" value="0" />
<input name="HDD" type="checkbox" value="1" /> HDD<br />
This way if PHP will revert to default 0 if it doesn't get the value from checkbox.
And please be careful with your data, don't use $_GET directly in SQL.
I want to link searches from a checkbox form together. E.g i have 5 checkboxes (a,b,c,d,e) if i have checked a and b, i want to search depending on what i have searched i.e display two different results.
<form role="form" action="R.php" method="post">
<div class="form-group">
<input type="checkbox" value="a" name="search" />a
<input type="checkbox" value="b" name="search" />b
<input type="checkbox" value="c" name="search" />c
<input type="checkbox" value="d" name="search" />d
<input type="checkbox" value="e" name="search" />e
</div>
<input class="btn btn-default glyphicon-align-right" type="submit" value=">>"/>
</form>
The PHP
<?php $output='';
if(isset($_POST['search'])){
$searchq=$_POST['search'];
$searchq = preg_replace ("#[^0-9a-z]#i","",$searchq);
$Squery = mysql_query("SELECT * FROM database WHERE Name LIKE '%$searchq%'");
$count = mysql_num_rows ($Squery);
echo "<div id=results><ul class=list-group><div id=qwerty>";
if($count == 0){
$output = 'There was no results!';
}else{
while($row = mysql_fetch_array($Squery)){
$name = $row ['Name'];
$id = $row['ID'];
$category = $row['Category'];
$output .='<div class="container">'.$name.$category.' '.$id.'</div>';
echo "<li>"."<div style=height:95px ><h3 class=text-center style=text-transform:capitalize >".$name.' '."</h3></div>".
"<div>"."<a href=n.php><img src=images/$id.jpg alt=Image /></a>
</div>".
' '.$category.' RESULT!!!!!'.$id."</li>";
}
echo "</div></ul></div>";
}
}
?>
If I understand the question correctly, you would want to be able to search for all results that match the checkboxes that you have checked.
I'm not sure exactly what results you would be looking for, so instead I'll give you a few different options and let you take it from there.
First, in order to pass MORE THAN ONE checkbox to your PHP script, you need to define you checkboxes differently.
<form role="form" action="R.php" method="post">
<div class="form-group">
<input type="checkbox" value="a" name="search[]" />a
<input type="checkbox" value="b" name="search[]" />b
<input type="checkbox" value="c" name="search[]" />c
<input type="checkbox" value="d" name="search[]" />d
<input type="checkbox" value="e" name="search[]" />e
</div>
<input class="btn btn-default glyphicon-align-right" type="submit" value=">>"/>
Notice the brackets after "search" .. name = "search[]". This allows your checkboxes to pass over a COLLECTION of values, instead of just one.
Secondly, on your PHP side.
Once you submit your checkboxes, you will want to get them the same way you currently do:
$searches = $_POST['search'];
$searches will now be an array filled with the values that were checked in your form.
Depending on your desired result, what you do next would look something like this.
Let's take the case that A and B were checked.
If you want to get all results where the word begins with the letter A OR the word begins with the letter B:
<?php
$searches = $_POST['searches'];
$Squery = "SELECT * FROM database";
$Swhere = "";
//add all your individual searches by looping through the checkbox values
foreach($searches as $search){
$Swhere .= " OR name LIKE '%$search' ";
}
//remove the first OR case, because it is not needed for the first search query
$Swhere = ltrim($Swhere, ' OR');
//add your dynamic where statement to your query
$Squery .= $Swhere
$result = mysql_query($Squery);
//...... run the query and get the results the same way you are
By using the foreach function, your can add all the checkboxes you want to your search, but the query on the PHP side will not have to change at all.
On a similar note, in the case of A selected and B selected, if you want to get all words that begin with A, begin with B, OR begin with AB, then your code would be adjusted like so:
<?php
$searches = $_POST['searches'];
//this takes your array of values and combines them into one word.
//your searches variable remains an array, but your stringSearch is now
//a word with the combined values
$stringSearch = implode($searches,'');
$Squery = "SELECT * FROM database";
$Swhere = " name LIKE '%$stringSearch%' ";
//add all your individual searches by looping through the checkbox values
foreach($searches as $search){
$Swhere .= " OR name LIKE '%$search' ";
}
//add your dynamic where statement to your query
$Squery .= $Swhere
$result = mysql_query($Squery);
//...... run the query and get the results the same way you are
Hopefully you can take this example and adjust to what your actual needs are.
One thing to note, is you SHOULD NOT be using mysql_query. It is opening your code up to major SQL injection concerns. I only used it above to display how you would do it with your current code. Look into the mysqli functions
I have a html form where there are two Input fields
say Caste (options are SC, ST and OBC) and Direction (options are north, south, east and west). Users will choose the options using check box.
<input name="ct" type="checkbox" value="SC">SC
<input name="ct" type="checkbox" value="ST">ST
<input name="ct" type="checkbox" value="OBC">OBC
<input name="dr" type="checkbox" value="N">North
<input name="dr" type="checkbox" value="S">south
<input name="dr" type="checkbox" value="E">East
<input name="dr" type="checkbox" value="W">West
I have also a database (name : PEOPLE) with column name Caste , Direction etc.
Now I want to run a sql query based on user's selection. For example
mysql_query("select * from PEOPLE where Caste='option/options selected by user' and Direction= 'option/options selected by user' ")
If user choose one option from each field then it is not a problem for me,
mysql_query("select * from PEOPLE where Caste='$_POST[ct]' and Direction= '$_POST[dr]' ")
but if they use multiple options , then how should I proceed.
Give name attribute to the checkboxes like this
<input type="checkbox" name="caste[]" value="SC" />
<input type="checkbox" name="caste[]" value="ST" />
<input type="checkbox" name="caste[]" value="OBC" />
On you php side you can use function implode to form caste into a string as shown below (considering you are doing a POST)
$caste_id = implode(",",$_POST["caste"]);
Where you read from the database you can transform the value from db to an array like this
$caste_array = explode(",",$row->caste_id);
I hope this helps
You can use WHERE IN
$caste_string = implode('","', $_GET['ct']);
$caste_string = '"'.$caste.'"';
$dir_string = implode('","', $_GET['dr']);
$dir_string = '"'.$caste.'"';
mysql_query("select * from PEOPLE WHERE Caste IN ($caste_string) AND Direction IN ($dir_string)")
I want to have a form that allows the user to choose what data to display from a table through checking the checkboxes. If the user wants only 2 columns to be shown, should only 2 columns be shown. I have my codes, but after I submit, it displays nothing.Here's my code:
<form name="form1" method="post" action="view_emp.php">
<p>Select Option
<input type="checkbox" name="number[]" value="name" />Name
<input type="checkbox" name="number[]" value="hired" />Date Hired
<input type="checkbox" name="number[]" value="basic" />Basic Pay
<input type="checkbox" name="number[]" value="incentives">Incentives
</p>
<input type="submit" name="Submit" value="Submit">
</form>
here's my php:
<?php
$db = mysql_connect('localhost', 'root', '');
mysql_select_db('eis', $db) or die (mysql_error());
$employee = array();
foreach ($_POST['number'] as $employee) {
$number = mysql_real_escape_string($number);
$employee[] = "'{$number}'";
}
$sql = "select * from employees where type in (" .implode(", ", $number). ")";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
print $row['name'];
}
?>
i am a beginner in php and i need help from gurus and experts. thank you...
PHP's implode() and explode() functions might come in handy. You can easily turn your POST or GET attribute, 'number', into a comma-separated list by using implode($_POST['number']). This would be easy to store in one MySQL field, maybe a column in your user table.
If you want users to edit the form later, render the checkboxes with a loop and add a "checked" attribute to each checkbox element whose name exists in 'exploded' list (array) retrieved from your database.
This is basically serialization/deserialization. There are other ways to do it including serialize(), unserialize() or json_encode(), json_decode(). But since your data seems to be easily modeled as a basic list, you can keep it even simpler.