PHP use MYSQL table result as parameter for another query - php

I have a table on my site that is pulled from MYSQL based on a search. I would like to be able to click a button added to the table which opens a modal with more data based on the row selected. In the below image I have a column labeled ID for the first table, which could be used for the = ? in my query, but I do not know how to set the value as a parameter first. Any thoughts on the best way to go about this?
I have $term = '%' . $_GET['itemID'] . '%'; added for the second query but need a way to bind ID 999999, 1000000, or 1000001 based on the corresponding view button. Thanks!
if(isset($_GET['term']))
{
$term = '%' . $_GET['term'] . '%';
$sql1 = "SELECT ID, Source, Contract_Number, Price_Effective, Price_Expiration, Min_Price, Average_Price
FROM Both_Search WHERE SearchName LIKE ? LIMIT 10";
$stmt = $conn->prepare($sql1);
$stmt->bind_param("s", $term);
$stmt->execute();
$result1 = $stmt->get_result();
while($row1 = $result1->fetch_assoc()) {
echo "<td>" . $row1["ID"] . "</td>"; <----would want this to be the parameter for next query
echo "<td>" . $row1["Source"] . "</td>";
echo "<td><a target='_blank' rel='noopener noreferrer' href='contracts.php?number=" . $row1["Contract_Number"] . "'>" . $row1["Contract_Number"] . "</td>";
echo "<td>" . $row1["Price_Effective"] . "</td>";
echo "<td>" . $row1["Price_Expiration"] . "</td>";
echo "<td>" . "$ " . $row1["Average_Price"] . "</td>";
echo "<td>" . "$ " . $row1["Min_Price"] . "</td>"; ?>
<td><button id="myBtn" type="button" class="btn btn-primary">View</button></td>
</tr><?php
}
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<span class="close">×</span>
<div class="card shadow mb-4">
<div class="card-header py-3" style="background-color: rgb(90, 136, 255);">
<h6 class="m-0 font-weight-bold text-white">Tiers</h6>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered" id="dataTable2" width="100%" cellspacing="0">
<thead>
<tr>
<th>Price 1</th>
<th>Price 2</th>
<th>Price 3</th>
<th>Price 4</th>
<th>Price 5</th>
<th>Price 6</th>
<th>Price 7</th>
<th>Price 8</th>
<th>Price 9</th>
</tr>
</thead>
<tbody>
<?php
if(isset($_GET['itemID']))
{
$term = '%' . $_GET['itemID'] . '%';
$sql1 = "SELECT *
FROM Pricelist WHERE ID = ? LIMIT 10";
$stmt = $conn->prepare($sql1);
$stmt->bind_param("s", $term);
$stmt->execute();
$result1 = $stmt->get_result();
while($row1 = $result1->fetch_assoc()) {
echo "<td>" . "$ " . $row1["P_1"] . "</td>";
echo "<td>" . "$ " . $row1["P_2"] . "</td>";
echo "<td>" . "$ " . $row1["P_3"] . "</td>";
echo "<td>" . "$ " . $row1["P_4"] . "</td>";
echo "<td>" . "$ " . $row1["P_5"] . "</td>";
echo "<td>" . "$ " . $row1["P_6"] . "</td>";
echo "<td>" . "$ " . $row1["P_7"] . "</td>";
echo "<td>" . "$ " . $row1["P_8"] . "</td>";
echo "<td>" . "$ " . $row1["P_9"] . "</td>";
?>
</tr> <?php
}
}
$conn->close();
?>
</tbody>
<script>
// Get the modal
var modal = document.getElementById("myModal");
// Get the button that opens the modal
var btn = document.getElementById("myBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>

You got the ID as $row1["ID"] and you can use that ID for Another Query.
// Replace this
<td><button id="myBtn" type="button" class="btn btn-primary">View</button></td>
// To this
<td>View</td>
And the button 'View' will redirect to YOUR_PAGE_VIEW.php with the Param Query ID = $row1["ID"].
Now you have the ID as variable $id for Another Query what you want in the YOU_PAGE_VIEW.php.
// In YOUR_PAGE_VIEW.php
$id = $_GET['id'];
Hope this help you,

Related

Export HTML table to Excel using php

I am trying to export html table to excel sheet.But it is not working. I have a table where i've called DB table values. I want to export that values to a excel sheet on a button click. But after clicking on the button, Nothing is happening.
I have followed one tutorial for this. Can you please tell me, if i am missing something?
Code:
Php code:
<button name="create_excel" id="create_excel" class='btn c-theme-btn c-btn-uppercase btn-xs c-btn-square c-font-sm'>Export to Excel</button>
</div>
<div class="c-content-panel">
<div class="c-body">
<div class="row">
<div class="col-md-12" id="employee_table">
<table class="table table-bordered">
<?php
echo " <thead>
<tr>
<th>Order ID</th>
<th>Date</th>
<th>Name</th>
<th>Store Name</th>
<th>Zip</th>
<th>City</th>
<th>Address</th>
<th>Contact</th>
<th>Tv Varient</th>
<th>Total</th>
<th>Delivery</th>
<th>Action</th>
</tr>
</thead>
<tbody>";
while($row = mysqli_fetch_array($result))
{
$id = $row['id'];
echo"<tr id='row_$id'>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['rdate'] . "</td>";
echo "<td>" . $row['rname'] . "</td>";
echo "<td>" . $row['rstore'] . "</td>";
echo "<td>" . $row['rzip'] . "</td>";
echo "<td>" . $row['rcity'] . "</td>";
echo "<td>" . $row['raddress'] . "</td>";
echo "<td>" . $row['rphone'] . "</td>";
echo "<td>" . $row['rtv_varient'] . "</td>";
echo"<td>" . $row['ramount'] . "</td>";
echo"<td>" . $row['rdelivery'] . "</td>";
echo "<input type='hidden' id='row_id' value='$id'/>";
echo "<td> <button id='test' class='btn c-theme-btn c-btn-uppercase btn-xs c-btn-square c-font-sm'>Delete</button> </td>";
echo"</tr>";
}
?>
</tbody>
excel.php:
<?php
//excel.php
header('Content-Type: application/vnd.ms-excel');
header('Content-disposition: attachment; filename='.rand().'.xls');
echo $_POST["data"];
?>
Script Code:
<script>
$(document).ready(function(){
$('#create_excel').click(function(){
var excel_data = $('#employee_table').html();
var page = "excel.php?data=" + excel_data;
window.location = page;
});
});
</script>
I have followed this tutorial: http://www.webslesson.info/2016/08/export-html-table-to-excel-file-using-jquery-with-php.html

Display table vertically

I want to display table vertically. Now, It's displaying horizontally. Each
and every field is displaying. How can I display table vertically?
And one more thing I want to add one more function to the button. Current
date added into database when button
submit is clicked. For now this code is making my status 0 and not updating
date in database.
<html>
<head>
<meta name="description" content="website description" />
<meta name="keywords" content="website keywords, website keywords" />
<meta http-equiv="content-type" content="text/html; charset=windows-
1252" />
<link rel="stylesheet" type="text/css" href="style/style.css" />
<style>
<table>
{
border-style:solid;
border-width:2px;
border-color:pink;
}
</style>
</head>
<?php
include 'inc/head.php';
include 'connection.php';
$id = $_GET['id'];
$sql = "SELECT * FROM user WHERE id='$id'";
$query = mysqli_query($conn, $sql);
echo "<table border='6'>
<tr>
<th>Application Type</th> <br>
<th>First name</th><br>
<th>Sur name</th><br>
<th>title</th>
<th>Date of Birth</th>
<th>Gender</th>
<th>Nationality</th>
<th>Country of Birth</th>
<th>Applicant's Location</th>
<th>alias</th>
<th>Criminal </th>
<th>Country of passport</th>
<th>Passport number</th>
<th>Passport issue date </th>
<th>Passport expiry date</th>
<th>Address </th>
<th>City</th>
<th>State</th>
<th>Postal Code</th>
<th>Country</th>
<th>Email Address</th>
<th>Home Number</th>
<th>Business Number</th>
<th>mobile</th>
</tr>";
while($row = mysqli_fetch_assoc($query)) {
echo "<tr>";
echo " <td>" . $row['applicationtype'] . "</td>";
echo " <td>" . $row['firstname'] . "</td>";
echo " <td>" . $row['lastname'] . "</td>";
echo " <td>" . $row['title1'] . "</td>";
echo " <td>" . $row['dob'] . "</td>";
echo " <td>" . $row['gender'] . "</td>";
echo " <td>" . $row['nationality'] . "</td>";
echo " <td>" . $row['countryofbirth'] . "</td>";
echo " <td>" . $row['applicantlocation'] . "</td>";
echo " <td>" . $row['alias'] . "</td>";
echo " <td>" . $row['criminal'] . "</td>";
echo " <td>" . $row['countryofpassport'] . "</td>";
echo " <td>" . $row['passportnumber'] . "</td>";
echo " <td>" . $row['pid'] . "</td>";
echo " <td>" . $row['ped'] . "</td>";
echo " <td>" . $row['address'] . "</td>";
echo " <td>" . $row['city'] . "</td>";
echo " <td>" . $row['state'] . "</td>";
echo " <td>" . $row['postalcode'] . "</td>";
echo " <td>" . $row['country'] . "</td>";
echo " <td>" . $row['email'] . "</td>";
echo " <td>" . $row['homephone'] . "</td>";
echo " <td>" . $row['businessphone'] . "</td>";
echo " <td>" . $row['mobile'] . "</td>";
echo "</tr>";
}
echo "</table><br><br>";
if(isset($_POST['submit'])) {
echo $radio = $_POST['radio'];
$date_clicked = date('Y-m-d H:i:s');
$sql = "UPDATE user SET visastatus='$radio' AND currentdate='$date_clicked' WHERE id='$id'";
$query = mysqli_query($conn, $sql);
if($query) {
echo "<h4 style='color:green'>Action Performed Successfully....</h4>";
} else {
echo "<h4 style='color:red'>Failed.</h4>";
}
}
?>
<form method="POST" action="" enctype="multipart/form-data">
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6" style="margin-top:
8px;">
<label class="checkbox-inline tourist-rad">
<input type="radio" name="radio" id="success"
value="Successfull">Successfull
</label>
<label class="checkbox-inline tourist-rad">
<input type="radio" name="radio" id="decline" value="Declined">Declined
</label>
</div>
<div class="form-group">
<button type="submit" name="submit" value="submit" class="btn btn-
primary" style="float: right;">Update Menu Item</button>
</div>
</body>
</html>
How can I display the table vertically?
Put each heading in a new <tr>. Remember, <tr> stands for table row, while <td> is for each of the elements in that row. This will require some modification of your php code so that you can essentially print a new row, and each td within it.
Edit: To display it vertically as you like, you'd need to modify your existing while loop.
//Print the row
echo "<tr>";
//Print the row heading
<th>Application Type</th>
while($row = mysqli_fetch_assoc($query)) {
//Print each matching data type inside
echo " <td>" . $row['applicationtype'] . "</td>";
}
//Close the row
echo "</tr>";
You'll need to do this functionality for each of the headings you have. You could also use arrays along with nested loops to print all the data cleanly and easily without excess code.
If by "vertically" you mean you want to have 2 columns and 24 rows instead of 24 columns and 2 rows, this will do it for you:
echo "<table border='6'>";
while($row = mysqli_fetch_assoc($query)) {
echo "<tr><th>Application Type</th><td>{$row['applicationtype']}</td></tr>";
echo "<tr><th>First Name</th><td>{$row['firstname']}</td></tr>";
echo "<tr><th>Surname</th><td>{$row['lastname']}</td></tr>";
echo "<tr><th>Title</th><td>{$row['title1']}</td></tr>";
echo "<tr><th>Date of Birth</th><td>{$row['dob']}</td></tr>";
echo "<tr><th>Gender</th><td>{$row['gender']}</td></tr>";
echo "<tr><th>Nationality</th><td>{$row['nationality']}</td></tr>";
echo "<tr><th>Country of Birth</th><td>{$row['countryofbirth']}</td></tr>";
echo "<tr><th>Applicant's Location</th><td>{$row['applicantlocation']}</td></tr>";
echo "<tr><th>Alias</th><td>{$row['alias']}</td></tr>";
echo "<tr><th>Criminal</th><td>{$row['criminal']}</td></tr>";
echo "<tr><th>Country of Passport</th><td>{$row['countryofpassport']}</td></tr>";
echo "<tr><th>Passport Number</th><td>{$row['passportnumber']}</td></tr>";
echo "<tr><th>Passport Issue Date</th><td>{$row['pid']}</td></tr>";
echo "<tr><th>Passport Expiry Date</th><td>{$row['ped']}</td></tr>";
echo "<tr><th>Address</th><td>{$row['address']}</td></tr>";
echo "<tr><th>City</th><td>{$row['city']}</td></tr>";
echo "<tr><th>State</th><td>{$row['state']}</td></tr>";
echo "<tr><th>Postal Code</th><td>{$row['postalcode']}</td></tr>";
echo "<tr><th>Country</th><td>{$row['country']}</td></tr>";
echo "<tr><th>Email Address</th><td>{$row['email']}</td></tr>";
echo "<tr><th>Home Number</th><td>{$row['homephone']}</td></tr>";
echo "<tr><th>Business Number</th><td>{$row['businessphone']}</td></tr>";
echo "<tr><th>Mobile</th><td>{$row['mobile']}</td></tr>";
}
echo "</table>";
Additionally, if you are going to be handling such sensitive personal data, you MUST improve your query security starting with mysqli prepared statements using placeholders.

I want to change the value of 'is_active' change 1 or 0 in my database using jquery, Here is my code

Here is button image value
$result = mysqli_query($con,"SELECT * FROM customer WHERE is_active=0");
echo "<div class='row'>
<div class='col-lg-12'>
<table class='table table-striped table-advance table-hover'>
<tbody>
<tr>
<th><i class='icon_profile'></i> First Name</th>
<th><i class='icon_calendar'></i> DOB</th>
<th><i class='icon_mail_alt'></i> Email</th>
<th><i class='icon_pin_alt'></i> Country</th>
<th><i class='icon_mobile'></i> Mobile</th>
<th><i class='icon_cogs'></i> Action</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['first_name'] . "</td>";
echo "<td>" . $row['dob'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['country_livingin'] . "</td>";
echo "<td>" . $row['mobile'] . "</td>";
echo"<td>
<div class='btn-group'>
<a id='pending' class='imgicon btn btn-success' href='#'><i id='check' class='icon_check_alt2'></i></a>
</div>
</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Jquery
$(".imgicon").on("click",function()
{
// alert("button");
var parent = $(this).parents("tr");
var dataEmail = $(parent).find("td:nth-child(3)").text();
$.ajax({
url:'pending_method.php',
dataType:'json',
data:{email:dataEmail},
success:function(data){
alert('hi')
},
error:function(data){
alert("error");
}
});
});
pending_method.php
<?php
// $con=mysqli_connect("166.62.6.65","UBBS_Matrimonial","UBBS_Matrimonial","UBBS_Matrimonial");
$con=mysqli_connect("localhost","root","","Dummy");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
var email = $_POST["email"];
$result = mysqli_query($con,"UPDATE customer set is_active=1 WHERE email=email);
?>

PHP connection to a database (no output!)

I wrote a code to connect a html page to the database, but it does not search for all employees in the table. I'm not sure why the code seems to be correct. It does not give any error or warning.
Can anyone explain to me what is wrong?
<div>
<form id='searchform' action='index.php' method='get'>
<a href='index.php'>All employees</a> ---
Search by a last name:
<input id='search' name='search' type='text' size='20' value='<?php echo #$_GET['search']; ?>' />
<input id='submit' type='submit' value='Go!' />
</form>
</div>
<?php
// check if search view of list view
if (isset($_GET['search'])) {
$sql = "SELECT * FROM Employee WHERE LastName like '%" . #$_GET['search'] . "%'";
} else {
$sql = "SELECT * FROM Employee";
}
// execute sql statement
$stmt = oci_parse($conn, $sql);
oci_execute($stmt);
?>
<table style='border: 1px solid #DDDDDD'>
<thead>
<tr>
<th>Personal Number</th>
<th>Insurance Number</th>
<th>First Name</th>
<th>Last Name</th>
<th>Birth Date</th>
<th>Gender</th>
<th>Hire Date</th>
<th>Salary</th>
<th>Office ID</th>
</tr>
</thead>
<tbody>
<?php
// fetch rows of the executed sql query
while ($row = oci_fetch_assoc($stmt)) {
echo "<tr>";
echo "<td>" . $row['PersonalNumber'] . "</td>";
echo "<td>" . $row['InsuranceNumber'] . "</td>";
echo "<td>" . $row['FirstName'] . " " . $row['LastName'] . "</td>";
echo "<td>born on " . $row['BirthDate'] . "</td>";
echo "<td>" . $row['Gender'] . "</td>";
echo "<td>hired on " . $row['HireDate'] . "</td>";
echo "<td>" . $row['Salary'] . "</td>";
echo "<td>" . $row['OfficeID'] . "</td>";
echo "</tr>";
}
?>
</tbody>
</table>
<div>There are found <?php echo oci_num_rows($stmt); ?> employees!</div>
<?php oci_free_statement($stmt); ?>
</body>

Hiding a table if a search result does not exist

Exactly what the title says. I want the table containing all of the search queries to be hidden, but I've tried a lot of things and none of them work. For example, if($myData!=null) {proceed with showing the table}, but that didn't work. isset() also didn't work. Any ideas?
<style>
ul
{
list-style-type: none;
}
</style>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Search Chemicals</title>
</head>
<p>
<body>
<h3>Chemical Information</h3>
<p>You may search by Catalog number, CASRN, or the chemical name.</p>
<form method="post" action="search.php?go" id="searchform">
<input type="text" name="name">
<input type="submit" name="submit" value="Search">
</form>
<?php
error_reporting(0);
if (isset($_POST['submit'])) {
if (isset($_GET['go'])) {
if (preg_match("/^[a-zA-Z0-9]+/", $_POST['name'])) {
$name = $_POST['name'];
$conn = mysql_connect("localhost", "Blimeo", "password");
$db = mysql_connect("localhost", "-", "-") or die('I cannot connect to the database because: ' . mysql_error());
//-select the database to use
$mydb = mysql_select_db("chemicals");
//-query the database table
$sql = "SELECT * FROM products WHERE Catalog LIKE '%" . $name . "%' OR CASRN LIKE '%" . $name . "%' OR Chemical_Name LIKE '%" . $name . "%'";
$myData = mysql_query($sql, $conn);
echo "<table border=1>
<tr>
<th>Catalog</th>
<th>Image</th>
<th>CASRN</th>
<th>Chemical Name</th>
<th>Quantity 1</th>
<th>Price 1</th>
<th>Quantity 2</th>
<th>Price 2</th>
<th>Quantity 3</th>
<th>Price 3</th>
<th>Quantity 4</th>
<th>Price 4</th>
</tr>";
while ($record = mysql_fetch_array($myData)) {
echo "<tr>";
echo "<td>" . $record['Catalog'] . "</td>";
echo "<td><img src=\"./img/" . $record['Image'] . "\" alt=\"Chemical\"/></td>";
echo "<td>" . $record['CASRN'] . "</td>";
echo "<td>" . $record['Chemical_Name'] . "</td>";
echo "<td>" . $record['Quantity1'] . "</td>";
echo "<td>" . $record['Price1'] . "</td>";
echo "<td>" . $record['Quantity2'] . "</td>";
echo "<td>" . $record['Price2'] . "</td>";
echo "<td>" . $record['Quantity3'] . "</td>";
echo "<td>" . $record['Price3'] . "</td>";
echo "<td>" . $record['Quantity4'] . "</td>";
echo "<td>" . $record['Price4'] . "</td>";
echo "</tr>";
echo "</form>";
echo "<ul>\n";
echo "<li>" . "" . $Catalog . " " . $CASRN . " " . $Chemical_Name . "</li>\n";
echo "</ul>";
}
}
} else {
echo "<p>Product not found! Please rephrase your search criteria.</p>";
}
}
?>
</body>
</html>
</p>
You should add mysql_num_rows();
$myData = mysql_query($sql, $conn);
$exists = mysql_num_rows($myData);
if($exists) {
echo "<table border=1>";
//..................
echo "</table>";
} else {
echo "<p>Product not found! Please rephrase your search criteria.</p>";
}
Well, it seems you are echoing out your table regardless of the results of the search query. You should probably check the number of rows returned in teh result set and only echo out the table if the count is > 0.
Use <div visibility="hidden"> to hide the table and use Javascript to change the visibility based on the search queries or some other condition.

Categories