php search table by id - php

Im trying to display all the information from a single row in my database table by the id.
When I click search it shows the entire table and the search really doesnt do anything..
Any help would be appreciated. Thank you :)
im using php/mysql
<?php
session_start();
include('connect.php');
if(isset($_POST['search']))
{
$q = $_POST['srch_query'];
?>
<form method="post" action="">
<input type="text" name="srch_query" value="<?php echo $q ?>" required>
<input type="submit" name="search" value="Search">
</form>
<?php
$search = $db->prepare("SELECT species, tree_desc, age, city, state, location FROM tree_info");
$search->execute();
if($search->rowcount()==0){ echo "No product found!"; }
else
{
echo "Search Result:</br>";?>
<table border="1" cellspacing="0" cellpadding="4">
<thead>
<tr>
<th>Species</th>
<th>Description</th>
<th>Age</th>
<th>City</th>
<th>State</th>
<th>Location</th>
</tr>
</thead>
<tbody>
<?php foreach($search as $s)
{ ?>
<tr class="record">
<td><?php echo $s['species']; ?></td>
<td><?php echo $s['tree_desc']; ?></td>
<td><?php echo $s['age']; ?></td>
<td><?php echo $s['city']; ?></td>
<td><?php echo $s['state']; ?></td>
<td><?php echo $s['location']; ?></td>
</tr>
<?php }
}
} ?>
</tbody>
</table>

You should use $q in your query to filter your resultset. Currently you only send the var to the server, without doing anything with it.
$search = $db->prepare("SELECT species, tree_desc, age, city, state, location FROM tree_info WHERE tree_id=:id");
$search->execute([':id' => (int) $q]);

Your $search variable is a PDOStatement object. You need to fetch result.
Do it this way :
<?php
$results = $search->fetchAll();
foreach($results as $s)
{ ?>
<tr class="record">
<td><?php echo $s['species']; ?></td>
<td><?php echo $s['tree_desc']; ?></td>
<td><?php echo $s['age']; ?></td>
<td><?php echo $s['city']; ?></td>
<td><?php echo $s['state']; ?></td>
<td><?php echo $s['location']; ?></td>
</tr>
<?php } ?>
And check the doc for more informations on the PDOStatement if needed.
http://php.net/manual/fr/class.pdostatement.php
EDIT : my bad, I was focusing on the wrong problem.
You must add your $q to your query if it exists :
if ($q) {
$search = $db->prepare("SELECT species, tree_desc, age, city, state, location FROM tree_info WHERE id = :id");
$search->bindParam('id', $q, PDO::PARAM_INT);
} else {
$search = $db->prepare("SELECT species, tree_desc, age, city, state, location FROM tree_info");
}

Related

How to insert a single user from a table of all users into a table in MySQL in PHP

