Unable to delete user from checkbox in PHP - php

i am trying to delete some rows but i dont know where i am mistaking please check this below code and suggest my error with solution:
if(isset($_GET['delete']) && $_GET['delete']=="true")
{
$checkbox = $_POST['checkbox'];
for($i=0;$i<count($checkbox);$i++)
{
echo $checkbox[$i]; // here i am going to write delete query
}
}
Now my HTML code for that. I have created a table for users.
it looks something like this in HTML
<table id="sample_2">
<thead>
<tr>
<th style="width:8px;"><input type="checkbox" class="group-checkable" data-set="#sample_2 .checkboxes" /></th>
<th>Username</th>
<th>Full Name</th>
<th class="hidden-480">Email</th>
<th>Mobile</th>
<th>Activation Key</th>
<th class="hidden-480">Status</th>
</tr>
</thead>
<tbody>
<?php $user_details->all_user_details(); ?>
</tbody>
</table>
Now here is the function i am using. to create table rows. So check this out:
while ($row = $database->fetch_array($result_set)) {
$single_row ="<tr class=\"odd gradeX\">
<td><input type=\"checkbox\" name=\"checkbox[]\" class=\"checkboxes\" value=\"".$row["userid"]."\" /></td>
<td>".$row['username']."</td>
<td>".$row['fullname']."</td>
<td class=\"hidden-480\">".$row['email']."</td>
<td>".$row['mobile']."</td>
<td>".$row['user_activation_key']."</td>";
if($row['status']==1){
$single_row .= "<td><span class=\"label label-success\">Approved</span></td>";
} else{
$single_row .= "<td><span class=\"label label-danger\">Pending</span></td>"; }
$single_row .="</tr>";
echo $single_row;
}
any solution for this problem? I know i am making mistake somewhere in $_POST['checkbox'] while getting the value, Please suggest.
Thank you!

