select that particular id when button is pressed in php - php

I am trying to send the id from one page to other and i have achieved that but how do i make sure that when the show is pressed of a particular column only that id is sent.Here is my code.
This is index.php.It displays the rows and a show button against each row.
<?php session_start(); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<h3>Restaurant Categories</h3>
</div>
<div class="row">
<p> Create </p>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<?php
include 'database.php';
$pdo = Database::connect();
$sql = 'SELECT * FROM Categories';
$result = $pdo->query($sql);
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['C_id'] . '</td>';
echo '<td>'. $row['C_name'] . '</td>';
echo '<td>'. $row['C_description'] . '</td>';
echo '<td><a class="btn" href="show_items.php">Shitems</a> </td>';
$_SESSION["id"] = $row['C_id']; <-----I am saving the id here.But it only saves the latest id
echo $_SESSION["id"];
echo '</tr>';
}
Database::disconnect();
?>
</tbody>
</table>
</div>
</div> <!-- /container -->
</body>
</html>

Just embed the ID in the url as a query string:
echo '<td><a class="btn" href="show_items.php?id=' . $row['C_id'] . '">Shitems</a> </td>';
^^^^^^^^^^^^^^^^^^^^

Simple using _GET...
<a class="btn" href="show_items.php">
<a class="btn" href="show_items.php?id=<?=$row['C_id']?>">
Then you can access it via... _GET['id']

First you need from form around button. But you can make this with ajax(if you don't redirect the page) or link

Related

How do I open modal using bootstrap 4 by clicking a hyperlinked table cell and populate data in the model from SQL database using the clicked ID

I have a page which displays data from SQL in a table and on that table one cell is hyperlinked and I want a modal (using bootstrap) to open and fetch data using the ID (the hyperlink value) and populate with data on the body in modal from SQL. I have tried different approaches but none is working well. The modal opens up but its not prominent as if its opening in background. Please help me with this.
Used Links in HEAD:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.9.1.js"></script>
PHP Code
<?php
while ($ticketdata = mysqli_fetch_array($ticketresults))
{
// Print out the contents of the entry
echo '<tbody>';
echo '<th scope="row"><a href="#" data-toggle="modal" data-target="#ticketModal'.$ticketdata['ticketnumber'].'">';
echo $ticketdata['ticketnumber'];
echo '</a>';
echo '</th>';
echo '<td>' . $ticketdata['policynumber'] . '</td>';
echo '<td>' . $ticketdata['registration_number'] . '</td>';
echo '<td>' . $ticketdata['insuredname'] . '</td>';
echo '<td>' . $ticketdata['contactnumber'] . '</td>';
echo '<td>' . $ticketdata['casestatus'] . '</td>';
echo '<td>';
echo '<form method="post">';
echo '<input type="hidden" name="ticketnumber" value="'. $ticketdata['ticketnumber']. '">';
echo '<input type="submit" class="btn btn-danger btn-sm" name="deletedata" value="Delete">';
echo '</form>';
echo '</td>';
echo '</tbody>';
}
?>
How do I pass the ID to the modal and where should I initialize the modal passing the ID (hyperlink) to populate the modal with data using the ID with PHP script?
<table>
<?php
while($ticketdata = mysqli_fetch_array($ticketresults)){
<tr>
<td><a href="#" id="<?php echo $ticketdata['ticketnumber']; ?>" class="view_data" /><?php echo $ticketdata['ticketnumber']; ?></a></td>
<td><?php echo $ticketdata['name']; ?></td>
<td><?php echo $ticketdata['policynumber']; ?></td>
<td><?php echo $ticketdata['registration_number']; ?></td>
<td><?php echo $ticketdata['insuredname']; ?></td>
<td><?php echo $ticketdata['contactnumber']; ?></td>
<td><?php echo $ticketdata['casestatus']; ?></td>
<td>DELETE</td>
</tr>
<?php } ?>
</table>
</body>
</html>
<div id="dataModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Policy Details</h4>
</div>
<div class="modal-body" id="detail">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
Ajax Code:-
<script>
$(document).ready(function(){
$('.view_data').click(function(){
var ticket_id = $(this).attr("id");
$.ajax({
url:"select.php",
method:"post",
data:{ticket_id:ticket_id},
success:function(data){
$('#detail').html(data);
$('#dataModal').modal("show");
}
});
});
});
</script>
select.php:-
<?php
if(isset($_POST["ticket_id"]))
{
$output = '';
$connect = mysqli_connect("localhost", "root", "", "testing");
$query = "SELECT * FROM table_name WHERE id = '".$_POST["ticket_id"]."'";
$result = mysqli_query($connect, $query);
$output .= '
<div class="table-responsive">
<table class="table table-bordered">';
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td width="30%"><label>S. No</label></td>
<td width="70%">'.$row["ticketnumber"].'</td>
</tr>
<tr>
<td width="30%"><label>Ticket Name</label></td>
<td width="70%">'.$row["name"].' Year</td>
</tr>
';
}
$output .= "</table></div>";
echo $output;
}
?>

