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.
Related
I am a student with a couple of weeks into PHP. What I am trying to do is Generate A table containing all users messages. Then I need a check box in each row. When the user hits the delete button rows with checkboxes marked will be deleted from table. The issue is that I can not figure out how to assign an individual value to each row of the HTML table to target with PHP. So I will only delete the rows that have been selected. I've only been able to delete all rows.
Here is what I got so far
<?php
//connect to database
include("../partials/.connect.php");
// select rows from contacts
$query = "SELECT * FROM contacts";
// display table headers
echo '<form>
<table width="100%" border="0" cellspacing="4" cellpadding="6">
<tr>
<th class="center">ID</th>
<th class="center">Name</th>
<th class="center">Phone</th>
<th class="center">Email</tdclass=center>
<th class="center">Message</th>
<th class="center"> <button type="submit" name="button1">Delete Selected</button</th>
</tr>';
// create loop to fetch all rows from DataBase
if ($result = $conn->query($query)) {
while ($row = $result->fetch_assoc()) {
$field1name = $row["id"];
$field2name = $row["name"];
$field3name = $row["phone"];
$field4name = $row["email"];
$field5name = $row["message"];
// If delete button is clicked delete user
if(isset($_POST['button1'])) {
$sql = "DELETE FROM contacts WHERE id=$field1name";
if ($conn->query($sql) === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn->error;
}
}
// display table data
echo '<tr>
<td class="center">'.$field1name.'</td>
<td class="center">'.$field2name.'</td>
<td class="center">'.$field3name.'</td>
<td class="center">'.$field4name.'</td>
<td class="center">'.$field5name.'</td>
<td class="center"> <input type="checkbox" id="if_checked" name="if_checked"></td>
</tr>
</form>';
}
$result->free();
}
?>
</body>
</html>
Your code is wrong, you just delete all your data along with your foreach run, so I just want to say keep learn more about programming logic since you're student
ok, for your question you have to put delete query before
$query = "SELECT * FROM contacts";
and your delete query should be based from foreach of checkbox value, and the most important thing is your checkbox name should be an array name since you will post multiple checkbox with the same name
<input type="checkbox" class="if_checked" name="if_checked[]">
and then you can use code like this
..................
include("../partials/.connect.php");
if(isset($_POST['button1'])) {
foreach($_POST['if_checked'] as $id) {
......your delete query
}
}
// select rows from contacts
$query = "SELECT * FROM contacts";
...................
You can use the checkbox as an array where you will store the ids when you select checkboxes for delete, you will loop through checkboxes and will be able to delete all selected rows.
I Was Able to solve this by storing the $row['id'] variable in the checkbox value instead of $Field1name. allowing me to specify which row to delete.
<?php
//connect to database
include("../partials/.connect.php");
// select rows from contacts
$query = "SELECT * FROM contacts";
//if delete button is set
if(isset($_POST['delete'])) {
$selected = $_POST['if_checked'];
//confirm record target
$confirm = mysqli_query($conn, "SELECT * FROM contacts WHERE id = '$selected'") or die("Not Found");
if(mysqli_num_rows($confirm)>0){
//record found delete
$delete_query = mysqli_query($conn, "DELETE from contacts where id = '$selected'")
or die("Not Deleted");
echo "<div><p>Record Deleted</p></div>";
} else {
//record not DataBase
die("Not deleted".mysqli_error());
}
}
// display table headers
echo '<form method="post" action="" role="form">
<table width="100%" border="0" cellspacing="4" cellpadding="6">
<tr>
<th class="center">ID</th>
<th class="center">Name</th>
<th class="center">Phone</th>
<th class="center">Email</th>
<th class="center">Message</th>
<th class="center"><input class="center" type="submit" name="delete" value="Delete"></th>
</tr>';
// create loop to fetch all rows from DataBase
if ($result = $conn->query($query)) {
while ($row = $result->fetch_assoc()) {
$field1name = $row["id"];
$field2name = $row["name"];
$field3name = $row["phone"];
$field4name = $row["email"];
$field5name = $row["message"];
// If delete button is clicked delete user
// display table data
echo '<tr>
<td class="center">'.$field1name.'</td>
<td class="center">'.$field2name.'</td>
<td class="center">'.$field3name.'</td>
<td class="center">'.$field4name.'</td>
<td class="center">'.$field5name.'</td>
<td class="center">
<input type="checkbox" id="if_checked" name="if_checked" value="'.$row['id'].'"></td>
</tr>
</form>';
}
$result->free();
}
?>
</body>
</html>
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>
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 make so a checkbox is added beside each row in a dynamic table? So if i get 5 result from databse i want to display 5 checkbox next to these results.
<table border="2">
<tr>
<th>title</th>
</tr>
<?php
include 'database.php';
$valid_query = "SELECT * FROM agencies ";
$valid_result = mysqli_query($link, $valid_query);
while ($row = mysqli_fetch_array($valid_result)) {
echo '<tr>';
echo '<td>'.$row['title'].'</td>';
<input type = 'checkbox' name = '$row[title]'>
echo '<tr>';
}
?>
</table>
I have been scouring the stack-overflow site and tried most of the similar questions that had been asked/answered, but unfortunately none of the solutions have worked for me so far. I am trying to have a list that is being populated by an sql database (which is working) and then once selecting the item, hitting the "generate" button and grabbing the data from the table and posting ONLY the selected data into the table. Previously I had an issue where the tables where being populated with ALL the data from the weapons list. After working on it some I have am now getting the error "Unknown column (whichever weapon name I choose) in where clause 107 (which I am assuming is my line number). Any suggestions?
Here is the entirety of the form from populating the list from the database to trying to select the weapon from the list.
<form action="#" method="post">
<table class="table">
<thead>
Martial Weapon Name
</thead>
<tr>
<th>
<select name="Choosen">
<?php
echo'<option>Select Weapon</option>';
//Check if at least one row is found
if($result->num_rows >0){
//Loop through results
while($row = $result->fetch_assoc()){
//Display weapon info
$output = $row['weapon_name'];
echo '<option>'.$output.'</option>';
}
}
?>
</select>
</th>
</tr>
</table>
<input class="btn btn-default" type="submit" name="submit" value="Generate">
<h3>
Weapon
</h3>
<table class="table table-striped">
<tr>
<th>
Weapon Name
</th>
<th>
Weapon Type
</th>
<th>
Damage
</th>
</tr>
<?php
if (isset($_POST['submit'])) {
$selected_weapon = $_POST['Choosen'];
$choose = "SELECT
weapon_types_martial.id,
weapon_types_martial.weapon_name,
weapon_types_martial.weapon_type,
weapon_types_martial.weapon_damage
FROM weapon_types_martial WHERE weapon_types_martial.weapon_name = " . $selected_weapon;
$result = $mysqli->query($choose) or die($mysqli->error . __LINE__);
foreach ($result->fetch_assoc() as $item) {
//Display weapon
$show = '<tr>';
$show .= '<td>' . $item['weapon_name'] . '</td>';
$show .= '<td>' . $item['weapon_type'] . '</td>';
$show .= '<td>' . $item['weapon_damage'] . '</td>';
$show .= '</tr>';
//Echo output
echo $show;
}
}
?>
</table>
</form>
And lastly here is the screenshot of what I am getting
1)You will not get data in $_POST['choosen'] As you haven't pass value in dropdown(select)
2)In your database table may be field weapon_name is varchar so you have to pass it into single quote.
Change your code as below:
<form action="#" method="post">
<table class="table">
<thead>
Martial Weapon Name
</thead>
<tr>
<th>
<select name="Choosen">
<?php
echo '<option>Select Weapon</option>';
//Check if at least one row is found
if($result->num_rows >0){
//Loop through results
while($row = $result->fetch_assoc()){
//Display weapon info
$output = $row['weapon_name'];
echo '<option value="'.$output.'">'.$output.'</option>'; //<--------------change here
}
}
?>
</select>
</th>
</tr>
</table>
<input class="btn btn-default" type="submit" name="submit" value="Generate">
<h3>
Weapon
</h3>
<table class="table table-striped">
<tr>
<th>
Weapon Name
</th>
<th>
Weapon Type
</th>
<th>
Damage
</th>
</tr>
<?php
if (isset($_POST['submit'])) {
$selected_weapon = $_POST['Choosen'];
$choose = "SELECT
id,
weapon_name,
weapon_type,
weapon_damage
FROM weapon_types_martial WHERE weapon_name = '$selected_weapon'"; //<--------------change here
$result = $mysqli->query($choose) or die($mysqli->error . __LINE__);
if ($result->num_rows > 0) {
while($item = $result->fetch_assoc()) {
//Display weapon
$show = '<tr>';
$show .= '<td>' . $item['weapon_name'] . '</td>';
$show .= '<td>' . $item['weapon_type'] . '</td>';
$show .= '<td>' . $item['weapon_damage'] . '</td>';
$show .= '</tr>';
//Echo output
echo $show;
}
}
else
{
echo "No data found";
}
}
?>
</table>
</form>
You need to place quotes in your SQL query.
Try:
$choose = "SELECT
weapon_types_martial.id,
weapon_types_martial.weapon_name,
weapon_types_martial.weapon_type,
weapon_types_martial.weapon_damage
FROM weapon_types_martial WHERE weapon_types_martial.weapon_name = '" . $selected_weapon . "'";
Word of advice, this query is very unsafe. I suggest using a framework or library for database queries.
You need to give value inside single quotes,
$choose = "SELECT
weapon_types_martial.id,
weapon_types_martial.weapon_name,
weapon_types_martial.weapon_type,
weapon_types_martial.weapon_damage
FROM weapon_types_martial
WHERE weapon_types_martial.weapon_name = '" . $selected_weapon ."'";
$choose = "SELECT id, weapon_name, weapon_type, weapon_damage FROM weapon_types_martial WHERE weapon_name = '".$selected_weapon."'";
or
$choose = "SELECT weapon_types_martial.id, weapon_types_martial.weapon_name, weapon_types_martial.weapon_type, weapon_types_martial.weapon_damage FROM weapon_types_martial as weapon_types_martial WHERE weapon_types_martial.weapon_name = '" . $selected_weapon ."'";
The options in your select element don't have a value=.. attribute, meaning that nothing is present in your query.
So $_POST['Choosen'] will = ''.
Not to mention that you're trying to pass a string, so your query will need to be wrapped in ':
$choose = "SELECT id, weapon_name, weapon_type, weapon_damage FROM weapon_types_martial WHERE weapon_name = '".$selected_weapon."'";