How to use checkbox to delete data from database after submit [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
So I am making this website and i got some data in my database. The data is read automatically with php into a table. Every table row also gets automatically a checkbox. Now I want to check the checkbox and then press delete and it deletes the checked data out of the database. Any ideas how?
Here is my code:
<table width="600" border="1" cellpadding="1" cellspacing="1">
<tr>
<th>Username</th>
<th>Paswoord</th>
<th>Delete</th>
</tr>
<?php
while($user = mysql_fetch_assoc($records)){
echo "<tr>";
echo "<td>".$user['UserName']."</td>";
echo "<td>".$user['Pass']."</td>";
echo "<td> <input type='checkbox' name='checkbox' value='checked'</td>";
echo "</tr>";
}
?>
</table>
<form method= "POST" action="deleteuser.php">
<input type="submit" name="delete" value="DELETE USERS">
</form>
this is the code in my delete user.php
$checked = $_POST['checkbox'];
if($_POST['checkbox'] == "checked"){
echo "SUCCEEEEEEES";
}

There are couple of things to be aware of;
The syntax and proper way of writing the <form> tags.
Use the <input> fields inside the <form>.
Pass an array of checkbox[] instead of a single checkbox.
Your Code
<form method= "POST" action="deleteuser.php">
<table width="600" border="1" cellpadding="1" cellspacing="1">
<tr>
<th>Username</th>
<th>Paswoord</th>
<th>Delete</th>
</tr>
<?php
while($user = mysql_fetch_assoc($records)){
echo "<tr>";
echo "<td>".$user['UserName']."</td>";
echo "<td>".$user['Pass']."</td>";
echo "<td> <input type='checkbox' name='checkbox[]' value='checked'</td>";
echo "</tr>";
}
?>
</table>
<input type="submit" name="delete" value="DELETE USERS">
</form>
PHP
Instead of using this;
if($_POST['checkbox'] == "checked"){
echo "SUCCEEEEEEES";
}
?>
Use this;
<?php
foreach($_POST['checkbox'] as $val)
{
echo $val . " this should be deleted";
}
?>

Well this is what I always do :-
Make these changes to your while loop.
<?php
$c=0;
while($user = mysql_fetch_assoc($records)){
$c++
echo "<tr>";
echo "<td>".$user['UserName']."</td>";
echo "<td>".$user['Pass']."</td>";
echo "<td> <input type='checkbox' name='checkbox-$c' value='checked'</td>";
echo "</tr>";
}
echo "<input type='hidden' name='total' value='$c'>";
?>
deleteuser.php (Access all the checkboxes here via a for loop)
<?php
for($i=1;$i<=$_POST['total'];$i++)
{
if($_POST['checkbox-$i'] == "checked")
{
//commands for delete
}
}
?>

Rename the name of the checkbox from checkbox to checkbox[]. Now you get an Array instead a string.
And then set value-attribute to the ID of you row in the table (Primary Key or whatelse you need to delete them)
echo "<td> <input type='checkbox' name='checkbox[]' value='".$user["UserID"]."'</td>";
Now you can iterate them in PHP
foreach ($_POST["checkbox"] as $value){
// ..Delete FROM ... where userID = $value
}

Related

how to send a selected database value that is inside loop to another page in php

i have made a form in php and inside form there is a loop that extracts multiple values from database(img,name,availability,..) for multiple books i have made a table to display those values to user and after displaying these data in table i have made an issue button inside loop so that every book has its issue button.
My problem is that i have to only retrieve id of that book for which user click issue button. i tried storing it in cookie but it send the id of the first book displayed then i tried get method but that results in sending the last book that is displayed on screen id. but i want is that it should send the id of book which is selected by user
display books
echo "<form action='issue.php' method='get'>";
while ($row= mysqli_fetch_array($result)) {
echo "<div id='img_div' style='background-color:#fff;'> ";
echo "<img src='books/".$row['image']."'>";
// echo "</div>";<div id='text'>
$isbn=$row['isbn'];
echo "<input type='hidden' name='isb' value='$isbn' />";
echo " <table>";
echo "<tr><td> NAME</td><td> ".$row['name']."</td></tr>";
echo "<tr><td> AVAILABILITY</td><td> ".$row['availabilty']."</td></tr>";
echo "<tr><td> CATEGORY</td><td> ".$row['category']."</td></tr>";
echo "<tr><td colspan='2'>
<button type='submit' name='issue'>issue</button></td></tr>";
echo "</table>";
echo "</div><br/>";
if (isset($_GET['issue'])) {
# code...
$bookid=$isbn;
setcookie("bid",$bookid);
if(!isset($_COOKIE['bid'])){
echo "COOKIE NOT SET";
}
else{
echo "COOKIE SET SUCCESSFULLY";
}
}
issue.php(in which i want to send id)
if(isset($_GET['issue'])){
$bookid=$_GET['isb'];
$dbser="localhost";
$use="[redacted]";
$pasw="[redacted]";
$db="[redacted]";
$con=mysqli_connect($dbser,$use,$pasw,$db);
mysqli_select_db($con,$db)or die("db not connected");
$userid=$_COOKIE['id'];
$id=$_SESSION['user']['username'];
$query = "select id from user_account where username='$id'";
$result=mysqli_query($con,$query);
$row= mysqli_fetch_assoc($result);
$uid=$row['id'];
echo "$uid";
echo "<br/>";
echo "$bookid";
$query = "INSERT INTO issue (bookid, userid)
VALUES ('$bookid', '$uid')";
mysqli_query($con, $query)or die(mysqli_error($con));
What you need is a way to select a single row then submit that with the form. That can be accomplished by adding a Radio button to your table inside the form. The user will check the radio button for the item they want then click the submit button.
Here is an example of what that code could look like for your page.
display books
<form action='issue.php' method='get'>
while ($row= mysqli_fetch_array($result)) {
$isbn=$row['isbn'];
echo " <table>";
echo "<tr>";
echo "<td><input type=\"radio\" name=\"optradio\" value=\"".$isbn."\"></td>";
echo "<td> NAME</td><td> ".$row['name']."</td>";
echo "<td> AVAILABILITY</td><td> ".$row['availabilty']."</td>";
echo "<td> CATEGORY</td><td> ".$row['category']."</td>";
echo "</tr>";
echo "</table>";
}
echo "<button type='submit' name='issue'>issue</button>";
echo "</form>";
Here is a HTML snippet so you can see what that PHP code would output in HTML. Click "Run snippet code" below to see the preview.
<form action='issue.php' method='get'>
<table>
<tr>
<td><input type="radio" name="optradio" value="1"></td>
<td> NAME</td>
<td> name1</td>
<td> AVAILABILITY</td>
<td> availability1</td>
<td> CATEGORY</td>
<td> category1</td>
</tr>
<tr>
<td><input type="radio" name="optradio" value="2"></td>
<td> NAME</td>
<td> name2</td>
<td> AVAILABILITY</td>
<td> availability2</td>
<td> CATEGORY</td>
<td> category2</td>
</tr>
</table>
<button type='submit' name='issue'>issue</button>
</form>
Then in issue.php you would look for $_GET['optradio'] to get the selected value.
if(isset($_GET['issue'])){
$bookid=$_GET['optradio'];
$dbser="localhost";
...
...
...

Php form array not passing values to other pages

I am working on a project where I want users to choose their space for parking and book it. I have a MySQL database which holds information about parking slots. I am fetching those values and display it using table and form. I have put checkboxes to make choice. and once they make choice they should be directed to payment page.
I am having problem with checkbox. I can see that in value fields it has values from database but when I hit submit button it doesn't pass any values to next page.
below is my code
<body>
<form method="POST" action="book.php">
<?php
//we create a table
echo "<table>";
// create table th
echo "<tr > <th> Parking Slot No </th> <th> Status </th>";
$sql=" select ParkingSlotNo,Status from fleming_dwing ";
$st=$conn->prepare($sql);
$st->execute();
$total=$st->rowCount();//get the number of rows returned
if($total < 1 ){//if no row was returned
echo "<tr> <td style> No Data: DataBase Empty </td> ";//print out error message
echo "<td> No Data: DataBase Empty </td> ";//print out error message
$ing = "<img src='img/occupied.png'/>" ;
}
else{
while($res = $st->fetchObject()){//loop through the returned rows
echo "<tr>";
if($res->ParkingSlotNo and $res->Status=='OCCUPIED')
{echo "<td> $res->ParkingSlotNo </td> ";
echo "<td> <img src='img/occupied.png'/> </td>";
echo"<td><a href=''> </a> </td>";
}
else
{
echo "<td> $res->ParkingSlotNo </td> ";
echo "<td> <img src='img/vacant.png'> </td>";
echo"<td><input type='checkbox' value='$res->ParkingSlotNo'></td>";
}
echo"</tr>";
}
}
?>
</table>
<input type="submit" value="Submit">
</form>
</body>
</html>
and this is the code for booking page
<?php
require_once("dbconfigpdo.php");
print_r($_POST);
?>
The checkboxes do not have name attributes. A form control can't be successful (included in the name=value pairs of data that are submitted) without one.
Any input must have a name attribute.
By name you can use a value. So, you need to add name="your name" to your checkboxes.
You need to set an attribute name to your input field, or it will not be processed.
Something like:
<input name='parkingslot' type='checkbox' value='$res->ParkingSlotNo'>

Submit button inside a while loop doesnt work

I need a help to make the submit button inside the while loop work. I design a code that fetch the value from DB and user has to approve or reject the pulled value. So when user click OK, then the DB should be updated with a OK value. I dont know where is the problem is that my OK button doesnt work,
When user click OK, then it should go to approval.php page
<?php
if ($_POST['action'] == 'show'){
$requestCompSql = "SELECT REQUEST_COMPONENT_CUTTING.PROJECT_NAME,
REQUEST_COMPONENT_CUTTING.BASE_PLATE,
REQUEST_COMPONENT_CUTTING.THICKNESS,
REQUEST_COMPONENT_CUTTING.QTY_REQUESTED,
REQUEST_COMPONENT_CUTTING.REQUESTER,
REQUEST_COMPONENT_CUTTING.REQUEST_DATE
FROM REQUEST_COMPONENT_CUTTING
WHERE REQUEST_COMPONENT_CUTTING.BASE_PLATE = '{$_POST["bp"]}'";
$requestCompParse = oci_parse($conn, $requestCompSql);
oci_execute($requestCompParse);
while($row = oci_fetch_assoc($requestCompParse)){
echo "<form action='approval.php'>";
echo "<div class='table-responsive'>";
echo "<table class='table table-bordered'>";
echo '<table cellspacing = "0"';
echo '<thead>';
echo '<tr>
<th>PROJECT</th>
<th>BASEPLATE</th>
<th>THICKNESS</th>
<th>QTY REQUESTED</th>
<th>REQUESTER</th>
<th>REQ. DATE</th>
<th align="center">ACTION</th>
</tr>
</thead>';
echo "<tbody>";
echo "<tr class='warning'><td>$row[PROJECT_NAME]</td>";
echo "<td>$row[BASE_PLATE]</td>";
echo "<td>$row[THICKNESS]</td>";
echo "<td>$row[QTY_REQUESTED]</td>";
echo "<td>$row[REQUESTER]</td>";
echo "<td>$row[REQUEST_DATE]</td>";
echo "<td><input type='button' value='OK' class='btn btn-success'>
<input type='button' value='REJECT' class='btn btn-danger'></td>";
echo "</tr>";
echo "</tbody>";
echo "<table cellspacing = '0'";
echo "</form>";
echo "</div>";
}
}
?>
For a submit button you need to use
<input type="submit" value="OK">
You used
<input type="button" value="OK">
which can be used for javascript execution. Same for the reject button. You could however send the form with javascript (which is not recommended, as some choose to turn of javascript) by using something like this:
<input type="button" value="OK" onClick="document.forms[0].submit()">
The button needs to be of type="submit" and not only of type="button"
echo "<td><input type='submit' value='OK' class='btn btn-success'>
You have some problems in your code. Firstly if you are posting data through form, then there should be method property for the form like:
echo "<form action='approval.php' method='post'>";
Secondly, if you are submitting the form on clicking the button, then the type of button should be submit instead of button.
<input type='submit' value='OK' class='btn btn-success'>
Since you are using type='button' you should add Javascript to submit the form such as onclick='submit();'

How do I create an html form with multiple checkboxes

This is my code for creating an html form that reads from a database and will allow the user to check and uncheck boxes for each of the 640 items. This is the form.php:
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
// see if any rows were returned
if (mysql_num_rows($result) > 0) {
// yes
// print them one after another
echo "<html><body> <table cellpadding=10 border=1>";
while($row = mysql_fetch_assoc($result)) {
echo "<tr>";
echo "<td>".$row['stickerID']."</td>";
echo "<td>" .$row['stickerName']."</td>";
echo "<td>".$row['stickerSection']."</td>";
echo "<td>"?>
<form name="some form" action="editform.php" method="post">
<input type="checkbox" name="<?php echo $row['stickerID'] ?>" value=" <?php echo $row['stickerStatus'] ?> ">
<?php "</td>";
echo "</tr>";
}
echo "</table></body></html>";
echo " " ?>
<input type="submit" name="editWish" value="Edit">
</form>
<?php " ";
} else {
// no
// print status message
echo "No rows found!";
}
The user must then be able to click on submit and have those values updated in the mysql database.
Right now when I click the submit button, it posts to edit form.php which has this:
<?php
//echo results
foreach($_POST['stickerID'] as $k=>$v ){
echo $k;
echo $v;
}
?>
But I don't get anything echoed. I was thinking the problem could be that Im actually creating a form for every row instead of 1 form with many rows/checkboxes. But when I move the form code after the and the tag to the line where line, I can't even load the form.php, it just loads blank.
Where is my problem? :) Thx
Name your checkbox like this:
<input type="checkbox" name="stickerID[]" value=" <?php echo $row['stickerStatus']; ?> ">
And as Amal already said update your code to PDO or MySQLi
you can do this with a tag :-
echo "<td>" .$row['stickerName']."</td>";
echo "<td>".$row['stickerSection']."</td>";
echo "<td>"?>
<form name="some form" action="editform.php" method="post">
<input type="checkbox" name="checkbox[]" value=" <?php echo $row['stickerStatus'] ?> ">
<?php "</td>";
echo "</tr>";
on your php code you get :-
$all_checkes_checkbox = $_POST['checkbox'];
here is your all checked checkbox:-
and this array also bale key and value

different array for different input text

hi i want to store value of each input text in different arrays how to do that
for example store value of input text 1 in array 1 and value of input text 2 in array 2 and so on how to achieve that
here is the code for print input text
for($r=1;$r<=10;$r++)
{
echo"<form id='ponts'>
<table>
<tr>
<td>Enter point number$r</td><td> <input type='text' id='pt$r' name='pt$r' pattern='[0-9.]+'/></td>
</tr>
</table>
</form>";
}
I guess I didn't understand well, but the following script may be what you want.
<?php
$g=$_GET;
if( isset($g['pt']) ){
// the form has been submitted.
$ptValues=$g['pt'];
print_r($ptValues);
}
echo "<form id='ponts'><table>";
for($r=1;$r<=10;$r++)
{
echo "<tr><td> Enter point number$r</td><td> <input type='text' id='pt$r' name='pt[]' pattern='[0-9.]+'/> </td></tr>";
}
echo "</table></form>";
?>
Maybe this:
<?php
$g=$_GET;
if( isset($g['pt0']) ){
// the form has been submitted.
$ptValues=array();
for($i=0; isset($g['pt'.$i]); $i++ )
$ptValues[]=$g['pt'.$i];
print_r($ptValues);
}
echo "<form id='ponts'><table>'";
for($r=1;$r<=10;$r++)
{
echo "<tr><td> Enter point number$r</td><td> <input type='text' id='pt$r' name='pt$r' pattern='[0-9.]+'/> </td></tr>";
}
echo "</table></form>";
?>
maybe this is what u are looking for
echo"<table id='points'>";
for($r=1;$r<=10;$r++)
{
echo"
<tr>
<td>Enter point number".$r."</td><td> <input type='text' id='pt".$r."' name='pt".$r."' pattern='[0-9.]+'/></td>
</tr>
";
}
echo "</table>";
i dont know why you are using the form tags here .

Categories