Function for deleting row from table using php - php

Trying to solve my problem with deleting rows from generated table.I have a page called report.php that shows all the rows from database and there is option 'delete row'. My problem is when I click on delete nothing happens and it should go on delete.php file. For now couldnt solve this by myself, maybe you see something that I dont. I dont get any errors so I am a little bit confused. Thank you.
report.php
<!DOCTYPE>
<html>
<head>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<head>
<body>
<?php
require_once '../include/functions.php';
require_once '../include/db.php';
$htmltable = "";
$htmltable .= "<table>
<tr>
<th>ID</th>
<th>Ime</th>
<th>Ime slavljenika</th>
<th>Datum</th>
<th>Poruka</th>
<th>Vreme prijave</th>
<th>Obrisi</th>
</tr>";
$prep = $db->prepare("SELECT * from prijavljeni");
$prep->execute();
$prijavljeni = $prep->fetchAll();
foreach($prijavljeni as $prijavljen => $row) {
$htmltable.= '<tr>
<td>'.$row['prijavljeni_id'].'</td>
<td>'.$row['ime'].' </td>
<td>'.$row['ime_slavljenika'].' </td>
<td>'.$row['datum'] .'</td>
<td>'.$row['poruka'].' </td>
<td>'.$row['vreme'].'</td>
<td><a href="delete.php?prijavljeni_id=<?php echo $prijavljeni["prijavljeni_id"]; ?>Delete</a></td>
</tr>';
}
$htmltable.='</table>';
echo $htmltable;
?>
<div>
<button onclick="return email()">Posalji</button>
<div id="emailporuka">
</div><br><br>
</div>
<p align="center">Logout</p>
<script>
function email(){
$.ajax({
type:"post",
url: "email.php",
success: function(data){
//window.alert(data);
document.getElementById("emailporuka").innerHTML = data;
},
error: function (req, status, err) {
console.log('Something went wrong', status, err);
}
})
return false;
}
</script>
</body>
</html>
delete.php
<?php
include('../include/db.php');
$prijavljeni_id=$_GET['prijavljeni_id'];
$result = $db->prepare("DELETE FROM prijavljeni WHERE prijavljeni_id= :prijavljeni_id");
$result->bindParam(':prijavljeni_id', $prijavljeni_id);
$result->execute();
header("location: izvestaj.php");
?>

You just need to change this in your $htmltable. (there was an error in appending strings in last block).
$htmltable.= '<tr>
<td>'.$row['prijavljeni_id'].'</td>
<td>'.$row['ime'].' </td>
<td>'.$row['ime_slavljenika'].' </td>
<td>'.$row['datum'] .'</td>
<td>'.$row['poruka'].' </td>
<td>'.$row['vreme'].'</td>
<td>Delete</td>
</tr>';

Update your code
$result->bindParam(':prijavljeni_id', $prijavljeni_id);
To
$result->bindParam('prijavljeni_id', $prijavljeni_id);
Remember one thing always avoid : before on bindParam method but we using : on prepareQuery to indicate replaceable value.

Related

How to take value of id from generated php table and delete row with that id

