HTML button not selecting correct form value - php

HTML button not selecting correct form value.
The following code selects data and displays it correctly in a html table: fname, lname , customer_id and the last column in the table shows a button labelled more info.
The first part of the code works fine.
<table>
<thead>
<tr>
<th>First Name</th> <th>Last Name</th> <th>Customer ID</th><th>Info</th>
</tr>
</thead>
<tbody>
<tr>
<?php
$sql = "select fname, lname,customer_id from customer_address ";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($result)){
$fname = $row['fname'];
$lname = $row['lname'];
$customer_id = $row['customer_id'];
echo" <tr>";
echo"<td>$fname</td>";
echo"<td>$lname</td>";
echo"<td>$customer_id</td>";
echo"<td><input type='hidden' name='customer_id' value='$customer_id'>";
echo "<button type='submit' name='submit'>More Info</button></td>" ;
}
echo" </tr>";
?>
</tbody>
</table>
</form>
With the second part of the code, I want to grab the customer_id when the button is clicked and display it below the table(I am going to uses the variable in a subsequent query to display more information later). The button selects the same customer_id no matter which button is clicked, and it is always the last record in the table. I would appreciate any help in correcting my error.
<?php
$ccf=" ";
if(isset($_POST['submit'])){
$ccf = $_POST['customer_id'];
}
echo "Customer Id selected is:$ccf";
?>

Your elements are all inside one form, so there are multiple elements with name='customer_id'. Place each row inside their own separate <form></form>:
<table>
<thead>
<tr>
<th>First Name</th> <th>Last Name</th> <th>Customer ID</th><th>Info</th>
</tr>
</thead>
<tbody>
<?php
$sql = "select fname, lname,customer_id from customer_address";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($result)){
$fname = $row['fname'];
$lname = $row['lname'];
$customer_id = $row['customer_id'];
echo" <tr>";
echo "<td>$fname</td>";
echo "<td>$lname</td>";
echo "<td>$customer_id</td>";
echo "<td>";
echo "<form action='' method='post'>";
echo "<input type='hidden' name='customer_id' value='$customer_id'>";
echo "<button type='submit' name='submit'>More Info</button>";
echo "</form>";
echo "</td>";
echo" </tr>";
}
?>
</tbody>
</table>

Related

Loading a specific row from database into a html table

As you can see I have developed a html/php page to create orders and I am having issues trying to load data from a table (MySQL) into that table I created in html. What I want is the user to digit an ID (identification) a secondary key like order number and then the whole row goes to my table. where he can edit, delete and save the row. So far i have the table and I was able to load the whole tabe in the html page.
HTML:
<p>What order number would you like to load?<input type="text" id="SKU" maxlength="10" size="10" placeholder="Search for SKU..." title="Type an SKU..."><button onclick="myCreateFunction()">LOAD</button></p>
<table id="itemsTable"> <tr> ID <th>Quantity</th>
<th>Item</th>
<th>SKU</th>
<th>Item Name</th>
<th>Item Price</th>
<th>Subtotal</th>
<th>Cartons Scanned</th>
<th>Individually Scanned</th>
<th>Current Inventory</th>
<th>Location Selected</th>
<th>Image</th>
<th>Edit</th>
</tr>
<tr>
</tr>
</table>
now the php:
<?php
//Step2
$query = "SELECT * FROM item_new WHERE SKU=input ";
mysqli_query($db, $query) or die('Error querying database.');
$result = mysqli_query($db, $query);
$all_property = array(); //declare an array for saving property
//showing property
echo '<table id="itemsTable" class="data-table">
<tr class="data-heading">'; //initialize table tag
while ($property = mysqli_fetch_field($result)) {
echo '<td>' . $property->name . '</td>'; //get field name for header
array_push($all_property, $property->name); //save those to array
}
echo '</tr>'; //end tr tag
//showing data selected by the user (input)
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
foreach ($all_property as $item) {
echo '<td>' . $row[$item] . '</td>'; //get items using property value
}
echo '</tr>';
}
echo "</table>";
database table
script
<script>
function myCreateFunction() {
var input
input = document.getElementById("SKU");
}
You can use following code block for you solution
there will be two ways to fetch data as you want one can be using AJAX which is bit of complex and another is using form.
<p>What order number would you like to load?</p>
<form type="post" action=''>
<input type="text" name='input_field' id="SKU" maxlength="10" size="10" placeholder="Search for SKU..." title="Type an SKU...">
<input type='submit' name='search'>
</form>
<table id="itemsTable">
<tr>
<th>ID</th>
<th>Quantity</th>
<th>Item</th>
<th>SKU</th>
<th>Item Name</th>
<th>Item Price</th>
<th>Subtotal</th>
<th>Cartons Scanned</th>
<th>Individually Scanned</th>
<th>Current Inventory</th>
<th>Location Selected</th>
<th>Image</th>
<th>Edit</th>
</tr>
<?php
//Check if form is submitted
if(isset($_POST['search'])){
$query = "SELECT * FROM item_new WHERE SKU='".$_POST['input_field']."'";
mysqli_query($db, $query) or die('Error querying database.');
$result = mysqli_query($db, $query);
$all_property = array();
//showing property
while ($property = mysqli_fetch_field($result)) {
array_push($all_property, $property->name); //save those to array
}
//showing data selected by the user (input)
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
foreach ($all_property as $item) {
echo '<td>' . $row[$item] . '</td>'; //get items using property value
}
echo "<a href='myeditpage?id=".$_row['id']."' >Edit product</a>"
echo '</tr>';
}
}
?>
</table>
in upper example we first let the user to input the data using input and when user click on search it will submit the form to current URL.
after that we check if user submitted the form or not if submitted the we will fetch data and print on the table.

