I'm making a website on which I have a set of data with checkbox. I need to delete multiple rows from database when I select multiple checkboxes. I don't know how to pass the id of the selected checkboxes to next page. Please help me.
Here is my code:
$select_qry="select * from data";
$result=mysql_query($select_qry);
$rows=mysql_num_rows($result);
if($rows>0)
{
?>
<form action="delete_submit.php" method="post">
<?php
for($i=0;$i<$rows;$i++)
{
$arr=mysql_fetch_assoc($result);
$id=$arr['id'];
$name=$arr['name'];
//echo $id;echo "<br>";echo $name;
?>
<input name="checkbox[]" type="checkbox" value="<?php echo $id;?>">
<?php
}
}
?>
How can I pass the selected checkboxes' id to the next page? Please help me.
Thanks
when form will be submitted then $_POST['checkbox'] will be set and contain the array of selected ids.
In your delete script
if(isset($_POST['checkbox']) && count($_POST['checkbox']) > 0){
$deleteIds = $_POST['checkbox']; // it will be an array
$sql = "DEFRE FROM tablename WHERE id in (".implode("," , $deleteIds).") ";
// run the query
}
Related
I want to enroll the student and insert the student id into Mysql database after I check and submit the checkbox value, but I already tried so many ways but still cannot...
This is the php code
<?php
if (isset($_POST['submitxd'])) {
foreach ($_POST['enrol'] as $items) {
$insert = $link->query("INSERT INTO student_course(studentID) values ('$items')");}
}
?>
This is the html code
$result = $link->query("SELECT * FROM student WHERE programmeName = '$programme' AND intake = '$intake'");
while ($row = mysqli_fetch_array($result)) {
echo "<tr>
<td>".$row['studentID']."</td>
<td>".$row['studentName']."</td>
<td>".$row['studentGender']."</td>
<td>".$row['studentContact']."</td>
<td>
<input type='checkbox' name='enrol[]' value='".$row['studentID']."'>
</td>
</tr>";
}
check whether your array contains values or not:
echo "<pre>";
print_r($_POST['enrol']);
echo "</pre>";
if not, you should write html code properly i.e. check form tag and its action path carefully and before submitting the form, remember to check out the checkbox
I would like to delete a row from an MySQL database. The row that I'd like to delete is displayed in a box, with each being obtained via a loop and SELECT statement.
I've already got the rows in the database being displayed accordingly; however, I'd like a button that once pressed, would delete the selected option from the database.
Here is my current code:
<form action="" method="post">
<label>Patient Name:</label>
<br><br>
<select name="patient" id="patient">
<?php
$conn = new mysqli("localhost", "root", "", "as2");
$result = $conn->query("SELECT patientID, patientName, address FROM patient ORDER BY patientName ASC");
while ($row = $result->fetch_assoc()){
$patientName = $row['patientName'];
$address = $row['address'];
echo "<option value=\"patient\">" .$patientName. ", ".$address."</option>";
}
?>
</select>
<input type="submit" name="delete" value="Delete Record">
</form>
How would I go about making the "Delete Record" button delete the selected option from the database?
first record (patientID) in a string
$patientID = $row['patientID'];
then Add $patientIDto (option value) so it become :
echo "<option value=".$patientID.">" .$patientName. ", ".$address."</option>";
then add this code After everything (ofc outside the "while" loop) :
<?php
$selected_patient = $_POST['patient'];
if( $_SERVER['REQUEST_METHOD'] === 'POST'
&& isset($_POST['delete']) && isset($_POST['patient']) ) {
if( !empty($_POST['patient']) ){
$patient_ID = mysql_real_escape_string($selected_patient);
if ( $conn->query("DELETE FROM patient WHERE patientID={$patient_ID}") )
echo "user has been deleted successfully";
else
echo "Error deleting";
}
}
?>
now you'r good to go , after click delete button refresh your page and Boom! , the user will Disappears
and if you want a real time Action u can use (Ajax)
Try this
while ($row = $result->fetch_assoc()){
$patientName = $row['patientName'];
$address = $row['address'];
$patientID = $row['patientID']; //get patient id
echo "<option value=\"$patientID\">" .$patientName. ", ".$address."</option>"; // change in option value
}
Now on form submit, you will get patient id in $_POST['patient'] , can write your delete query.
Hope this will hope.
I'm updating some countries values into db table. All countries fetch from TBL_COUNTRY table. Then few countries store to another table. I'm using implode function to store multiple values. it works fine. it stored like this in my db table Afghanistan,Argentina,Austria,Bangladesh.
I have tried this code
<?php
$exp_str = explode(',', $model_availability);
foreach($exp_str as $get_str)
{
echo $get_str;
}
?>
This above code return this output AfghanistanArgentinaAustriaBangladesh
How do I put tick on the checkbox based on this value?
<?php
$sql = "SELECT * FROM ".TBL_COUNTRY." ORDER BY country_name ASC";
$exe = mysql_query($sql, $CN);
while($r = mysql_fetch_array($exe))
{
?>
<input type="checkbox" name="model_availability[]" value="<?=$r['country_name']?>" id="<?=$r['country_name']?>" />
<label for="<?=$r['country_name']?>"><?=$r['country_name']?></label>
<?php } ?>
<input type="checkbox" name="model_availability[]" value="<?=$r['country_name']?>" id="<?=$r['country_name']?>"<?=(in_array($r['country_name'],$model_availability)?" checked":"")?> />
//In the input box just add a checked attribute, you will get.
" id="" checked = "true" />
i've tried many solutions from the website, but noone give me the result.
I have a combobox, and i insert data from mysql db
after pressing "submit button", the combobox refresh, but restart from first data and not from selected,
hereunder my code,
can someone help me?
$result = mysql_query("SELECT * FROM courier");
while($row = mysql_fetch_array($result))
{
echo "<option ". (($_POST['NAZIONE'] == $row["NAZIONE"]) ? 'selected ' : '') ."value=\"".$row["NAZIONE"]."\">".$row["NAZIONE"]."</option>";
}
echo'</select> <br> <input type="submit" value="Proceed">';
mysql_close($con);
?>
you can set selected value to $_SESSION variable and then you can get that selected value
I can call the values of the drop down box from the database, but I can't save the selected value in a new row within the database. Can someone please tell me where I'm going wrong?
if (isset($_POST["addDestination"]) && $_POST["addDestination"]=="yes")
{
$countriesName=$_POST["countriesName"];
$Name=$_POST["Name"];
$photo=$_POST["photo"];
$description=$_POST["description"];
$Airport=$_POST["Airport"];
$Airport2=$_POST["Airport2"];
$Airline=$_POST["Airline"];
$Airline2=$_POST["Airline2"];
$Airline3=$_POST["Airline3"];
$Airline4=$_POST["Airline4"];
$dbQuery= "INSERT INTO destinations VALUES( NULL, '$countriesName', '$Name', '$photo', '$description', '$Airport', '$Airport2', '$Airline', '$Airline2', '$Airline3', '$Airline4')";
$result= mysql_query($dbQuery,$db);
The html code and php code that call the values in from the database is:
<form id="addDestination" name="addDestination" method="post" action="addDestination.php">
<input type="hidden" name="addDestination" value="yes">
<div id="countriesName" class="info"><span class="formLabel">Country (Please select one of the following)</span><br>
<?php
echo "<p></p>";
$dbQuery="SELECT Name FROM countries order by name asc";
$dbResult=mysql_query($dbQuery);
echo "<select name=\"Name\">";
while ($dbRow=mysql_fetch_array($dbResult)) {
$Name=$dbRow["Name"];
echo "<option value='$Name'>$Name</option>";
}
echo "</select>";
echo "<p> </p>";
?>
</div>
When I try to save a selected value, the field in the database stays blank.
I think this might be that rather than:
$countriesName=$_POST["countriesName"];
you may need to use:
$countriesName=$_POST["Name"];
"Name" is the name of your actual HTML select, rather than "countriesName" which is the containing div.