I have a table of all registered users from my users table from my database. Now I have an accept button, when clicked I want to add that specific user to a new table called accepted_applicants.
But when I try to do this it adds all the users into the database. How can I only add the specific row that I click?
What am I doing wrong?
Here is my code:
//users query
$query = $conn->query("SELECT users.id AS userid, users.status AS status, users.name AS username, users.dob AS dob, users.gender AS g, users.country AS country, users.addedDate AS created_date, users.categoryId AS catID ,
category.name AS awardname, country.id AS countryID, country.country_name AS countryName FROM users LEFT JOIN category ON users.categoryId = category.id LEFT JOIN country ON country.id = users.country;");
<table class="table table-head-fixed text-nowrap">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Gender</th>
<th>Date of Birth</th>
<th>Country</th>
<th>Award Category</th>
<th>Date Created</th>
<th>Application Status</th>
<th>Answers</th>
<th>Accept</th>
<th>Reject</th>
</tr>
</thead>
<tbody>
<?php
while ($row = $query->fetch()) {
$userid = $row['userid'];
$username = $row['username'];
$gender = $row['g'];
$dob = $row['dob'];
$date = $row['created_date'];
$status = $row['status'];
$countryName = $row['countryName'];
$awardCatName = $row['awardname'];
if(isset($_POST['accept'])){
$insertq = $conn->prepare("INSERT INTO accepted_applicants (user_id,name,gender,dob,country,award_cat,status) VALUES (?,?,?,?,?,?,?)");
$insertq->bindValue(1,$userid);
$insertq->bindValue(2,$username);
$insertq->bindValue(3,$gender);
$insertq->bindValue(4,$dob);
$insertq->bindValue(5,$countryName);
$insertq->bindValue(6,$awardCatName);
$insertq->bindValue(7,$status);
$insertq->execute();
}
?>
<tr>
<td><?php echo $row['userid'] ?></td>
<td><?php echo $row['username'] ?></td>
<td><?php echo $row['g'] ?></td>
<td><?php echo $row['dob'] ?></td>
<td><?php echo $countryName ?></td>
<td><?php echo $awardCatName ?></td>
<td><?php echo $row['created_date'] ?></td>
<td><?php if($row['status'] == 1){
echo "Submitted";
}else{
echo "Not Submitted";
} ?></td>
<td><button>Answers</button></td>
<td><input type="submit" name="accept" value="Accept"/></td>
<td><input type="submit" name="reject"/></td>
</tr>
<?php
}
?>
</tbody>
</table>
</form>
i solved it this way:
I added an extra condition to my if statement to check if the userid is equal to the value of the Accept input field.
<form method="post" action="applicant_approval.php" >
<tbody>
<?php
while ($row = $query->fetch()) {
// $userid = $row['userid'];
$username = $row['username'];
$gender = $row['g'];
$dob = $row['dob'];
$date = $row['created_date'];
$status = $row['status'];
$countryName = $row['countryName'];
$awardCatName = $row['awardname'];
if(isset($_POST['accept']) && $_POST['accept']==$row['userid']){
$insertq = $conn->prepare("INSERT INTO accepted_applicants (user_id,name,gender,dob,country,award_cat,status) VALUES (?,?,?,?,?,?,?)" );
$insertq->bindValue(1,$row['userid']);
$insertq->bindValue(2,$username);
$insertq->bindValue(3,$gender);
$insertq->bindValue(4,$dob);
$insertq->bindValue(5,$countryName);
$insertq->bindValue(6,$awardCatName);
$insertq->bindValue(7,$status);
$insertq->execute();
}
?>
<tr>
<td><?php echo $row['userid'] ?></td>
<td><?php echo $row['username'] ?></td>
<td><?php echo $row['g'] ?></td>
<td><?php echo $row['dob'] ?></td>
<td><?php echo $countryName ?></td>
<td><?php echo $awardCatName ?></td>
<td><?php echo $row['created_date'] ?></td>
<td><?php if($row['status'] == 1){
echo "Submitted";
}else{
echo "Not Submitted";
} ?></td>
<td><button>Answers</button></td>
<td><input type="submit" name="accept" value="<?php echo $row['userid']; ?>"/></td>
<td><input type="submit" name="reject"/></td>
</tr>
<?php
}
?>
</tbody>
</table>
</form>

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.

First result from DB not showing when using while loop

I am creating a php script to generate reports based on data stored in SQL, The query is here:
$sql = $adb->query("SELECT firstname, lastname, policynumber, isatype, isaname, startdate, unitvalue, numberofunits, currentamount, date, newunitvalue, newnumberofunits, newcurrentamount
FROM vtiger_isa, vtiger_addisa, vtiger_contactdetails
WHERE vtiger_isa.relatedclient = vtiger_addisa.addrelatedclient
AND vtiger_addisa.addrelatedclient = vtiger_contactdetails.contactid
AND vtiger_isa.relatedclient = $clientid
AND vtiger_isa.policynumber = $polnum
AND vtiger_addisa.addrelatedclient = $clientid
AND vtiger_addisa.newpolicynumber = $polnum
ORDER BY date ASC"
);
This performs fine as I have tested by using print_r($sql); and the results I want are there. Although when I am looping through the results they do not show. I have tested with different clientid's and the first result seems to missed out.
<b> New Figures:</b>
<table style="width:100%">
<tr>
<th>Date</th>
<th>Unit Value (P)</th>
<th>Number of Units</th>
<th>Total Value (£)</th>
</tr>
<?php
while ($sql->fetchInto($row)) {
?>
<tr>
<td><?php echo $row['date'];?></td>
<td><?php echo $row['newunitvalue'];?></td>
<td><?php echo $row['newnumberofunits'];?></td>
<td><?php echo $row['newcurrentamount'];?></td>
</tr>
<?php
}?>
</table>
</body>
</html>
I am not using the fetchInto() method. I using the following example:
<?php
$sql = $adb->query("...");
foreach($sql as $row){
?>
<tr>
<td><?php echo $row['date'];?></td>
<td><?php echo $row['newunitvalue'];?></td>
<td><?php echo $row['newnumberofunits'];?></td>
<td><?php echo $row['newcurrentamount'];?></td>
</tr>
<?php
}
?>
This is working every time. Try it out.

PHP not displaying last column in table

I am trying to display all the info in the table but when I query the information then put it into a PHP table it doesn't show any data in the last table.
Here is my PHP code for the table
<table border='1'>
<tr>
<th>Ticket ID</th>
<th>Username</th>
<th>Message</th>
<th>Date</th>
<th>Error #</th>
<th>Priority</th>
</tr>
<?php
if(!$query){
die('Invalid query: ' .mysql_error());
}
while ($row = mysql_fetch_array($query)) { ?>
<tr>
<td><?php echo $row['TicketID']; ?></td>
<td><?php echo $row['Message']; ?></td>
<td><?php echo $row['Date']; ?></td>
<td><?php echo $row['Error']; ?></td>
<td><?php echo $row['Priority']; ?></td>
</tr>
<?php } ?>
</table>
Here is the PHP code for query
<?php
$server = mysql_connect("localhost", "root", "**password**");
$db = mysql_select_db("minecraft", $server);
$query = mysql_query("SELECT * FROM tickets");
?>
All of my row names are correct but it doesn't want to put the data into that column.
Here is my table structure
You Omitted
<td><?php echo $row['Username']; ?></td>
that should be after
<td><?php echo $row['TicketID']; ?></td>

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>

Categories