Deleting record from database with button in PHP

I have to delete a record from database using a button but my delete query does not work. Records are entered in database successfully with insertion query. I followed exact tutorial for php code available on YouTube "How to delete records from database with PHP & MySQL" by "kanpurwebD". The code in tutorial works fine but my code still does not delete record. (I have 2 records entered in database).
My code is as follows:
<div class="container">
<div class="row">
<form action='add_record.php' method='get'><button type='submit' name='id' value='submit' class='btn btn-default'>ADD RECORD</button><br />
</form>
<table class="table table-hover table-responsive">
<thead>
<tr>
<th>Topic #</th>
<th>Name</th>
<th>Admin ID</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
echo '<br />';
$query = "SELECT * FROM tb_topic";
$result = $con->query($query);
if(isset($_POST['submitDeleteBtn'])){
$key = $_POST['keyToDelete'];
$check = "Select * from tb_topic where topic_id = '$key'";
if(mysqli_num_rows($con, $check)>0){
$query_delete = mysqli_query($con,"Delete from tb_topic where topic_id = '$key'");
echo 'record deleted';
}
}
while($query_row = mysqli_fetch_array($result)) {?>
<tr>
<td><?php echo $query_row['topic_id'];?></td>
<td><?php echo $query_row['topic_name'];?></td>
<td><?php echo $query_row['aid'];?></td>
<td><input type = 'checkbox' name = 'keyToDelete' value = "<?php echo $query_row['topic_id'];?>" required></td>
<td><input type="submit" name="submitDeleteBtn" class="btn btn-danger"></td>
</tr>
<?php }
?>
</html>
I got it resolved by using following statement:
if(isset($_GET['delete'])) {
$page = filter_input(INPUT_GET, 'delete', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
$sql = "DELETE FROM tb_topic WHERE topic_id = $page";
}
$_GET() was not taking id as int so I tried typecasting it and it worked for me.

How to Disable Specific Button rows in php

I want to disable the two buttons in a single row if the button will be clicked and won't affect the buttons of another row.
I dont know how to disable an echo button of the table. I want to disable the "Accept" and "Reject" button if one of those was been clicked.I provide a screenshot so that you can easily understand what I mean. Thank you in advance.
Here's my php code. It names as app.php
<?php
//connect to database
$con = mysqli_connect('127.0.0.1','root','');
//select database
mysqli_select_db($con, 'appointment');
//select query
$sql = "SELECT * FROM service";
//Execute the query
$records = mysqli_query($con,$sql)
?>
<html>
<head>
<title>Appointment Schedule</title>
</head>
<body>
<table width = "100%" border = "5px" height = "20%">
<tr align = "left">
<th>First Name</th>
<th>Middle Name</th>
<th>Last Name</th>
<th>Address</th>
<th>Date</th>
<th>Time</th>
<th>Office</th>
<th>Service</th>
<th>Contact No.</th>
<th>Remarks</th>
</tr>
<?php
while($row = mysqli_fetch_array($records))
{
echo "<tr><form action = 'display.php' method = post>";
echo "<input type=hidden name=id value='".$row['ID']."'>";
echo "<td>".$row['fname']."</td>";
echo "<td>".$row['mname']."</td>";
echo "<td>".$row['lname']."</td>";
echo "<td>".$row['address']."</td>";
echo "<td>".$row['date']."</td>";
echo "<td>".$row['time']."</td>";
echo "<td>".$row['office']."</td>";
echo "<td>".$row['services']."</td>";
echo "<td><name = number>".$row['contactno']."</td>";
echo "<td>".$row['remarks']."</td>";
echo "<td><input type =submit value='Accepted' name=accept>";
echo "<td><input type =submit value='Rejected' name=reject>";
echo "</form></tr>";
}
?>
</table>
</body>
</html>
here's my another one php code. It names display.php
<?php
//connect to database
$con = mysqli_connect('127.0.0.1','root','');
//select database
mysqli_select_db($con, 'appointment');
if($_POST['accept'])
{
$sql = "UPDATE service SET remarks = 'Accepted' WHERE ID=$_POST[id]";
}
else if($_POST['reject'])
{
$sql = "UPDATE service SET remarks = 'Rejected' WHERE ID=$_POST[id]";
}
//Execute Query
if(mysqli_query($con,$sql))
header("refresh:1; url=app.php");
else
echo "Unsuccessful";
?>
here's the screenshot of my work
Sample of my database table using php
Kindly try the below code:
Provide the condition to display the buttuon
echo "<td><input type =submit value='Accepted' name=accept>";
echo "<td><input type =submit value='Rejected' name=reject>";
if($row['remarks'] == 'Accepted' || $row['remarks'] == 'Rejected')
{
echo "<td><input type =submit disabled value='Accepted' name=accept>";
echo "<td><input type =submit disabled value='Rejected' name=reject>";
}

Query data from multiple database tables into form table (MYSQL,PHP)

As I'm new to PHP, I want to know that how to put data from different database tables into one table form on the page.
My codes so far as below,
<?php
include('DBconnect.php');
mysql_query("USE onlinerecruitment");
$username =$_SESSION['user'];
$result = mysql_query("SELECT * FROM application_data_file");
$rows = mysql_fetch_array($result, MYSQL_ASSOC);
$pos_id = $rows['Position_ID'];
$resultt = mysql_query("SELECT * FROM position WHERE Position_ID = '".$pos_id."' ");
$resulttt = mysql_query("SELECT * FROM resume_data_file WHERE App_Email = '".$pos_id."' ");
?>
<TABLE border ='1'>
<table style="width:100%">
<tr>
<th>Application ID</th>
<th>Applicant E-mail</th>
<th>Position Selected</th>
<th></th>
<th></th>
<th></th>
</tr>
<?php
while ($row = mysql_fetch_array($result, MYSQL_ASSOC) & $rowss = mysql_fetch_array($resultt, MYSQL_ASSOC)){
echo "<TR>";
echo "<TD>".$row['App_Data_ID']."</TD>";
echo "<TD>".$row['App_Email']."</TD>";
echo "<TD>".$rowss['Position_Name']."</TD>";
echo "<TD><a href='view-app-form.php?app_mail=".$row['App_Email']."'>View Application Data</a></TD>";
echo "<TD><a href='view-resume-form.php?app_mail=".$row['App_Email']."'>View Resume Data</a></TD>";
echo "<TD><a href='view-test-score.php?app_mail=".$row['App_Email']."'>View Testing Score Data</a></TD>";
echo "</TR>";
}
?>
</table>
I will focus the part here.
<TABLE border ='1'>
<table style="width:100%">
<tr>
<th>Application ID</th>
<th>Applicant E-mail</th>
<th>Position Selected</th>
<th></th>
<th></th>
<th></th>
</tr>
<?php
while ($row = mysql_fetch_array($result, MYSQL_ASSOC) & $rowss = mysql_fetch_array($resultt, MYSQL_ASSOC)){
echo "<TR>";
echo "<TD>".$row['App_Data_ID']."</TD>";
echo "<TD>".$row['App_Email']."</TD>";
echo "<TD>".$rowss['Position_Name']."</TD>";
echo "<TD><a href='view-app-form.php?app_mail=".$row['App_Email']."'>View Application Data</a></TD>";
echo "<TD><a href='view-resume-form.php?app_mail=".$row['App_Email']."'>View Resume Data</a></TD>";
echo "<TD><a href='view-test-score.php?app_mail=".$row['App_Email']."'>View Testing Score Data</a></TD>";
echo "</TR>";
}
?>
</table>
But if there is any problem in the section that I didn't focused, I still appreciate your solution.
Thank you in advance.
To do this you would need to use a JOIN in the sql statement.
mysql_query("SELECT resume_data_file.App_Email, position.Position_ID FROM position INNER JOIN resume_data_file ON position.Position_ID = position.Position_ID WHERE position.Position_ID = '".$pos_id."' ");
http://www.w3schools.com/sql/sql_join.asp

hyperlink on every row of mysql data

<?php
require 'database.php';
$query = "SELECT id, date, ponumber FROM so";
$result = $mysqli->query($query) or die(mysqli_error($mysqli));
if ($result) {
echo "<form method='post' action='delete.php'>";
echo "<table cellspacing='0' cellpadding='15' border='1'>
<th >DELETE</th>
<th >VIEW</th>
<th >ID</th>
<th >DATE</th>
<th >PO NUMBER</th>";
while ($row = $result->fetch_object()) {
$date = $row->date ;
$ponumber = $row->ponumber;
$id = $row->id;
//put each record into a new table row with a checkbox
echo "<tr>
<td>
<input type='checkbox' name='checkbox[]' id='checkbox[]' value=$id />
</td>
<td>
$id
</td>
<td>
view
</td>
<td>
$date
</td>
<td>
$ponumber
</td>
</tr>";
}
echo "</table><p><input id='delete' type='submit' class='button' name='delete'
value='Delete Selected Items'/></p></form>";}
?>
i have a sort of an online order form which enable the sales rep to input sales order,
i have done the insert and delete using the code above now i want every row to be a hyperlink so that when they click view it will display only row that has been clicked, in my code above if you click :view" all the detail will display, how can i display only the row that i will click will display the detail of the record!
you need to pass the id in the url and you need to read it if it's there.
e.g.
<?php
require 'database.php';
$query = "SELECT id, date, ponumber FROM so";
/* Edit 1 */
if (!empty($_GET['id'])) {
$query .= " WHERE id = " . mysql_real_escape_string($_GET['id']);
}
/* Edit 1 end */
$result = $mysqli->query($query) or die(mysqli_error($mysqli));
if($result) {
echo "<form method='post' action='delete.php'>";
echo "<table cellspacing='0' cellpadding='15' border='1'>
<th >DELETE</th><th >VIEW</th><th >ID</th><th >DATE</th><th >PO NUMBER</th>";
while ($row = $result->fetch_object()) {
$date = $row->date ;
$ponumber = $row->ponumber;
$id = $row->id;
//put each record into a new table row with a checkbox
echo "<tr>
<td><input type='checkbox' name='checkbox[]' id='checkbox[]' value=$id /></td>
<td>$id</td>
<td>";
/* Edit 2 */
echo "<a href='view.php?id=$id'>view</a>";
/* Edit 2 End */
echo "</td>
<td>$date</td>
<td>$ponumber</td></tr>";
}
echo "</table><p><input id='delete' type='submit' class='button' name='delete' value='Delete Selected Items'/></p></form>";}
?>
A style suggestion:
Don't do/stop doing this:
echo "<form method='post' action='delete.php'>";
echo ...
while
Where what you are echoing is a static string. Instead, do:
?>
<form method='post' action='delete.php'>
...
<?php
while
it's simply easier to read and maintain.

Categories