I am trying to use php and I am really new to it.
I create one page that (with a php code) send the info to the database and one page that display the info from the databse.
My question is: Lets say I am going to input 10 info , but I am displaying just 5 of them, how can I display the rest of the info clicking to a button for each row?
Do I have to assign to the button a value? I cant understand how I can relate the button to the row.
Basically for each row I want to display the rest of the info of that specific row clicking on that specific button.
Any help will be appricciate. thanks
----- script that insert the info in the db-----
<?php
$name = mysqli_real_escape_string($link, $_POST['name']);
$mail = mysqli_real_escape_string($link, $_POST['mail']);
$number = mysqli_real_escape_string($link, $_POST['number']);
$device = mysqli_real_escape_string($link, $_POST['device']);
$model = mysqli_real_escape_string($link, $_POST['model']);
$problem = mysqli_real_escape_string($link, $_POST['problem']);
$sql = "INSERT INTO cj (name, mail, number, device, model, problem) VALUES ('$name', '$mail', '$number', '$device', '$model', '$problem')";
$result = mysqli_query($link, $sql);
// if query fails stop script and echo error
if( $result === false)
{echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
exit;
}
header("location:add-customer.php?message=A job and customer has been added to the database");
exit;
echo "You'll never see this";
?>
----------- page that shows the info ------
<?php
$sql = "SELECT * from "database name"";
echo "
<table class='table'>
<thead>
<tr>";
/* Get field information for all columns */
echo "<form action='' method=post>";
echo "<tr class='info'>
<input type=hidden name=hidden value=" . $row['id'] . ">
<td>" . $row['id'] . "</td>
<td>" . $row['device'] . "</td>
<td>" . $row['model'] . "</td>
<td>" . $row['problem'] . "</td>
<td>
</td>
<td>
<a class='btn btn-primary btn-sm' data-toggle='modal' data-target='#myModal' value= " . $row['id'] . " > Info</a></form></td>
</tr>";
echo "</form>";
}
echo "
</tbody>
</table>";
?>
<div class="container">
<!-- Trigger the modal with a button -->
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog modal-lg">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Customer Information</h4>
</div>
<div class="modal-body">
<?php
include("../includes/connection.php");
if ($link->connect_errno > 0) {
die('Unable to connect to database [' . $link->connect_error . ']');
}
$sql = "SELECT name,mail,number from databasename WHERE **???**;
if (!$result = $link->query($sql)) {
die('There was an error running the query [' . $link->error . ']');
}
echo "
<table class='table'>
<thead>
<tr>";
/* Get field information for all columns */
while ($finfo = $result->fetch_field()) {
echo "
<th>" . $finfo->name . "</th>";
}
echo "
</tr>
</thead>
<tbody>";
while ($row = $result->fetch_assoc()) {
echo "<tr class='info'>
<td>" . $row['name'] . "</td>
<td>" . $row['mail'] . "</td>
<td>" . $row['number'] . "</td>
</tr>";
...
Connect to datatabase and get all 10 info with PHP. But show only 5 info. Put a button there, and use Javascript or Jquery to show other rows. (Onclick event)
I found a solution using ajax:
here is the code: (an ajax call to a third php page which fixed my problem)
<script>
$(document).ready(function(){
$('.get_info').click(function(){
var job_id = $(this).parent().siblings('input[name=hidden]').val();
$.ajax({
url: 'THIRDPAGE.php',
data: 'job_id=' + job_id,
type: 'POST',
success: function(data){
// console.log(data);
$('.info_data').html(data);
}
});
});
});
</script>
For the oone that is getting here just comment and I can explain better what I did.
Related
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,
I have a database called simple_stall with table order_detail which have 4 columns ID Name Ordered_Item Quantity...currently after user submit their order, they'll be redirected to a page called order_detail.php...this page will show all ordered item in table with header ID Name Ordered_Item Quantity
now, when user click on someone's name from the table, i want to redirect user to a new page called view_more.php which will show the item ordered by the user however, nothing showed in the page.
This is my code:
index.php
<div class="container">
<form action="insert_data.php" method="POST">
<div>
<input type="text" name="Name" placeholder="Name">
</div>
<div>
<input type="text" name="Order" placeholder="Order">
</div>
<div>
<input type="text" name="Quantity" placeholder="Quantity">
</div>
<div>
<button type="submit" name="submit">Send Order</button>
</div>
</form>
</div>
insert_data.php
if (isset($_POST['submit']))
{
include_once 'dbh.php';
// Escape user inputs for security
$name = mysqli_real_escape_string($connection, $_POST['Name']);
$order = mysqli_real_escape_string($connection, $_POST['Order']);
$quantity = mysqli_real_escape_string($connection, $_POST['Quantity']);
// attempt insert query execution
$sql = "INSERT INTO order_detail (Name, Ordered_Item, Quantity) VALUES ('$name', '$order', '$quantity')";
if(mysqli_query($connection, $sql))
header("Location: ./order_detail.php?status=ordered");
else
echo "ERROR: Could not able to execute $sql. " . mysqli_error($connection);
// close connection
mysqli_close($connection);
}
else
{
header("Location: ./index.php?status=failed");
exit();
}
order_detail.php
<body>
<table>
<tr>
<th width="30px">No</th>
<th width="30%">Name</th>
<th width="30%">Ordered Item</th>
<th width="50px">Quantity</th>
</tr>
<?php
include_once 'dbh.php';
$query = "SELECT * FROM order_detail"; //You don't need a ; like you do in SQL
$result = mysqli_query($connection, $query);
echo "<table border = 1px>"; // start a table tag in the HTML
while($row = mysqli_fetch_array($result))
{
$name = $row['Name'];
//Creates a loop to loop through results
echo "<tr><td style = 'width:30px;'>" . $row['ID'] . "</td>
<td style = 'width:30%;'>" . "<a href='view_more.php?id=$name'>" . $row['Name'] . "</td>
<td style = 'width:30%;'>" . $row['Ordered_Item'] . "</td>
<td>" . $row['Quantity'] . "</td></tr>"; //$row['index'] the index here is a field name
}
echo "</table>"; //Close the table in HTML
mysqli_close($connection); //Make sure to close out the database connection
?>
view_more.php
if (isset($_POST['Name']))
{
include_once 'dbh.php';
$name = $row['Name'];
$query = "SELECT * FROM order_detail WHERE Name = $name";
$result = mysqli_query($connection, $query);
echo "<table border = 1px>"; // start a table tag in the HTML
while($row = mysqli_fetch_array($result))
{
//Creates a loop to loop through results
echo "<tr><td style = 'width:30px;'>" . $row['ID'] . "</td>
<td style = 'width:30%;'>" . $row['Name'] . "</td>
<td style = 'width:30%;'>" . $row['Ordered_Item'] . "</td>
<td>" . $row['Quantity'] . "</td></tr>"; //$row['index'] the index here is a field name
}
echo "</table>"; //Close the table in HTML
mysqli_close($connection);
}
It will not show,
because on view_more.php you have if (isset($_POST['Name'])) which will never be true since you are not using $_POST on view_more.php, you are using <td style = 'width:30%;'>" . "<a href='view_more.php?id=$name'>" . $row['Name'] . "</td> you are using normal link so replace it with this code
if (isset($_GET['id']))
{
include_once 'dbh.php';
$name = $_GET['id'];
$query = "SELECT * FROM order_detail WHERE Name = '$name'";
$result = mysqli_query($connection, $query);
echo "<table border = 1px>"; // start a table tag in the HTML
while($row = mysqli_fetch_array($result))
{
//Creates a loop to loop through results
echo "<tr><td style = 'width:30px;'>" . $row['ID'] . "</td>
<td style = 'width:30%;'>" . $row['Name'] . "</td>
<td style = 'width:30%;'>" . $row['Ordered_Item'] . "</td>
<td>" . $row['Quantity'] . "</td></tr>"; //$row['index'] the index here is a field name
}
echo "</table>"; //Close the table in HTML
mysqli_close($connection);
}
and you should be good to go, however, I highly recommend you to use proper php framework.
When generating the link to the view_more.php page, you're are injecting a GET param named id:
<a href='view_more.php?id=$name'>
And on the view_more.php page, you are testing for a POST param called name:
if (isset($_POST['Name']))
fix this to
if (isset($_GET['id']))
By the way, your code is really, really ugly. there are a tons of things done wrong :
unescaped params in SQL query : you are exposed to SQL injections which is a severe security breach.
coupled PHP and HTML scripts : take a look at Separation of Concerns and MVC Design pattern
I have a PHP page that shows pending claims which can have their status changed when a checkbox is selected and the array updated.
<?php include("partials/header.php"); ?>
<p>Manage expenses here!</p>
<body>
<style type="text/css">
#dis{
display:none;
}
</style>
<div id="dis">
<!-- here message will be displayed -->
</div>
<p>Current Claims</p>
<div class="container">
<form method="post" action="export.php" enctype="multipart/form-data" >
<table cellspacing="0" width="100%" id="example" class="table table-striped table-hover table-responsive">
<thead>
<tr>
<th>ID</th>
<th>Employee ID</th>
<th>Ref</th>
<th>Date</th>
<th>Filed Date</th>
<th>Type</th>
<th>Client</th>
<th>Project</th>
<th>Narrative</th>
<th>Net</th>
<th>VAT</th>
<th>Status</th>
<th></th>
</tr>
</thead>
<tbody>
<?php
//connection details
require_once 'dbconfig.php';
//end connection details
$query = "SELECT * FROM claim WHERE status='pending'";
$result = mysql_query($query);
if($result === false) {
die(mysql_error());
}
while($row = mysql_fetch_array($result)){
$row_id = $row['c_id'];
$employee_id = $row['employee_id'];
$claim_ref = $row['claim_ref'];
$transaction_date = $row['transaction_date'];
$filed_date = $row['filed_date'];
$expense_type_id = $row['expense_type_id'];
$client_code_id = $row['client_code_id'];
$project_code_id = $row['project_code_id'];
$narrative = $row['narrative'];
$net = $row['net'];
$vat = $row['vat'];
$status = $row['status'];
echo
"<tr>
<td>" . $row_id . "</td>
<td>" . $employee_id . "</td>
<td>" . $claim_ref . "</td>
<td>" . $transaction_date . "</td>
<td>" . $filed_date . "</td>
<td>" . $expense_type_id . "</td>
<td>" . $client_code_id . "</td>
<td>" . $project_code_id . "</td>
<td>" . $narrative . "</td>
<td>" . $net . "</td>
<td>" . $vat . "</td>
<td>" . $status . "</td>
<td><input type='checkbox' name='c_id[]' value='" . $row_id . "'/></td>
</tr>";
}
echo "</tbody></table><input type='submit' value='accept'></form>"; //end form
/***** Update Status *****/
/*print_r($_POST);*/
if(gettype($_POST['c_id'])=="array"){
foreach($_POST['c_id'] as $val){
$id_c = $val;
$query2 = "UPDATE claim SET status = 'accepted' where c_id='".$id_c."'";
$result2 = mysql_query($query2);
if($result2 === false) {
die(mysql_error());
}
echo "Status " .$id_c. " is updated. <br>";
}
}
mysql_close();
?>
</tbody>
</table>
</div>
</div>
<br />
</script>
</body>
</html>
Q1. I was wondering whether the rows which are having the status column changed to accepted, can somehow also be exported to a csv file.
Q2. Is there a way to have two buttons, one for accepting and the other one for rejecting as each claim has three possible statuses: pending, accepted, rejected
Update: the claim table is as follows: http://sqlfiddle.com/#!9/5ce91
create table claim (
c_id INT(5) zerofill primary key auto_increment,
claim_ref VARCHAR(7),
employee_id integer NOT NULL REFERENCES employee(emp_id),
transaction_date DATE,
filed_date DATE,
expense_type_id VARCHAR(20) REFERENCES expense_type(expense_type),
client_code_id VARCHAR(7) REFERENCES client(client_code),
project_code_id VARCHAR(10) REFERENCES project(project_code),
narrative VARCHAR(255),
net decimal(6,2),
vat decimal(6,2),
status ENUM('pending', 'rejected', 'accepted'))
ENGINE = InnoDB;
Another bit is that the project ends in about a week so I'd rather not get into updating to PDO or MySQLi although if anyone has a complete solution in one of these it would be just as good, thanks.
I have a create a report form which has 2 dropdown lists and table in above. I want to filter the data and make report with dropdown list options. The first dropdown list has all table names from database and second one has "item" column, and every table has same items values. Like when we make report from table 3 and item option 1. We should make report.
Here is my code for HTML:
<div class="row">
<div class="col-lg-12">
<div class="ibox float-e-margins">
<div class="ibox-title">
<h5>Report by Items</h5>
</div>
<div class="ibox-content">
<form method="post" class="form-horizontal">
<div class="form-group"><label class="col-sm-4 control-label"Stocks</label>
<div class="col-sm-4"><select class="form-control m-b" name="Stock">
<option></option>
<option>Stock</option>
<option>Stock1</option>
<option>Stock2</option>
</select>
</div>
</div>
<div class="form-group"><label class="col-sm-4 control-label">Items</label>
<div class="col-sm-4">
<select class="form-control m-b" name="Items">
<option></option>
<option>Item1</option>
<option>Item2</option>
<option>Item3</option>
<option>Item4</option>
</select>
</div>
</div>
</div>
<div class="col-lg-25">
<div class="ibox float-e-margins">
</div>
</div>
<div class="form-group">
<div class="col-sm-4 col-sm-offset-5">
<button class="btn btn-white" type="submit">Cancel</button>
<button class="btn btn-primary" type="submit" name=submit>Run Report</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
PHP and table code I try is:
<div class="ibox-content">
<table class="table table-striped table-bordered table-hover dataTables-example" >
<thead>
<tr>
<th>No</th>
<th>Quantity</th>
<th>Date</th>
<th>Sold</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<?php
$mysqli = new mysqli( 'localhost', 'user', 'pass', 'mis_db' );
if (mysqli_connect_error()) {
echo mysqli_connect_error();
exit();
}
if($_POST['Stock']=='Stock1')
{
$stck1 = 'Stock1';
}
if($_POST['Stock']=='Stock2')
{
$stck1 = 'Stock2';
}
if($_POST['Stock']=='Stock1')
{
$stck1 = 'Stock3';
}
if($_POST['Items']=='Item1')
{
$itm = 'Item1';
}
if($_POST['Items']=='Item2')
{
$itm = 'Item2';
}
if($_POST['Items']=='Item3')
{
$itm = 'Item3';
}
if (isset($_POST['submit'])) {
$query = 'SELECT * FROM .$stck1 where Items=.$itm';
$data = mysqli_query($mysqli, $query) ;
if (!$data) {
echo("Error description: " . mysqli_error($mysqli));
} else {
while ($row = mysqli_fetch_array($data)) {
echo "<tr>
<td>" . $row['No'] . "</td>
<td>" . $row['Qty'] . "</td>
<td>" . $row['date'] . "</td>
<td>" . $row['Sold'] . "</td>
<td>" . $row['Total'] . "</td>
</tr>";
}
}
}
?>
</tbody>
<tfoot>
</tfoot>
</table>
</div>
I have doubt on MySQL query.
You have some syntax errors and your PHP can be simplified quite a bit.
As you can see I removed the if statements for each item in the dropdowns, not needed. You also had syntax errors in your $query, no . are required for concatenation so I've removed them. Finally, the $stck1 variable in the $query changed to simply $stck.
Try this:
<?php
$mysqli = new mysqli('localhost', 'user', 'pass', 'mis_db');
if (mysqli_connect_error()) {
echo mysqli_connect_error();
exit();
}
$stck = $_POST['Stock'];
$itm = $_POST['Items'];
if (isset($_POST['submit'])) {
$query = "SELECT * FROM $stck WHERE Items = $itm";
$data = mysqli_query($mysqli, $query) ;
if (!$data) {
echo("Error description: " . mysqli_error($mysqli));
} else {
while ($row = mysqli_fetch_array($data)) {
echo "<tr>
<td>" . $row['No'] . "</td>
<td>" . $row['Qty'] . "</td>
<td>" . $row['date'] . "</td>
<td>" . $row['Sold'] . "</td>
<td>" . $row['Total'] . "</td>
</tr>";
}
}
}
?>
Hi coders <3 I am a beginner in php, mysql thing and now i am stuck in one place. I have a table with approve and reject button in it, i want whenever I will will click on approve button, it will change the status of it as approved in the mysql table. How can i do this....through button click
here is my mysql table
here is the UI
and the coding is like this
<?php
// Connect to the database
$dbLink = new mysqli('127.0.0.1', 'root', '', 'hct_db');
if(mysqli_connect_errno()) {
die("MySQL connection failed: ". mysqli_connect_error());
}
// Query for a list of all existing files
$sql = 'SELECT `quote_id`, `name`, `mime`, `size`, `created`, `status` FROM `quote`';
$result = $dbLink->query($sql);
// Check if it was successfull
if($result) {
// Make sure there are some files in there
if($result->num_rows == 0) {
echo '<p>There are no files in the database</p>';
}
else {?>
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>Quote ID</th>
<th>File Name</th>
<th>File</th>
<th>Size</th>
<th>Created</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead><tbody>
<?php
while($row = $result->fetch_assoc()) {
if ($row["status"]=="Not Approved")
{
echo "<tr>";
echo "<td>" . $row['quote_id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['mime'] . "</td>";
echo "<td>" . $row['size'] . "</td>";
echo "<td>" . $row['created'] . "</td>";
echo '<td>Download</td>';
echo "<td><input type='submit' name='submit' value='Approved'></td>";
echo "<td><input type='submit' name='submit' value='Reject'></td>";
echo "</tr>";
}}
?>
</tbody>
</table>
</div>
</div>
<!-- /.table-responsive -->
<?php
$result->free();
}
// Close the mysql connection
$dbLink->close();}
?>
1) You need to use ajax.
2) For every button you can use a form such as:
<form method="post" action="approved.php" target="_self">
<td>
<input type='submit' name='submit' value='Approved'>
<input type='hidden' name='quoteID' value='<?php echo $row['quote_id']?>'>
</td>
</form>
approved.php:
mysqli_connect
$approvedElement = $_POST['quoteID'];
$query = 'UPDATE ... WHERE `Quote ID` = \''.$quoteID.'\' ';
mysqli_query($query);
So before ajax I suggest you to learn basics about GET and POST methods.
In this example you need:
• form, to redirect the user to another page (approved.php, rejected.php)
• $_POST, in the second page to retrieve the ID of the approved element, and use it in the next step
• mysql_query, after you have correctly coded the query and successfully connected to the DB