this is your first problem:
if(isset($_GET['delete']) && $_GET['delete']=="true")
{
$checkbox = $_POST['checkbox'];
for($i=0;$i<count($checkbox);$i++)
{
echo $checkbox[$i]; // here i am going to write delete query
}
}
please change it to this:
if (isset($_GET['delete']))
{
if ($_GET['delete']=="true") {
$checkbox = $_POST['checkbox'];
for($i=0;$i<count($checkbox);$i++)
{
echo $checkbox[$i]; // here i am going to write delete query
}
}
}
and also you forgot to leave a space between if and (

Related

Passing a value by anchor tag through PHP

I do not understand how to use anchor tag to pass a value. I have a form which takes a few values and submits to my mysql database. I am able to view the table. Each row has columns edit and delete. When I click edit I want to pass the primary key(job name) of that row to my edit page and similarly for delete. I am able to see the value on the URL,but I am not able to get the value on the edit page. I do not understand what is the use of ? in anchor tag.
ViewAllJobs.php
<?php
include 'jobDB.php';
$query="Select * from Jobs";
$result=mysqli_query($con,$query);
echo'
<html>
<center><h1>View all Jobs</h1></center>
<body bgcolor="#E6E6FA">
<center><form method=POST action=edit.php >
<table border="1" style="border-collapse:collapse; border-color:Black; border-style:solid; border-width:1pt">
<tr>
<th>Job Name</th>
<th>Job State</th>
<th>Schedule days</th>
<th>URL</th>
<th>Filename</th>
<th>To</th>
<th>CC</th>
<th>Reply To</th>
<th>From</th>
<th>EmailSubject</th>
<th>Email Content</th>
<th>Edit</th>
<th>Delete</th>
</tr>';
while($row=mysqli_fetch_array($result)){
echo'<tr>
<td>'.$row["jobName"].' </td>
<td>'.$row["jobState"].' </td>
<td>'.$row["jobSchedule"].' </td>
<td>'.$row["jobURL"].' </td>
<td>'.$row["jobFilename"].' </td>
<td>'.$row["jobToList"].' </td>
<td>'.$row["jobCCList"].' </td>
<td>'.$row["jobReplyTo"].' </td>
<td>'.$row["jobFrom"].' </td>
<td>'.$row["jobSubject"].' </td>
<td>'.$row["jobEmailContent"].' </td>
<td>Edit</td>
<td>Delete</td>
</tr>
';
}
echo'</table>
</form>
<center>Click to return to Home Page</center>
</center>
</body>
</html>
';
?>
edit.php
<?php
include 'jobDB.php';
$jobName=isset($_REQUEST['jobName']);
if($jobName){
echo"jobname is set";
}
else{
echo"job name is not set";
}
echo"<br>jobname is".$jobName;
$query="select* from Jobs where jobName=".$jobName;
$result=mysqli_query($con,$query);
if($result){
echo"<br>query succesful";
}
$fetch=$row=mysqli_fetch_row($result);
if($fetch){
echo"<br>result set fetched";
}
else{
echo"<br>result set not fetched";
}
?>
In edit.php $jobName=isset($_REQUEST['jobname']) gives $jobName=1 when it is supposed to have the value job1, but there does not exist a variable jobname, when I give $jobName=isset($_REQUEST['jobName']), nothing works. What is the name I should give in $_REQUEST(). Kindly help.
It should be:
$jobName = $_REQUEST['jobName'];
isset() is used to check if variable IS SET, not to assign variables.
When you use $jobName = isset($_REQUEST['jobName']);, the variable will be set to 1 as $_REQUEST['jobName'] is set and 1 represents true. Thus $jobname becomes 1.
Your code should be:
$jobName = $_REQUEST['jobName'];;
if(isset($jobName)) {
echo "jobname is set";
}
else {
echo "job name is not set";
}
echo "<br>jobname is" . $jobName;
$query = "SELECT * FROM Jobs WHERE jobName = '$jobName'";
To answer what is the use of ? in anchor tags, it is used for PHP to GET/REQUEST the values, more information at http://www.w3schools.com/php/php_forms.asp.
Hope this helps, thanks!

Don't display if no records exist in mysql

I have this form to search names in mysql database
<form action="search.php" method="GET">
<input type="text" placeholder="Search" name="name">
<input type="submit" value="Search">
this is the search.php
<?php
name = $_GET['name'];
require_once("connect.php");
$records = $connect->query("SELECT * FROM Userlists WHERE Name = '$name'");
echo
"<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Description</th>
</tr>
</thead>
<tbody>";
if (mysqli_num_rows($records)== 0){
echo "No data available for that name specified";
}
else {
while($row=mysqli_fetch_array($records)) {
$name = $row['Name'];
$email = $row['Email'];
$desc = $row['Desc'];
echo
"<tr>
<td>".$name."</td>
<td>".$email."</td>
<td>".$desc."</td>
</tr>";
}
}
echo
"</tbody>
</table>";
?>
so there is no problems when I search for a name that exists in database it displays correctly, but the problem comes when I search for a name that doesn't exist in database.. I want it to display only
"No data available for that name specified" for the output but I will also see empty table in the output like this ------------> IMAGE..
so how can I get rid of the empty table for the output?
Just pu the if outside the table....
<?php
name = $_GET['name'];
require_once("connect.php");
$records = $connect->query("SELECT * FROM Userlists WHERE Name = '$name'");
if (mysqli_num_rows($records)== 0){
echo "No data available for that name specified";
} else {
echo
"<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Desc</th>
</tr>
</thead>
<tbody>";
while($row=mysqli_fetch_array($records)) {
$name = $row['Name'];
$email = $row['Email'];
$desc = $row['Desc'];
echo
"<tr>
<td>".$name."</td>
<td>".$email."</td>
<td>".$desc."</td>
</tr>";
}
echo
"</tbody>
</table>";
}
?>
change your if clause as below and remember to add exit() or die() function,this will end your php if there is no any data in database, and if there is any it will then start creating table for once and repeatedly fill up the table rows for given rows of data on database.
if (mysqli_num_rows($records)== 0){
echo "No data available for that name specified";
exit();
} else {
echo
"<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Desc</th>
</tr>
</thead>
<tbody>";
while($row=mysqli_fetch_array($records)) {
$name = $row['Name'];
$email = $row['Email'];
$desc = $row['Desc'];
echo
"<tr>
<td>".$name."</td>
<td>".$email."</td>
<td>".$desc."</td>
</tr>";
}
echo
"</tbody>
</table>";
}
Move your echo
"<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Desc</th>
</tr>
</thead>
<tbody>";
into the the if statement. This way it will only display the table when data is available!
$name = $_GET['name'];
require_once("connect.php");
$records = $connect->query("SELECT * FROM Userlists WHERE Name = '$name'");
if (mysqli_num_rows($records)== 0){
echo "No data available for that name specified";
}
else {
echo
"<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Desc</th>
</tr>
</thead>
<tbody>";
while ($row = mysqli_fetch_array($records)) {
$name = $row['Name'];
$email = $row['Email'];
$desc = $row['Desc'];
echo
"<tr>
<td>" . $name . "</td>
<td>" . $email . "</td>
<td>" . $desc . "</td>
</tr>";
}
echo "</tbody></table>";
}
Try this one. But don't forget to escape $_GET['name'] like htmlspecialchars and real_escape_string

PHP Search Code from HTML form

I had a previous question on here a few minutes ago about a syntax error that was sorted. I need help getting this script working or at least for someone to point me in the right direction.
This is a search script to search by multiple fields. The search() array works fine and is a series of tickboxes with the following code:
<td width="22"><input type="checkbox" name="search[olevel = 'Yes']" id="search[olevel = 'Yes']" value="1"/>
The postcode box is a text box with the following code:
<input name="postcode[]" type="text" id="postcode[]" size="12" maxlength="12" /></td>
When I tick the olevel box it returns all the records that have Yes in the olevel field. That works as I expect.
If I put in anything in the postcode box it returns no results.
Here is the php code for the search engine.
<?php
include ('c1.php');
if ($_COOKIE["auth"] == "1") {
$display_block = "<p>You are an authorized user.</p>";
} else {
header("Location: userlogin.html");
exit;
}
doDB();
$display_block = "<h1>Results</h1>";
if (isset($_POST['search']) && !empty($_POST['search'])) {
foreach ($_POST['search'] as $key => $value) {
if ($value == 1)
$search[] = "$key";
$searchstring = implode(' AND ', $search);
$post_map = array(
'postcode' => 'candididate_contact_details.postcode'
);
}
if (isset($_POST['postcode']) && !empty($_POST['postcode'])) {
foreach ($_POST['postcode'] as $key => $value) {
if (array_key_exists($key, $post_map))
$search[] = $post_map[$key] . '=' . mysql_real_escape_string($value);
echo $searchstring;
echo $search;
$query = "SELECT candidate_id.master_id, candidate_contact_details.first_name, candidate_contact_details.last_name, candidate_contact_details.home_phone, candidate_contact_details.work_phone, candidate_contact_details.mobile_phone, candidate_contact_details.email FROM candidate_id, candidate_contact_details, qualifications, security_experience, previous_career WHERE qualifications.active = 'finished' and candidate_id.master_id = candidate_contact_details.master_id and candidate_id.master_id = qualifications.master_id and candidate_id.master_id = security_experience.master_id and candidate_id.master_id = previous_career.master_id and $searchstring";
$query_res = mysqli_query($mysqli, $query)
or die(mysqli_error($mysqli));
// $search = mysqli_query($mysqli, $query)or die(mysqli_error($mysqli));
{
$display_block .= "
<table width=\"98%\" cellspacing=\"2\" border=\"1\">
<tr>
<th>Registration Number</th>
<th>First Name</th>
<th>Last Name</th>
<th>Home Number</th>
<th>Work Number</th>
<th>Mobile Number</th>
<th>E-Mail</th>
</tr>";
while ($result = mysqli_fetch_array($query_res)) {
$regnum = $result['master_id'];
$first_name = $result['first_name'];
$last_name = $result['last_name'];
$home_phone = $result['home_phone'];
$work_phone = $result['work_phone'];
$mobile_phone = $result['mobile_phone'];
$email = $result['email'];
$display_block .= "
<tr>
<td align=\"center\">$regnum <br></td>
<td align=\"center\">$first_name <br></td>
<td align=\"center\">$last_name <br></td>
<td align=\"center\">$home_phone <br></td>
<td align=\"center\">$work_phone <br></td>
<td align=\"center\">$mobile_phone <br></td>
<td align=\"center\">$email <br></td>
</tr>";
}
$display_block .= "</table>";
}
}
}
}
?>
<html>
<head>
<title> Display results</title>
</head>
<body>
<?php echo $display_block; ?>
</body>
</html>
I know I am doing something wrong but cannot quite figure it out. Thanks in advance.
if I understand correctly, you have several postcodes per form.
then id="postcode[]" is wrong as there will be several id named the same in dom.
just delete that from your code.

Can each row in an HTML table be numbered?

What I would like to do is number each row in my table. I can't manually number the table myself, as all of the info in it is retrieved from a database. Is this possible with jQuery or PHP? Here's a screen shot of the table:
I tried searching for this, and did not find anything that helped me.
Here is the PHP / HTML that is displaying the table:
<tr>
<th>Name</th>
<th>Email</th>
<th>Subject</th>
<th>Created on</th>
<th style="width:65px;">Status</th>
<th>Actions</th>
</tr>
<?php
[...]
//Display the results
while($info = mysql_fetch_assoc($result)){
//data
$name = $info['name'];
$email = $info['email'];
$subject = $info['subject'];
$ticketid = $info['ticket'];
$isActive = $info['is_active'];
$created = $info['created'];
//status
if($isActive == "1") {
$status = "<span class=\"open\">Open</span>";
$status2 = "active";
}
else if($isActive == "0") {
$status = "<span class=\"closed\">Closed</span>";
$status2 = "closed";
}
else {
$status = "";
}
echo "
<tr>
<td style=\"min-width: 87px;\">$name</td>
<td style=\"min-width:248px;\" title=\"$email\">".addEllipsis($email, 33)."</td>
<td title=\"$subject\">".addEllipsis($subject, 18)."</td>
<td style=\"width: 235px;\">$created</td>
<td title=\"This ticket is $status2\">$status</td>
<td><a href='/employee/employee.php?ticket=$ticketid'>View Ticket »</a></td>
</tr>
";
}
As you can see, it's displayed with a while loop.
If anyone knows a way to number each line in my table with jQuery or PHP, please help me :)
$trCounter=0;
while($info = mysql_fetch_assoc($result)){
//data
$name = $info['name'];
$email = $info['email'];
$subject = $info['subject'];
$ticketid = $info['ticket'];
$isActive = $info['is_active'];
$created = $info['created'];
//status
if($isActive == "1") {
$status = "<span class=\"open\">Open</span>";
$status2 = "active";
}
else if($isActive == "0") {
$status = "<span class=\"closed\">Closed</span>";
$status2 = "closed";
}
else {
$status = "";
}
echo "
<tr>
<td>$trCounter++</td>
<td style=\"min-width: 87px;\">$name</td>
<td style=\"min-width:248px;\" title=\"$email\">".addEllipsis($email, 33)."</td>
<td title=\"$subject\">".addEllipsis($subject, 18)."</td>
<td style=\"width: 235px;\">$created</td>
<td title=\"This ticket is $status2\">$status</td>
<td><a href='/employee/employee.php?ticket=$ticketid'>View Ticket »</a></td>
</tr>
";
}
or you could always use the :eq api in your jquery selector or its equivalent to work with the index but like I asked it depends on what you want to do with the index.
I would go with PHP solution listed by Atul Gupta.
To add more - you also can to start iteration based on which page you are.
<?php
$i = ($page-1) * $itemsPerPage;
while(....)
{
echo $i;
$i++;
}
?>
if you are on the second page of your list would get something like 11,12,13,14,....
jQuery:
$(function () {
var i = 0;
$('table thead tr').prepend('<th>#</th>');
$('table tbody tr').each(function () {
i += 1;
$(this).prepend('<td>' + i + '</td>');
});
});
PHP
<tr>
<th>Sr. No</th> // Add header for counter
<th>Name</th>
...
</tr>
...
$i=1; // add counter -----------corrected-------------
while($info = mysql_fetch_assoc($result)){
//data
...
echo "
<tr>
<td>$i</td> // include counter to table
<td style=\"min-width: 87px;\">$name</td>
...
</tr>
";
$i++; // increment counter
}
Set an auto increment property in the SQL table, which can be used as an index, and will increase automatically when a new entry is added?