I have problem with delete function. I am trying to delete specific row from table, to allow admin to delete row he wants, row with certain id. I think there is problem in my script with getting value of id.
With these two scripts, I get deleted all the values from table when I click on delete option in any row, so thats why I think that I am having problem with taking value of id. Still couldnt solve this by myself so I decided to ask you, do you maybe see something that I dont?
report.php is a page with table that includes all the rows from database table, here is the code:
<!DOCTYPE>
<html>
<head>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<style>
table {
border-collapse: collapse;
}
table, th, td {
border: 1px solid gray;
padding:5px;
}
tr:hover {background-color: #f5f5f5}
</style>
<head>
<body>
<?php
require_once '../include/functions.php';
require_once '../include/db.php';
$htmltable = "";
$htmltable .= "<table>
<tr>
<th>ID</th>
<th>Ime</th>
<th>Ime slavljenika</th>
<th>Datum</th>
<th>Poruka</th>
<th>Vreme prijave</th>
<th>Obrisi</th>
</tr>";
$prep = $db->prepare("SELECT * from prijavljeni");
$prep->execute();
$prijavljeni = $prep->fetchAll();
foreach($prijavljeni as $prijavljen => $row) {
$htmltable.= '<tr>
<td>'.$row['prijavljeni_id'].'</td>
<td>'.$row['ime'].' </td>
<td>'.$row['ime_slavljenik'].' </td>
<td>'.$row['datum'] .'</td>
<td>'.$row['poruka'].' </td>
<td>'.$row['vreme'].'</td>
<td>Delete</td>
</tr>';
}
$htmltable.='</table>';
echo $htmltable;
?>
<div>
<button onclick="return email()">Posalji</button>
<div id="emailporuka">
</div><br><br>
</div>
<p align="center">Logout</p>
<script>
function email(){
$.ajax({
type:"post",
url: "email.php",
success: function(data){
//window.alert(data);
document.getElementById("emailporuka").innerHTML = data;
},
error: function (req, status, err) {
console.log('Something went wrong', status, err);
}
})
return false;
}
</script>
</body>
</html>
delete.php
<?php
include('../include/db.php');
include('../include/functions.php');
$prijavljeni_id=$_GET['prijavljeni_id'];
$result = $db->prepare("DELETE FROM prijavljeni WHERE prijavljeni_id = prijavljeni_id");//brise sve reove
$result->bindParam('prijavljeni_id', $prijavljeni_id);//brise sve redove
$result->execute();
header("location: izvestaj.php");
?>
You have issue in prepare statement
Change
$result = $db->prepare("DELETE FROM prijavljeni WHERE prijavljeni_id = prijavljeni_id");//brise sve reove
$result->bindParam('prijavljeni_id', $prijavljeni_id);
To
$result = $db->prepare("DELETE FROM prijavljeni WHERE prijavljeni_id = :prijavljeni_id");// Need to add : before bind param name//brise sve reove
$result->bindParam(':prijavljeni_id', $prijavljeni_id);
According to your code prijavljeni_id = prijavljeni_id so this condition become always true so your all records are being deleted
Also change <a> as below. add $row instead of $prijavljeni
<td>Delete</td>

Error retrieving indexes using $_POST from skeleton table in Ajax. Undefined index

So, I'm trying to make a live interactive table that can do active CRUD, but for some reason, the values I'm trying to insert for the add feature are not correctly being read to $_POST.
Here is the table:
<?php
include "../db.php";
$connection = DB::CreateConnection();
$output = '';
$sql = "SELECT * FROM users ORDER BY userid ASC";
$result = mysqli_query($connection, $sql);
$output .= '
<div class="table-responsive">
<table class="table table-bordered">
<tr>
<th width="10%">Id</th>
<th width="40%">First Name</th>
<th width="40%">Last Name</th>
<th width="10%">Delete</th>
</tr>';
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.
$row["userid"].'</td>
<td class="userfirstname" data-id1="'.$row["userid"].'" contenteditable>'.$row["userfirstname"].'</td>
<td class="userlastname" data-id2="'.$row["userid"].'" contenteditable>'.$row["userlastname"].'</td>
<td><button type="button" name="delete_btn" data-id3="'.$row["userid"].'" class="btn btn-xs btn-danger btn_delete">x</button></td>
</tr>';
}
$output .= '
<tr>
<td></td>
<td id="userfirstname" contenteditable></td>
<td id="userlastname" contenteditable></td>
<td><button type="button" name="btn_add" id="btn_add" class="btn btn-xs btn-success">+</button></td>
</tr>';
}
else
{
$output .= '<tr>
<td colspan="4">Data not Found</td>
</tr>';
}
$output .= '</table>
</div>';
echo $output;
?>
Here it the live data edit file:
<!-- This should be where matt's code goes to edit via popup or live -->
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<div class="container">
<br />
<br />
<br />
<div class="table-responsive">
<h3 align-"center">Live Table</h3>
<br />
<div id="live_data"></div>
</div>
</div>
</body>
<script type="text/javascript">
function fetch_data()
{
$.ajax({
url:"UViewTABLE.php",
method:"POST",
success:function(data){
$('#live_data').html(data);
}
});
}
fetch_data();
$(document).on('click', '#btn_add', function(){
var userfirstname = $('#userfirstname').text();
var userlastname = $('#userlastname').text();
if(userfirstname == '')
{
alert("Enter First Name");
return false;
}
if(userlastname == '')
{
alert("Enter Last Name");
return false;
}
$.ajax({
url:"UViewTABLEinsert.php",
method:"POST",
data:{userfirstname:userfirstname, userlastname:userlastname},
dataType:"text",
success:function(data)
{
alert(data);
fetch_data();
}
})
});
</script>
Here is the insert file:
<?php
include "../db.php";
$connection = DB::CreateConnection();
$sql = "INSERT INTO users (userfirstname, userlastname) VALUES('".$_POST["userfirstname"]."', '".$_POST["userlastname"]."')";
if(mysqli_query($connection, $sql))
{
echo 'Data Inserted';
}
?>
The error I get is:
Notice: Undefined index: userfirstname in
/var/www/html/UOM/UViewTABLEinsert.php on line 4
Notice: Undefined index: userlastname in
/var/www/html/UOM/UViewTABLEinsert.php on line 4
And, I know that means in my edit file, I'm not defining those indexes properly, but I can't see anything wrong with it. I have researched this problem for 3 days, if someone is kind enough to help, please do, evidently I don't know what I'm doing wrong.
Just remove the dataType from ajax options because you are explicitly setting the data type to text, the data is not submitting in key value pair, and after insertion, send response in JSON, may work. For more about $.ajax's dataType option, refer this. If you want to set dataTyp explicitly, then keep the response in same type as set in ajax request.

