I am working on an inventory program and I need help on the following:
I have a table that works through selecting data from my Database and showing this in a table.
I now need to make it possible for a user to be able to delete bad/faulty records from the form ( Ofcourse u don't want to everytime have to go in the database itself to remove a record, would be stupid)
My code:
<div id="artikel-container">
<table class="table 1">
<thead>
<title>Inventory Grid.html</title>
<meta charset = "UTF-8" />
<style type = "text/css">
table, td, th {
border: 1px solid black;
}
</style>
</thead>
<tbody>
<?php
$con = mysqli_connect("localhost", "root", "admin", "inventarisdb");
// Check connection
if ( mysqli_connect_errno() ) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query( $con, "SELECT * FROM BCD" );
echo "<table border='0'>
<tr>
<th>Categorie</th>
<th>SerieNummer</th>
<th>MacAdress</th>
<th>ProductCode</th>
<th>Prijs</th>
<th>RekNummer</th>
<th>PaletNummer</th>
<th>Hoeveelheid</th>
<th>Aantekeningen/th>
</tr>";
while( $row = mysqli_fetch_array( $result ) ) {
echo "<tr>";
echo "<td>" . $row['Categorie'] . "</td>";
echo "<td>" . $row['SerieNummer'] . "</td>";
echo "<td>" . $row['MacAdress'] . "</td>";
echo "<td>" . $row['ProductCode'] . "</td>";
echo "<td>" . $row['Prijs'] . "</td>";
echo "<td>" . $row['RekNummer'] . "</td>";
echo "<td>" . $row['PaletNummer'] . "</td>";
echo "<td>" . $row['Hoeveelheid'] . "</td>";
echo "<td>" . $row['Aantekeningen'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</article>
</fieldset>
</tbody>
This code works perfectly!
What i want to achieve is to select Records from this table with a checkbos and delete them by clicking on a delete button in the bottom of the page programmed like this:
<Input type="submit" id="submit" name="submit" value="Verwijderen" />
Or at least with this principle!
(This is the second ever application I wrote, 1st was a webpage, and I have no idea how to research this. I keep finding questions and posts that are just not what i need :S Hope U can help me )
I suggest you to use jQuery ajax so you will be able to delete entry one by one without page reload.
At first include jQuery library in HTML HEADER and the Javascript script file
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="script.js"></script>
script.js:
$(document).ready(function() {
$(".delete").click(function(event) {
var entryid = $(this).closest('tr').find('.entryid').val();
alert('This entry will be deleted: ' + entryid); // this will alert you the value what you get. After finisgin to get correct value you can delete this
$.ajax({
type: "GET",
url: "delete.php",
data: "id=" + entryid
});
$(this).closest('tr').remove();
});
});
in MySQL database must be uniqe ID for every entry named 'id'
Grid.html:
echo "<td>" . $row['Aantekeningen'] . "</td>";
echo '<td><input type="hidden" class="entryid" name="id" value='.$row['id'].' />delete</td>';
delete.php:
// connect to database
$id = $_GET['id'];
$sql="DELETE FROM BCD WHERE id='$id'";
First of all you need to wrap the table into a form
<div id="artikel-container">
<form name="form1" method="post" action="...">
<table class="table 1">
...
echo "</table></form>";
mysqli_close($con);
Next you need the checkboxs on the form $row['ID'] is the primary key of the table
echo "<td>" . $row['Aantekeningen'] . "</td>";
echo "<td><input type='checkbox' name='del[]' value='".$row['ID'] . "></td>";
echo "</tr>";
In the php file called in the form's action attribute:
<?php
if( isset( $_POST['del'] ) {
$del = $_POST['del'];
for(x=0; x<count(del); x++ ) {
$sql = 'DELETE FROM BCD WHERE ID='.$del[$x];
//---execuste the query
}
}
?>
Related
Im trying to make an orders table in PHP where i can choose in a dropdownlist box the driver and the truck to each order from the existing sql db.
i managed to create the dropdownlist inside the table but i dont know how to update the db.
im trying using isset function but im probably not doing it right.
here is the code i made + a screenshot:
screen
<!DOCTYPE html>
<html>
<head>
<title>Orders</title>
<link rel="stylesheet" type="text/css" href="table-general.css">
<style type="text/css">
body
{
font-family: Arial, Verdana, sans-serif;
font-size: 90%;
color: #666;
background-color: #f8f8f8;
}
</style>
</head>
<body>
<h1>Orders</h1>
Done</br></br>
<table class="general">
<tr class="head">
<th>Order_ID</th>
<th>Customer_ID</th>
<th>Driver_ID</th>
<th>Truck_ID</th>
<th>Date</th>
<th>Project_Name</th>
<th>Project_Place</th>
<th>Amount</th>
</tr>
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("login");
$result = mysql_query("select * from orders_table") or die("Failed".mysql_error());
$result2 = mysql_query("select * from trucks_table") or die("Failed".mysql_error());
if(mysql_num_rows($result2))
{
$select= '<select name="select">';
while($record2=mysql_fetch_array($result2))
{
$select.='<option value="'.$record2['TruckID'].'">'.$record2['TruckID'].'</option>';
}
}
$select.='</select>';
while($record = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $record['Order_ID'] . "</td>";
echo "<td>" . $record['Customer_ID'] . "</td>";
echo "<td>" . $record['Driver_ID'] . "</td>";
echo "<td>" . $select . "</td>";
if(isset($_POST['select']))
{
$t=$_POST['select'];
$sql = mysql_query("update orders_table set TruckID='$t' where Order_ID='".$record['Order_ID']."' ");
}
echo "<td>" . $record['Date'] . "</td>";
echo "<td>" . $record['Project_Name'] . "</td>";
echo "<td>" . $record['Project_Place'] . "</td>";
echo "<td>" . $record['Amount'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
You probably need to learn something more about HTTP FORMS, how to send data from client to the server with POST or GET and how to use this data on the server with PHP.
see tutorials like these:
http://www.w3schools.com/php/php_forms.asp
In short - to send data from HTML page to the server, you need to wrap the inputs, selects, textareas in
<form action="url"> </form>
where url is pointing to your server php script, where your handling the form data
then you need to include
<input type="Submit">
which will generate a button on which when the user clicks the data are sent back to the server
I want a checkbox for each query that way when the table is populated with data from the db, there is also a checkbox to confirm that booking for each query result.
I also want to know how to create a script that once a single checkbox has been selected,I click on submit and it takes me to another page that displays values (route_no,to_city,from_city and price) based on the checkbox selected and route_no corresponding to that checkbox selected. route_no is auto_incrementing and is a primary key(values of route_no range from 0-49).
this is my html form
<html>
<head>
<script>
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("GET","display.php?q="+str,true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<form>
<select name="to_city" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="Sydney">Sydney</option>
<option value="Brisbane">Brisbane</option>
<option value="Adelaide">Adelaide</option>
<option value="Newcastle">Newcastle</option>
</select>
</form>
<br>
</body>
</html>
and below is my php file, which has text that shows what I want to acheive. The text has been commented out.
<!DOCTYPE html>
<html>
<head>
<style>
table {
width: 100%;
border-collapse: collapse;
}
table, td, th {
border: 1px solid black;
padding: 5px;
}
th {text-align: left;}
</style>
</head>
<body>
<?php
$q = strval($_GET['q']);
$con = mysqli_connect('localhost','root','','mydb');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"flights");
$sql="SELECT * FROM flights WHERE to_city = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table>
<tr>
<th>Route_no</th>
<th>to_city</th>
<th>from_city</th>
<th>price</th>
<th>Confirm</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['route_no'] . "</td>";
echo "<td>" . $row['to_city'] . "</td>";
echo "<td>" . $row['from_city'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
/// I want a checkbox here for each query that way when the table is populated with data from the db, there is also a checkbox to confirm that booking for each query result.
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
I'll edit the body section of your php code. Try and see
<body>
<?php
$q = strval($_GET['q']);
$con = mysqli_connect('localhost','root','','mydb');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"flights");
$sql="SELECT * FROM flights WHERE to_city = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table>
<tr>
<th>Route_no</th>
<th>to_city</th>
<th>from_city</th>
<th>price</th>
<th>Confirm</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['route_no'] . "</td>";
echo "<td>" . $row['to_city'] . "</td>";
echo "<td>" . $row['from_city'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td><input type=\"checkbox\" name=\"example\" class=\"radio\" value=\"example\"></td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
here is the jQuery part:
$("input:checkbox").change(function(){
var group = ":checkbox[name='"+ $(this).attr("name") + "']";
if($(this).is(':checked')){
$(group).not($(this)).attr("checked",false);
}
});
Well you've already got annotations at the right spot:
echo "<form>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['route_no'] . "</td>";
echo "<td>" . $row['to_city'] . "</td>";
echo "<td>" . $row['from_city'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td><input type='checkbox' name='checkbox_id' value='" . $variable_that_has_id . "'> </td>";
echo "</tr>";
}
echo "</form>";
of course you'll have to replace the $variable_that_has_id with whatever $row[] correlates with the id in your database.
After that you can create a submit button, then use the form to post data to the same page(or a different one) and you can use the $_POST variable to grab whatever values were checked and submitted with the form.
I highly suggest getting familiar with creating forms in html and usage of $_POST since it is relevant to what you're doing.
if you want to allow the user to only select one option change the input type to "radio"
in this siomple code where i have to retrieve from table personnel which is having a column fname and some other columns a s well , i m not able to retrieve by matching fname.
code:->
<!DOCTYPE html>
<html>
<title>fname searched!!!</title>
<body>
<div id="container" style="width:1500px"> <! div for main header ... orange portion>
<div id="header" style="background-color:#FFA500;">
<h1 style="margin-bottom:0;">LIST OF SEARCHED ACTIVE SERVICE PERSONNEL</h1>
<figure>
<img src="personnel_head.jpg" alt="botg" width="1400" height="250">
<figcaption>.....tracking records......</figcaption>
</figure>
</div>
<div id="menu" style="background-color:#FFD700;height:1050px;width:200px;float:left;">
<! div for left side menus ... yellow portion>
candidate_reg_form<br>
view enrolled persons<br>
list of ammunitions<br>
character_certificate<br>
list_dependents<br>
regiments<br>
current serving general<br>
<b>SEARCH<b><br>
</div>
<! div for main container ... grey portion>
<div id="content" style="background-color:#EEEEEE;height:1050px;width:1300px;float:left;">
<BODY>
<fieldset>
<h1>active service personnel's details=>></h1>
<?php
$con=mysqli_connect("localhost","sumit","","ind_army");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$fname2=$_POST['fname2'];
echo"<br>";
$result = mysqli_query($con,"SELECT * FROM 'personnel' WHERE fname=$fname2");
echo "<table border='1'>
<tr>
<th>FNAME</th>
<th>MNAME</th>
<th>LNAME</th>
<th>SERV_NO</th>
<th>SEX</th>
<th>RANK</th>
<th>BDATE       </th>
<th>R_NAME</th>
<th>SALARY</th>
<th>ADDRESS</th>
<th>SUPER_SERV_NO</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['fname'] . "</td>";
echo "<td>" . $row['mname'] . "</td>";
echo "<td>" . $row['lname'] . "</td>";
echo "<td>" . $row['SERV_NO'] . "</td>";
echo "<td>" . $row['sex'] . "</td>";
echo "<td>" . $row['RANK'] . "</td>";
echo "<td>" . $row['bdate'] ."</td>";
echo "<td>" . $row['R_NAME'] . "</td>";
echo "<td>" . $row['salary'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['super_serv_no'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
<br><br>
<font color="orange">enter the service no for knowing his/her service ` `details</font>
<!-- form to get key detail of personnel record in database -->
<form name="form" method="POST" action="http://localhost/search_per2.php">
serv_no<input type="text" name="search"> <br><br>
<input type="submit" value="submit">
</form>
<font color="blue">enter the service no for knowing his/her depedents</font>
<!-- form to get key detail of dependent record in database -->
<form name="form" method="POST" action="http://localhost/dep_search.php">
serv_no<input type="text" name="search2"> <br><br>
<input type="submit" value="submit">
</form>
<LABEL>
<form action="http://localhost/indarmy.php" method="post" >
<form>
<input type="SUBMIT" name="HOME" VALUE="HOME"></label>
</FORM>
</FIELDSET>
<div id="footer" style="background-color:#FFA500;clear:both;text-align:center;">
Copyright © indarmy.com</div>
</div>
</body>
</html>
and the error it is giving is:-->
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\search_fname.php on line 66
Change
while($row = mysqli_fetch_array($result))
to:
while($row = mysqli_fetch_assoc($result))
If you loop fetch the result as an array you have loop through the array, whereas getting the row as associated array you will be able to reference the columns as you are doing now in your loop.
To see what's the error put:
if(!$result)
{
echo "error: ".mysqli_error($con);
}
right after the query execution.
It's probably something in the SQL.
Consider also mysqli_error_list
One you think you can definitely try is cleaning up your query just a tad bit by adding quotes around your search string $fname. Im pretty sure cant use single quotes, so i replaced those with backticks.
$result = mysqli_query($con,"SELECT * FROM `personnel` WHERE fname = '$fname2' ");
Try Query Like below:
$result = mysqli_query($con,"SELECT * FROM `personnel` WHERE fname='$fname2'");
I have a table coded in php which pulls in data from mysql database and displays it in a html table . Now when i click on a row I need certain textboxes to be filled.
What is the best approach for this: is it using an array or ajax,html and php,
<?php
$default = "<img src='http://localhost/on.png' width='24' height='24'/>";
$default1 = "<img src='http://localhost/of.png' width='24' height='24'/>";
$con = mysql_connect("*****","******","******");
if (!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db ("*****",$con);
$sql= "select act.*
from audit_activity as act
inner join (
select user_id, max(timestamp) as max_ts
from activity
group by user_id) as a on act.user_id=a.user_id and act.timestamp=a.max_ts";
$mydata = mysql_query($sql,$con);
echo "<table id='tfhover',table border=0>
<tr>
<th>Users</th>
<th>Status<th>
</tr>";
while($record = mysql_fetch_array($mydata)){
echo "<tr>";
echo "<td>" . $record['user_id'] . "</td>";
if (strtolower(trim($record['activity']))!=strtolower('LOGIN')){
echo "<td>" . $default1 . "</td>";
}else{
echo "<td>" . $default . "</td>";
}
echo "</tr>";
}
echo "</table>";
;
mysql_close($con);
?>
<html>
<script type="text/javascript">
window.onload=function(){
var tfrow = document.getElementById('tfhover').rows.length;
var tbRow=[];
for (var i=1;i<tfrow;i++) {
tbRow[i]=document.getElementById('tfhover').rows[i];
tbRow[i].onmouseover = function(){
this.style.backgroundColor = '#ffffff';
};
tbRow[i].onmouseout = function() {
this.style.backgroundColor = '#d4e3e5';
};
}
};
</script>
<head>
</head>
<body>
Total exams taken : <br>
<input type="text" name="fname"/>
</body>
</html>
I have a code that populates data from a mysql table into an html table. I also have a text box and a button at the end of each row. I want to send all the variables in the row including the text in the textbox to update.php. Could not do that somehow. Here is the code I am trying. Please help. I can send it through GET method. But I want to use POST.
<?php
require 'config.php';
mysql_connect ($dbhost, $dbusername, $dbuserpass);
mysql_select_db($dbname) or die('Cannot select database');
$query = "SELECT * FROM cust_info;";
$result = mysql_query($query) or die(mysql_error());
echo "</br>";
echo "<table border='1'>
<tr>
<th>Pop Code</th>
<th>Open_Date</th>
<th>Maturity_Date</th>
<th>Amount</th>
<th>Balance</th>
<th>Collection Amount</th>
</tr>";
while($row1 = mysql_fetch_array($result))
{
echo "<div class=\"addform\"><form method='post' action=\"update.php?upd=".$row1['pop_code']."\">\n";
echo "<tr>";
echo "<td>" . $row1['pop_code'] . "</td>";
echo "<td>" . $row1['open_date'] . "</td>";
echo "<td>" . $row1['mat_date'] . "</td>";
echo "<td>" . $row1['depoamt'] . "</td>";
echo "<td>" . $row1['balance'] . "</td>";
echo "<td>" . " <input type=\"text\" name=\"amount\"/>\n" . "</td>";
echo "<td>" . " <input type=\"image\" src=\"images/update.png\" alt=\"Update Row\" class=\"update\" title=\"Update Row\">\n" . "</td>";
echo "</tr>";
echo "</form></div>";
}
echo "</table>";
?>
Change your table code to something like this:
echo "<td><input type='hidden' name='pop_code' value='".$row1['pop_code']."'>" . $row1['pop_code'] . "</td>";
It looks like your code isn't actually sending anything, just displaying it on the page. This will display it as well as sending a hidden field when the form is submitted.
Edit: Another way to do it would be to have a single form on your page outside your loop and have the button run a javascript function that copies the data to the form and submits the form. Probably easier the way you have it at the moment, but a javascript like that would be able to pick up other information off the user/page easily and send it via a single form to the next page.
You can make the fields hidden:
echo "<td><input type='hidden' name='pop_code' value='" . $row1['pop_code'] . "' />" . $row1['pop_code'] . "</td>";
echo "<td><input type='hidden' name='open_date' value='" . $row1['open_date'] . "' />" . $row1['open_date'] . "</td>";
your HTML is broken, <table> only allows <thead>, <tbody>, <tfoot> and <tr> as direct children, try fix this first because this will cause your Browser to close Tags automatically