delete using checkbox code not working properly [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Code not working properly always shows the message fail.
function delete(){
$con = mysqli_connect("localhost","root","","rishita_db");
$sql="select * from 14_patientdetails";
$result=mysqli_query($con,$sql);
?>
<form method="post" action="">
<center>
<h1><u>Patient Details</u></h1>
<table border="1" style="font-family:Georgia;color:#800000;font-style:bold;">
<tr style="font-family:Georgia;color:green;font-style:bold;">
<th>#</th>
<th>Patient ID</th>
<th>Patient Name</th>
<th>DOB</th>
<th>Gender</th>
<th>Address</th>
<th>Phone No.</th>
<th>Medicare</th>
<th>Doctor Associated</th>
</tr>
<form method="post" action="">
<?php
while($row=mysqli_fetch_array($result))
{
$r=$row['patientId'];
?>
<tr>
<td><input type='checkbox' name='checkbox[]' id="checkbox" value=<?php echo $r; ?>></td>
<td><?php echo $row['patientId']; ?></td>
<td><?php echo $row['patientName']; ?></td>
<td><?php echo $row['DOB']; ?></td>
<td><?php echo $row['Gender']; ?></td>
<td><?php echo $row['Address']; ?></td>
<td><?php echo $row['Phone']; ?></td>
<td><?php echo $row['Medicare']; ?></td>
<td><?php echo $row['Doctor']; ?></td>
</tr>
<?php
}
?>
</table>
<table>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="del" type="submit" id="del" value="Delete"></td>
</tr>
</table>
</form>
<?php
if(isset($_POST["del"]))
{
if(isset($_POST["checkbox"]))
echo 'Enter';
$chk = isset($_POST['checkbox']);
$chkcount = count($chk);
for($i=0;$i<$chkcount;$i++){
$del=$chk[$i];
$sql1 = "DELETE FROM 14_patientdetails WHERE id='$del'";
$q = mysqli_query($con,$sql1);
}
if($q){
echo "Success";
}else{
echo 'Fail';
}
}
}

This is wrong:
$chk = isset($_POST['checkbox']);
$chkcount = count($chk);
for($i=0;$i<$chkcount;$i++){
$del=$chk[$i];
Now there are one too many mistakes, multiple <form> tags and a single closing </form>
I couldn't not edit your code on my phone so I will suggest the way to get along. I'm pretty sure you'll love it when you do it yourself.
Make a <td><input type = "checkbox" name = "checkbox" value = "<?php echo $r; ?>"/>Proceed</td> inside a while-loop something similar to your code while($row = mysqli_fetch_array($result))
In the end of the <form> make a <button> and redirect user to another page probably delete.php. Now, check its set;
if(isset($_POST['checkbox']
{
//foreach loop for your query
foreach($_POST['checkbox'] as $val)
{
// check what you're getting..
echo $val;
}}
You could also use a simple for-loop
If you want:
for($i = 0; $i <count($_POST['checkbox']; $i++)
{
// do your stuff..
}
Bottom line: try to differentiate your forms, queries and make a single loop to read from database and assign the values at the same time to your checkboxes.
This is what I have understood and tried to write from my phone,
please study mysqli/PDO to prevent SQL Injection/XSS.

Related

I want the php results to be displayed on the same page instead of a completely new page

I'm trying to search by name and display the results on the same page.
My php code and the html code are in 2 separate files. Given below is my search.php code.
<?php
include_once '../connection.php';
include_once '../session.php';
$output = '';
if(isset($_POST['search'])){
$searchquery=$_POST['search'];
$result = mysqli_query($conn,"SELECT * FROM details where customername LIKE '%$searchquery'");
$count = mysqli_num_rows($result);
if($count == 0) {
$output = 'There was no search result!';
}else{
while($row = mysqli_fetch_array($result)) {
$ID = $row['id'];
$Name= $row['customername'];
$Type = $row['type'];
$Phone = $row['phoneno'];
$Email = $row['email'];
$output .= '<div>' .$Name.' '.$Type.' '.$Phone.' '.$Email.'<div>';
}
}
}
?>
<?php print("$output");?>
Given below is the html code for my form.
<form action="search.php" method="POST" >
<center><input type="text" name="search" placeholder="Search by name" >
<input type="submit" value="Search"></center>
<br>
<table id="myTable">
<tr>
<th>ID</th>
<th>Name</th>
<th>Type</th>
<th>Telephone Number</th>
<th>Email</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["customername"]; ?></td>
<td><?php echo $row["type"]; ?></td>
<td><?php echo $row["phoneno"]; ?></td>
<td><?php echo $row["email"]; ?></td>
When I search, the results are displayed on a completely new page. But I want the results to be displayed on the same page with all the existing page layout, headers and footers.
How can I achieve this?
Thank in Advance!
Assuming your first file name is index.php.
Then need to call this file on form action & little bit refactor a file. Now it have $output array which is empty. If it contains search parameter, it will fill data to $output array and then on html part it checks if $output array has data to display it.
<?php
include_once '../connection.php';
include_once '../session.php';
$output = array();
if(isset($_POST['search'])){
$searchquery=$_POST['search'];
$result = mysqli_query($conn,"SELECT * FROM details where customername LIKE '%$searchquery'");
$count = mysqli_num_rows($result);
while($row = mysqli_fetch_array($result)) {
$output[] = $row;
}
}
?>
<!DOCTYPE html>
<html>
<body>
<form action="index.php" method="POST" >
<center><input type="text" name="search" placeholder="Search by name" >
<input type="submit" value="Search"></center>
<br>
<?php if(count($output) > 0): ?>
<table id="myTable">
<tr>
<th>ID</th>
<th>Name</th>
<th>Type</th>
<th>Telephone Number</th>
<th>Email</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<?php foreach($output as $row): ?>
<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["customername"]; ?></td>
<td><?php echo $row["type"]; ?></td>
<td><?php echo $row["phoneno"]; ?></td>
<td><?php echo $row["email"]; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
</form>
</body>
</html>

Using PHP to get data from MSQL and need a button in the foreach table

Let me post some code first so I can explain more of what I want to accomplish.
<?php
require "../config.php";
require "../common.php";
if (isset($_POST['submit'])) {
if (!hash_equals($_SESSION['csrf'], $_POST['csrf'])) die();
try {
$connection = new PDO($dsn, $username, $password, $options);
$sql = "SELECT * FROM saledata WHERE itemnumber = :itemnumber";
$itemnumber = $_POST['itemnumber'];
$statement = $connection->prepare($sql);
$statement->bindParam(':itemnumber', $itemnumber, PDO::PARAM_STR);
$statement->execute();
$result = $statement->fetchAll();
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
}
?>
<?php require "templates/header.php"; ?>
<?php
if (isset($_POST['submit'])) {
if ($result && $statement->rowCount() > 0) { ?>
<h2>Results</h2>
<style>
table, th, td {
border: 1px solid black;
}
</style>
<table>
<thead>
<tr>
<th>Item Number</th>
<th>Sale Number</th>
<th>Lot Number</th>
<th>Item Description</th>
<th>Seller Number</th>
<th>Buyer Number</th>
<th>Sold</th>
<th>Sold</th>
<th>Paid</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<?php foreach ($result as $row) : ?>
<tr>
<td><?php echo escape($row["itemnumber"]); ?></td>
<td><?php echo escape($row["salenumber"]); ?></td>
<td><?php echo escape($row["lotnumber"]); ?></td>
<td><?php echo escape($row["itemdescription"]); ?></td>
<td><?php echo escape($row["sellernumber"]); ?></td>
<td><?php echo escape($row["buyernumber"]); ?></td>
<td><?php echo escape($row["sold"]); ?></td>
<td><?php echo escape($row["paid"]); ?></td>
<td><?php echo escape($row["amount"]); ?></td>
<td><?php echo escape($row["date"]); ?> </td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php } else { ?>
<blockquote>No results found for <?php echo escape($_POST['itemnumber']); ?>.</blockquote>
<?php }
} ?>
<h2>Find item based on Item Number</h2>
<form method="post">
<input name="csrf" type="hidden" value="<?php echo escape($_SESSION['csrf']); ?>">
<label for="itemnumber">Item Number</label>
<input type="text" id="itemnumber" name="itemnumber"><br><br>
<input type="submit" name="submit" value="View Results">
</form>
<?php require "templates/footer.php"; ?>
(END)
This produces an HTML table containing the data in the database in rows. What I would like is a button at the beginning or end of the rows that will modify a field in that item's row. An example is the column for sold or not sold. What I would like is a button at either the beginning or the end of each row that modifies the "sold" column from N to Y for that item. I have tried to put a block of code in the code here at the end but I can't get it to do anything. I can get the button to show using the HTML tag but can't get it to actually do anything.
Here is the concept of the foreach:
<td><?php echo escape($row["sold"]); ?></td>
<td><?php echo escape($row["paid"]); ?></td>
<td><?php echo escape($row["amount"]); ?></td>
<td><?php echo escape($row["date"]); ?> </td>
<td>BUTTON and code to change Sold from N to Y</td>
</tr>
Any assistance would be greatly appreciated!
I know my stuff is probably elementary and not very efficient, but I have been working on this project for several weeks and never worked with a MYSQL database before.

How to update data from html table to mysql using php

I have 4 columns in sqltable.
How can I update solution cell value to SQL table. Need to update value based on checkbox value as userid.
<?php
$result = mysql_query("SELECT * FROM `tbl_query`);
?>
<table id="t01" >
<thead>
<tr>
<th>User Id</th>
<th>Query</th>
<th>Solution</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<?php
while( $row = mysql_fetch_assoc( $result ) ){
?>
<tr>
<td><?php echo $row['userid']; ?></td>
<td><?php eco $row['userquery']; ?></td>
<td contenteditable='true'><?php echo $row['solution']; ?></td>
<td><?php $row['querydate']; ?></td>
<td style='display:none;'><input type='checkbox' id='<?php echo $row['userid'];?>' name='check_list[]' value='<?php echo $row['userid']; ?>' checked /></td>
</tr>
<?php
}
?>
</tbody>
</table>
first of all, you should not use mysqlanymore since its deprecated. Use mysqli or pdo instead.
second, you are using double-quote at the beginning and single-quote at the end of your query.
and third: you forgot the singlequot in the [], thats not how you access an item from the array, you need $row['userid']

How would I be able to add an Edit to the table at the end of the table

How would I be able to add an Edit to the table after the table.
Any help would be greatly appreciated. I've tried a number of variations such as : Trying to add Edit
<center><?php
//connect to mysql for search
if(isset($_POST['search']))
{
$valueToSearch = $_POST['valueToSearch'];
// search in all table columns
// using concat mysql function
$query = "SELECT * FROM `2016server` WHERE CONCAT(`ServerGroup`,
`Week`, `Status`, `ServerName`, `IP`, `DateComplete`, `DateSched`,
`HeatTicket`) LIKE '%".$valueToSearch."%'";
$search_result = filterTable($query);
}
else {
$query = "SELECT * FROM `2016server`";
$search_result = filterTable($query);
}
// function to connect and execute the query
function filterTable($query)
{
$connect = mysqli_connect("localhost", "root", "", "");
$filter_Result = mysqli_query($connect, $query);
return $filter_Result;
}
?>
//start the webpage
<html>
<head>
<title>SEARCH 2016 Servers</title>
</head>
<body>
<center><h3>Type in your query to search all of the 2016
Servers</h3><br>
<form action="search2016.php" method="post">
<input type="text" name="valueToSearch" placeholder="SEARCH">
<input type="submit" name="search" value="GO!"><br><br>
table
<table>
<tr>
<th>ServerGroup</th>
<th>Server Name</th>
<th>Date Scheduled</th>
<th>Heat Ticket</th>
<th>Status</th>
<th>Date Complete</th>
<th>Week</th>
<th>Downtime</th>
<th>IP Address</th>
<th>Operating System</th>
</tr>
populate table from mysql database
<?php while($row = mysqli_fetch_array($search_result)):?>
<tr>
<td><?php echo $row['ServerGroup'];?></td>
<td><?php echo $row['ServerName'];?></td>
<td><?php echo $row['DateSched'];?></td>
<td><?php echo $row['HeatTicket'];?></td>
<td><?php echo $row['Status'];?></td>
<td><?php echo $row['DateComplete'];?></td>
<td><?php echo $row['Week'];?></td>
<td><?php echo $row['DT'];?></td>
<td><?php echo $row['IP'];?></td>
<td><?php echo $row['os'];?></td>
This is the section that I'm trying to add the edit to in the table. From the edit code above, it's trying to go to the 2016edit.php page with the ID how ever since there is no such page, it gets a 404 error.
</tr>
<?php endwhile;?>
</table>
</form>
</body>
</html>
close the first <form tag early (after the <input type="submit")
add new column named action where you'll put your edit button
use this for the edit
<form action="youreditform.php" method="post">
<button type="submit" name="edit" value="<?= $theidofthis; ?>">Edit</button>
</form>
Add another "td" tag and make it a link and it will go to the next edit page on click. but for editing purpose you have to post id in the url to check that which record was clicked to edit. hope it will be helpful.
<tr>
<td><?php echo $row['ServerGroup'];?></td>
<td><?php echo $row['ServerName'];?></td>
<td><?php echo $row['DateSched'];?></td>
<td><?php echo $row['HeatTicket'];?></td>
<td><?php echo $row['Status'];?></td>
<td><?php echo $row['DateComplete'];?></td>
<td><?php echo $row['Week'];?></td>
<td><?php echo $row['DT'];?></td>
<td><?php echo $row['IP'];?></td>
<td><?php echo $row['os'];?></td>
<td><a href='edit.php'>edit</a></td>
</tr>

$_POST variable is always the same

I'm trying to get a variable from a link (invisible button, basically), however, when I get to the redirected page, it's always the same $_POST variable it shows. I've checked if it's set before or after the for loop, and it's not. I've also printed out the values in the for loop, and they're different, as they should be. I've also tried unsetting the variable at the bottom of the redirect page after use. I'm totally out of ideas now... what to do?
<form method="post" action="http://localhost:8888/wordpress/job-openings/view-job/">
<table>
<tr>
<th>Job ID</th>
<th>Position</th>
<th>Client</th>
<th>Recruiter</th>
</tr>
<? for($i = 0; $i < count($rowArray); $i++) { ?>
<? $positionStatus = $data->response->result->JobOpenings->row[$i]->FL[8]->content;
$recruitmentResponsible = $data->response->result->JobOpenings->row[$i]->FL[7]->content;
$recruitmentResponsibleTweak = current(explode("(", $recruitmentResponsible));
$job_opening_id = $data->response->result->JobOpenings->row[$i]->FL[0]->content;
$request_url = 'http://recruit.zoho.com/ats/private/json/JobOpenings/getRecordById?authtoken=xxx&scope=recruitapi&id=' . $job_opening_id;
if ($positionStatus == 'In-progress') {
?>
<tr>
<td>
<input type="hidden" name="job_id" value="<?php echo $request_url ?>" />
<button id="jobopening-link">
<? echo $data->response->result->JobOpenings->row[$i]->FL[4]->content; ?>
</button>
</td>
<td><? echo $data->response->result->JobOpenings->row[$i]->FL[5]->content; ?></td>
<td><? echo $data->response->result->JobOpenings->row[$i]->FL[6]->content; ?></td>
<td><? echo $recruitmentResponsibleTweak ?></td>
</tr>
<? }
} ?>
</table>
</form>
First, there is no reference to $_POST in your code. Second, the reason why you get always the same value is because you are always setting the same exactly name to the hidden input, and when the form is sent you will get only the first input with that name. My suggestion, is to move the form tag inside the for loop, that way you will create multiple forms and each one will have the submit button and the proper value in the hidden input:
<table>
<tr>
<th>Job ID</th>
<th>Position</th>
<th>Client</th>
<th>Recruiter</th>
</tr>
<? for($i = 0; $i < count($rowArray); $i++) { ?>
<form method="post" action="http://localhost:8888/wordpress/job-openings/view-job/">
<? $positionStatus = $data->response->result->JobOpenings->row[$i]->FL[8]->content;
$recruitmentResponsible = $data->response->result->JobOpenings->row[$i]->FL[7]->content;
$recruitmentResponsibleTweak = current(explode("(", $recruitmentResponsible));
$job_opening_id = $data->response->result->JobOpenings->row[$i]->FL[0]->content;
$request_url = 'http://recruit.zoho.com/ats/private/json/JobOpenings/getRecordById?authtoken=xxx&scope=recruitapi&id=' . $job_opening_id;
if ($positionStatus == 'In-progress') {
?>
<tr>
<td>
<input type="hidden" name="job_id" value="<?php echo $request_url ?>" />
<button id="jobopening-link">
<? echo $data->response->result->JobOpenings->row[$i]->FL[4]->content; ?>
</button>
</td>
<td><? echo $data->response->result->JobOpenings->row[$i]->FL[5]->content; ?></td>
<td><? echo $data->response->result->JobOpenings->row[$i]->FL[6]->content; ?></td>
<td><? echo $recruitmentResponsibleTweak ?></td>
</tr>
<? } ?>
</form>
<? } ?>
</table>
and the input type="submit"???
to capture the $_POST content:
<?php
if (isset($_POST['job_id'])) {
echo 'Do something for this job Id.';
}
?>

Categories