tablesorter.pager plugin

The number of returned results is 2 which is correct and it comes up with 2 for the number of pages which is also correct but instead of showing 1 on the first page it shows both records. I'm not sure hwy the tablesorter.pager plugin is doing that.
<?php
session_start();
require("../inc/dbconfig.php");
require("../inc/global_functions.php");
require("../inc/variables.php");
// find out how many rows are in the table
$query = "SELECT CONCAT_WS(' ',firstName,lastName) AS name, username, emailAddress, userID FROM manager_users WHERE statusID != 4";
$result = mysqli_query($dbc,$query);
$rows = mysqli_num_rows($result);
$itemsPerPage = 1;
$totalPages = ceil($rows/$itemsPerPage);
$fileName = basename($_SERVER[PHP_SELF]);
$pageName = "User Accounts";
$userData = $_SESSION['user_data'];
$userID = $userData['userID'];
?>
<script type="text/javascript">
$(document).ready(function() {
$('#usersPageList').tablesorter().tablesorterPager({sortlist: [0,0], container:$('#usersPageList .pagination'),cssPageLinks:'a.pageLink'});
$('a.bt_green').click(function(e) {
e.preventDefault();
$('div.right_content').load('forms/addnew/' + $(this).attr('id'));
});
$('.ask').jConfirmAction();
});
</script>
<h2>User Accounts</h2>
<table id="usersPageList" class="rounded-corner">
<thead>
<tr>
<th scope="col" class="rounded-first"></th>
<th scope="col" class="rounded">Name</th>
<th scope="col" class="rounded">Email Address</th>
<th scope="col" class="rounded">Username</th>
<th scope="col" class="rounded">Edit</th>
<th scope="col" class="rounded-last">Delete</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="5" class="rounded-foot-left"><em>Displays all of the registered and verified users!</em></td>
<td class="rounded-foot-right"> </td>
</tr>
</tfoot>
<tbody>
<?php
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo "<tr>";
echo "<td><input type=\"checkbox\" name=\"\" /></td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['emailAddress']."</td>";
echo "<td>".$row['username']."</td>";
echo "<td><img src=\"images/user_edit.png\" alt=\"\" title=\"\" border=\"0\" /></td>";
echo "<td>";
if (($row['userID'] !== '10000') && ($row['userID'] !== $userID)){
echo "<img src=\"images/trash.png\" class=\"delete\" alt=\"\" title=\"\" border=\"0\" id=\"".$row['userID']."\" />";
}
echo "</td>";
echo "</tr>";
}
?>
</tbody>
</table>
<?php
addRemove($fileName,$pageName);
pagination($totalPages);
?>
<input type="hidden" name="myhiddenPageToken" id="myhiddenPageToken" value="useraccounts" />
Maybe I'm misunderstanding what you want, but I think you want just one record at a time to show on the page? I have no idea why you would do that, but you can use the pager's size option:
$("table").tablesorter({
widthFixed: true,
widgets: ['zebra']
}).tablesorterPager({
container: $("#pager"),
size: 1 // only show one record
});
Also, if you are using the select to change the number of records showing, you'll need to include a "1" (demo).

Categories