PHP Ajax not getting the wanted search

A newbie question so please be kind :)
I've made a small search script to lookup values in my database.
But when I put in a search result I'm getting all the files in my database. Not just the name I looked for. I'm not a technical person so I was wondering where my mistake sits.
Main search page
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
function search(){
var name=$("#search").val();
if(name!=""){
$("#result").html("<img src='ajax-loader.gif'/>");
$.ajax({
type:"post",
url:"search-database.php",
data:"name="+search,
success:function(data){
$("#result").html(data);
$("#search").val("");
}
});
}
}
$("#button").click(function(){
search();
});
$('#search').keyup(function(e) {
if(e.keyCode == 13) {
search();
}
});
});
</script>
</head>
<body>
<div id="container">
<input type="text" id="search" placeholder="Zoek hier uw kandidaat...."/>
<input type="button" id="button" value="Zoek" />
<ul id="result"></ul>
</div>
</body>
</html>
search-database.php
<?php
include 'dbconfig.php';
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Gevonden kandidaten</title>
</head>
<body>
<div id="header">
<label>Resultaten</label>
</div>
<div id="body">
<table width="80%" border="1">
<tr>
<td>File Name</td>
<td>File Type</td>
<td>File Size(KB)</td>
<td>View</td>
</tr>
<?php
$search=$_POST['search'];
$query = $pdo->prepare("select * from test where name LIKE '%$search%' LIMIT 0 , 10");
$query->bindValue(1, PDO::PARAM_STR);
$query->execute();
while($results= $query->fetch())
{
?>
<tr>
<td><?php echo $results['name'] ?></td>
<td><?php echo $results['type'] ?></td>
<td><?php echo $results['size'] ?></td>
<td>view file</td>
</tr>
<?php
}
?>
</table>
</div>
</body>
</html>
Replace your search function with
function search(){
var name=$("#search").val();
if(name!=""){
$("#result").html("<img src='ajax-loader.gif'/>");
$.ajax({
type:"post",
url:"search-database.php",
data:"name="+name,
success:function(data){
$("#result").html(data);
$("#search").val("");
}
});
}
}
Here :
var name=$("#search").val();
You're setting your input value to a variable named name, but here :
data:"name="+search,
You're sending a non-existent value named search to your PHP file (in $POST['name']).
Hence, $_POST['search'] is empty, and your SQL request is :
select * from test where name LIKE '%%' LIMIT 0 , 10
Which returns all results.
You should replace your data line in your AJAX call to :
data:"search="+name,

Let a div get a value from a fetch-array-table row after click on a cell with jquery

first I want to say that this question is related to jquery replacewith to get data with Ajax after click on a cell and if this against the rule I am sorry and delete this post.
I tried to get the value of a cell in a MySQL-generated table (with fetch_array). I want to click on a cell in the same and receive the value of the first cell in the row in a div-container.
Thanks to several entries here I found an example for "static" tables. Like this:
<table id="choose-address-table" class="ui-widget ui-widget-content">
<thead>
<tr class="ui-widget-header ">
<th>Name/Nr.</th>
<th>Street</th>
<th>Town</th>
<th>Postcode</th>
<th>Country</th>
<th>Options</th>
</tr>
</thead>
<tbody>
<tr>
<td class="nr"><span>50</span>
</td>
<td>Some Street 1</td>
<td>Glasgow</td>
<td>G0 0XX</td>
<td>United Kingdom</td>
<td class="use-address">
Use
</td>
</tr>
<tr>
<td id="chip" class="nr">49</td>
<td>Some Street 2</td>
<td>Glasgow</td>
<td>G0 0XX</td>
<td>United Kingdom</td>
<td>
<button type="button" class="use-address">Use</button>
</td>
</tr>
</tbody>
</table>
<br><br>
<div id="glasgow">Hello</div>
and
$("#chip").click(function () {
var scotland = $(this).closest("tr").find(".nr").text();
$("#glasgow").html(scotland);
});
http://jsfiddle.net/FnDvL/231/
Works fine. I insert this to my file, php, and get table data from mysql.
This is my code:
<html>
<head>
<script type="text/javascript" charset="utf-8" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(".information").click(function () {
var rodney = $(this).closest("tr").find(".nr").text();
$(".clicked_info").html(rodney);
});
</script>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<?
$con = mysqli_connect('localhost','root','','boerse');
$sql="SELECT short, name FROM markets";
$result = mysqli_query($con,$sql);
?>
<div class="markets">
<?
echo "<table>
<thead border='0'>
<tr>
<th>Index</th>
<th>Name</th>
</tr>
</thead>
<tbody border='1'>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td class='nr'>" . $row['short'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td class='information'>Use</td>";
echo "</tr>";
}
echo "</tbody></table>";
?>
<div class="clicked_info">Hello</div>
</div>
<br><br>
</body>
</html>
Now my problem is that I can click on the td with class information, but div class 'clicked_info' doesn't change. But I did like in the fiddle. So what went wrong? Or where is the problem? It must have to do with the MySQL-matter. But why?
This way I want to check if I got the content from the cell and I can continue working with it (as seen in my post before).
Maybe anyone can help. And again I am sorry if I break rules here.
Thank you to everyone.
You need to wrap your code in document-ready handler
$(document).ready(function () {
$(".information").click(function () {
var rodney = $(this).closest("tr").find(".nr").text();
$(".clicked_info").html(rodney);
});
});
Currently when your script executes there is no $(".information") thus event is not binded properly.
OR
Move your script at the bottom of your page. like
<script>
$(".information").click(function () {
var rodney = $(this).closest("tr").find(".nr").text();
$(".clicked_info").html(rodney);
});
</script>
</body>
OR
You can use event-delegation
$(document).on("click", ".information",function () {
var rodney = $(this).closest("tr").find(".nr").text();
$(".clicked_info").html(rodney);
});