How to use an HTML button to trigger a function?

I am populating an HTML table with information from a database and want to trigger it with a button.
Can someone help me with this, and perhaps add some links to relevant website with examples?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div method="GET">
<table>
<thead>
<tr>
<th>ID</th>
<th>Nombre</th>
<th>Usuario</th>
</tr>
<?php
include "php/populate.php";
?>
</thead>
</table>
<input type="button" value="button" id="button">
</div>
</body>
</html>
<?php
$result = mysqli_query($enlace,"SELECT * FROM tb_personas");
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['txt_nombre'] . "</td>";
echo "<td>" . $row['txt_usuario'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($enlace);
?>
You need to use jQuery (the easiest way to use ajax)
take a look at this question on stackoverflow how to call a php script on a html button click
additionally you are including your data at table's header, instead they should be included in table's body.
<table>
<thead>
<tr>
<th>ID</th>
<th>Nombre</th>
<th>Usuario</th>
</tr>
</thead>
<tbody>
<?php include "php/populate.php"; ?>
</tbody>
</table>

PHP Database Invalid argument supplied for foreach()

Hi I'm very new to PHP because I just started learning this 2 days ago!
I'm trying to display my second table properly in my index.html file but I get this error: Invalid argument supplied for foreach() Take a look
Any help would be much appreciated
Class
public class Category {
public int Id{get;set;}
public string Name {get;set;}
}
public class Product{
public int Id{get;set;}
public int CategoryId{get;set;}
public String Brand {get;set;}
public String Name {get;set;}
public decimal Price{get;set;
}
index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<h3>PHP CRUD Grid</h3>
</div>
<div class="row">
<p>
Create
</p>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Id</th>
<th>CategoryId</th>
<th>Brand</th>
<th>Name</th>
<th>Barcode</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<?php
include_once 'database.php';
$pdo = Database::connect();
$sql = 'SELECT * FROM product ORDER BY id DESC';
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['id'] . '</td>';
echo '<td>'. $row['category_id'] . '</td>';
echo '<td>'. $row['brand'] . '</td>';
echo '<td>'. $row['name'] . '</td>';
echo '<td>'. $row['barcode'] . '</td>';
echo '<td>'. $row['price'] . '</td>';
echo '<td width=250>';
echo '<a class="btn" href="read.php?id='.$row['id'].'">Read</a>';
echo ' ';
echo '<a class="btn btn-success" href="update.php?id='.$row['id'].'">Update</a>';
echo ' ';
echo '<a class="btn btn-danger" href="delete.php?id='.$row['id'].'">Delete</a>';
echo '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
<div class="container">
<div class="row">
<h3>PHP CRUD Grid</h3>
</div>
<div class="row">
<p>
Create
</p>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>CategoryId</th>
<th>Category Name</th>
</tr>
</thead>
include_once 'database.php';
<?php
$sql2 = 'SELECT * FROM category ORDER BY id DESC';
foreach ($pdo->query($sql2) as $row) {
echo '<tr>';
echo '<td>'. $row['id'] . '</td>';
echo '<td>'. $row['category_name'] . '</td>';
echo '<td width=250>';
echo '<a class="btn" href="readCategory.php?id='.$row['id'].'">Read</a>';
echo ' ';
echo '<a class="btn btn-success" href="updateCategory.php?id='.$row['id'].'">Update</a>';
echo ' ';
echo '<a class="btn btn-danger" href="deleteCategory.php?id='.$row['id'].'">Delete</a>';
echo '</td>';
echo '</tr>';
}
Database::disconnect();
?>
</tbody>
</table>
</div>
</div>
</body>
</html>
Remove the second:
include 'database.php';
or use:
include_once 'database.php';
to prevent double loading of your database file.
You might want to consider not disconnecting and connecting again
You have 6 th elements in the header and 7 td elements in your rows. Try to add a th in the header row.
This implies that the SQL query is failing (PDO returns false if the query fails). The manpage says
If you do not fetch all of the data in a result set before issuing your next call to PDO::query(), your call may fail. Call PDOStatement::closeCursor() to release the database resources associated with the PDOStatement object before issuing your next call to PDO::query().
Edit:
Doesn't look like this is the issue - try adding
echo '<pre>'.print_r($pdo->errorInfo(), true).'</pre>';
after the for each loop, this should output the PDO error details.

Add selected data from one table to another table using 'onclick button HTML

My project is about Allergies and I am trying to add the allergies given in one table "List of Allergies" to another table "MyAllergies" with an onclick button "add" for each allergies given. What I want is when the user clicks on add button for one allergy, that specific allergy should automatically be added to the "MyAllergy.php". Please help me. I have been trying for 2 days and I got no where. However, I tried the query and it was almost correct. I would be so grateful. Thank you. Let me know if you want me to attach the other code or any more questions. :)
AddAllergy.php
<?php
include('Session.php');
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Allergen Details</title>
<div align="center">
<h1>Allergen Information</h1>
</div>
</head>
<body>
<?php
$allergysql = 'SELECT Allergy,Description,Symptoms FROM FoodAllergy';
$retrieve = mysqli_query($connection, $allergysql);
if(!$retrieve)
{
die("Could not get data!".mysqli_error($connection));
}
print "
<table border=\"5\" cellpadding=\"7\" style=\"border-collapse: collapse\" bordercolor=\"#85144b\" width=\"100%\" id=\"AutoNumber2\" bgcolor=\"#7FDBFF\"><tr>
<th width=100>Allergy:</th>
<th width=100>Description:</th>
<th width=100>Symptoms:</th>
<th width=100>Status:</th>
</tr>";
while($rowA = mysqli_fetch_assoc($retrieve))
{
print "<tr>";
print "<td>" . $rowA['Allergy'] . "</td>";
print "<td>" . $rowA['Description'] . "</td>";
print "<td>" . $rowA['Symptoms'] . "</td>"; ?>
<td> <div align ="center"> <button type ="button" onclick="addallergy()">Add</button></div></td>
<script>
function addallergy() {
// WHAT SHOULD I ADD HERE?? //
alert ('Added to your profile!');
}
</script>
<?php
print "</tr>";
}
print "</table>";
?>
</body>
</html>

add/remove data from table from previous page

I have an HTML table being displayed from php data and I'm trying to move data from one table to another based on what is clicked. I cannot get it to move from table to another but I"m not getting any code error.
<html>
<head>
<title>View Requests</title>
</head>
<body>
<div class="breadcrumbs">
<center>
Home · Requests
</center>
</div>
<?php
require_once '../../scripts/app_config.php';
require_once '../../scripts/database_connection.php';
// get results from database
$result = mysql_query("SELECT * FROM temp")
or die(mysql_error());
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th>Username</th> <th></th> <th></th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['User_id'] . '</td>';
echo '<td>' . $row['First_name'] . '</td>';
echo '<td>' . $row['Last_name'] . '</td>';
echo '<td>' . $row['Username'] . '</td>';
echo '<td>Approve</td>';
echo '<td>Delete</td>';
echo "</tr>";
}
// close table>
echo "</table>";
?>
</body>
</html>
PHP EDIT SCRIPT:
<html>
<head>
<title>View Requests</title>
</head>
<body>
<div class="breadcrumbs">
<center>
Home · Requests · All Users
</center>
<?php
require_once '../../scripts/app_config.php';
require_once '../../scripts/database_connection.php';
$id = $_GET['id'];
$sql = ("INSERT INTO users
SELECT * FROM temp WHERE User_id = $id ");
mysql_query($sql)
or die(mysql_error());
?>
</body>
</html>
I needed to use
$id = $_get['id];
and in the insert statement i had to replace id with User_id
<html>
<head>
<title>View Requests</title>
</head>
<body>
<div class="breadcrumbs">
<center>
Home · Requests · All Users
</center>
<?php
require_once '../../scripts/app_config.php';
require_once '../../scripts/database_connection.php';
$id = $_GET['id'];
$sql = ("INSERT INTO users
SELECT * FROM temp WHERE User_id = $id ");
mysql_query($sql)
or die(mysql_error());
?>
</body>
</html>

Categories