JQuery Barcode Only Generates One Barcode From Query Results

I have finally got a barcode to appear in my table that is generated from a Query. But now I am having an issue getting a code on each of the results from $row[1]. I tried to use PHP-BARCODE but it require too much memory to create all the barcodes. Jquery looks like it uses less memory to create them. Hence why I chose that method.
Any help on this would be AWESOME
My script is mainly PHP with an odd splash from JQUERY.
JQUERY for screen refresh and Barcode Generation. With soon more once i get past this hurdle.
<?php
include('inc/database.php');
// MSSQL Query
$sql = "SELECT warehouse, pick_order_number, order_status, pick_order_type, customer_order_number
FROM pick_order_header
WHERE warehouse = 'XDGM'
AND order_status <> 'Complete'
AND order_status <> 'Closed'
AND pick_order_type <> 'Backorder'
AND customer_order_number LIKE '%1 hr%'";
?>
<!DOCTYPE HTML>
<link rel="stylesheet" type="text/css" href="css/master.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="js/jquery-barcode.js"></script>
<script>
setTimeout(function(){
window.location.reload(1);
}, 5000);
</script>
<html>
<title>Current Orders</title>
<body>
<table>
<?php
// SQLSRV Query
$results = sqlsrv_query( $conn, $sql );
if( $results === false) {
die( print_r( sqlsrv_errors(), true) );
}
echo "
<table border=1>
<tr>
<th>Order Number</th>
<th>Order Status</th>
<th>Order Type</th>
<th>Customer Order</th>
<th>Barcode</th>
</tr>";
while ($row = sqlsrv_fetch_array($results))
{
$odrnum = $row[1];
$odrstatus = $row[2];
$odrtype = $row[3];
$custorder = $row[4];
$barcode = $row[1];
echo "
<tr>
<td>$odrnum</td>
<td>$odrstatus</td>
<td>$odrtype</td>
<td>$custorder</td>
<td>
<div id="bcTarget_'.$odrnum.'"><input type="button" id="bc" name="bc" value="Click Here" /></div>
</td>
<script>
$("#bc").click(function(){$("#bcTarget_'.$odrnum.'").barcode("'.$row[1].'", "code39",{barWidth:2.5, barHeight:30, showHRI: true, bgColor: "#DEF3CA"});});</script>
</tr>';
}
echo "</table>";
?>
</table>
</body>
</html>
I am a complete noob at this, so if you do find a solution could you explain it to me as well so I can learn?
Thanks a bunch
The problem is that you have more then one of the same ids bcTarget and jquery will select the first found within the DOM.
Try to make your ids for the selector unique:
echo '
<tr>
<td>'.$odrnum.'</td>
<td>'.$odrstatus.'</td>
<td>'.$odrtype.'</td>
<td>'.$custorder.'</td>
<td>
<div id="bcTarget_'.$odrnum.'"></div>
</td>
<script>
$(function(){$("#bcTarget_'.$odrnum.'").barcode("'.$row[1].'", "code39",{barWidth:2, barHeight:30});});</script>
</tr>';
u can use this code
<div class="bcTarget" rel="4874214545"></div>
//rel= 'barcode digits'
javascript
$(document).ready(function() {
$(".bcTarget").each(function() {
var bcdigits = $(this).attr('rel');
$(this).barcode(bcdigits, "code39",{barWidth:2, barHeight:30});
});
});

Categories