Wordpress plugin - Deleting only the row I'm clicking? - php

I'm currently working on a Wordpress plugin, and ran into trouble when simple trying have a delete button for each of my rows..
My idea is like many others, that every single row has an ID, and when I click the button that belongs to that row - It should be deleted..
I also tried doing what this guy did...
https://stackoverflow.com/questions/20428783/delet-row-in-custom-wp-db#=
This is what I got:
<form action="" method="POST">
<table class="widefat">
<thead>
<tr>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
</tr>
</thead>
<tbody>
<?php
global $wpdb;
$rows = $wpdb->get_results(
"
SELECT *
FROM skibInfo
WHERE skib_status = 'Forventet skib'
"
);
foreach ($rows as $row)
{
if(isset($_POST['deleteItem'])){
global $wpdb;
$table = $wpdb->prefix."skibInfo";
$skib_nr = $_POST['ID'];
$wpdb->delete( $table, array( 'ID' => $skib_nr), array( '%d' ));
}
echo "<tr>";
echo"<td>".$row->field1."</td>";
echo"<td>".$row->field2."</td>";
echo"<td>".$row->field3."</td>";
echo"<td>".$row->field4."</td>";
echo"<td>".$row->field5."</td>";
echo'<td><input type="submit" value="' . $row->ID . '" name="deleteItem" /></td>';
echo "</tr>";
}
?>
</tbody>
</table>
</form>

You're using the wrong $_POST value as the delete ID. You are checking for 'deleteItem' correctly, but you're using 'ID'. Change:
$skib_nr = $_POST['ID'];
to:
$skib_nr = $_POST['deleteItem'];

Related

How to list PHP table query as non repeatable heading

I have a PHP table which consists of three catagories:
finit "material"
disc "discription"
size "dimension"
Similar 'finit' comes in different 'disc' and 'size'
I would like to display the results in a way that 'finit' are displayed only once depending upon the quantity while 'disc' and 'size' are listed within the table under their associated 'finit'
I am able to display 'finit' only once if it contains multiple discriptions etc.
But the listings are not properly set within the table.
they are listed directly under 'finit' and horizontally listed.
<?php
include("connect.php");
$query = "SELECT * FROM prime order by finit asc";
$info = mysqli_query($conn, $query);
$finit = $rows['finit'];
?>
<table class="table table-striped">
<tr>
<th>Material</th>
<th>Discription</th>
<th>Dimension</th>
</tr>
<?php
while($rows = mysqli_fetch_assoc($info)):
if($rows['finit'] != $finit) {
echo '<tr><td>'.$rows['finit'].'</td></tr>';
$finit = $rows['finit'];
}
echo'<td>'.$rows['disc'].'</td>';
echo'<td>'.$rows['size'].'</td>';
endwhile;
?>
</table>
</div>
</body>
</html>
Your code was not generating correct HTML which may well explain the presentation issues. You would also have lost the first finit value. See comments in code
<?php
include("connect.php");
$query = "SELECT * FROM prime order by finit asc";
$info = mysqli_query($conn, $query);
// this will cause the first finit to be completely lost
//$finit = $rows['finit'];
$finit = NULL;
?>
<table class="table table-striped">
<tr>
<th>Material</th>
<th>Discription</th>
<th>Dimension</th>
</tr>
<?php
while($row = mysqli_fetch_assoc($info)):
echo '<tr>';
if($row['finit'] != $finit) {
echo '<td>'.$row['finit'].'</td>';
$finit = $row['finit'];
} else {
// when you dont print a finit, you need to output something in that column
echo '<td> </td>';
}
echo '<td>' . $rows['disc'] . '</td>';
echo '<td>' . $rows['size'] . '</td>';
echo '</tr>';
endwhile;
?>
</table>
UPDATE
To format the table so that the finit value is always on its own row followed by rows containing only the other 2 columns, you could try.
All you have to remember is that you have to output the same number of <td>'s on each line, so in this case 3, unless you use the colspan="2" attribute, but try that once you have the simple route working.
<?php
include("connect.php");
$query = "SELECT * FROM prime order by finit asc";
$info = mysqli_query($conn, $query);
// this will cause the first finit to be completely lost
//$finit = $rows['finit'];
$finit = NULL;
?>
<table class="table table-striped">
<tr>
<th>Material</th>
<th>Discription</th>
<th>Dimension</th>
</tr>
<?php
while($row = mysqli_fetch_assoc($info)):
if($row['finit'] != $finit) {
echo '<tr><td>'.$row['finit'].'</td><td> </td><td> </td></tr>';
$finit = $row['finit'];
}
echo '<tr><td> </td>';
echo '<td>' . $rows['disc'] . '</td>';
echo '<td>' . $rows['size'] . '</td>';
echo '</tr>';
endwhile;
?>
</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 remove a query from an HTML table and database?

I have a database with 2 tables:
1st table "data" with columns(name , phone , personid)
2nd table "links" with columns (linkid , link , personid)
Personid is the foreign key that connects the two tables with a "one to many" relationship and it's "CASCADE" when DELETE or UPDATE ,
So one person could have more than 1 link.
The HTMl table looks like that:
name phone links
jim 432443 link1
link2
link3
.....
_______________________
john 54545 link1
_______________________
... ..... .....
The code that show database contains on an html table:
$state = $connect->prepare("SELECT data.personid, name, phone, link FROM data JOIN links ON data.personid = links.personid");
$state->execute();
$results = $state->fetchAll(PDO::FETCH_ASSOC);
$data = [];
foreach($results as $result) {
$data[$result['personid']] = [
'name' => $result['name'],
'phone' => $result['phone'],
'links' => [],
];
$data[$result['personid']]['links'][] = $result['link'];
}
<table>
<thead>
<tr>
<th>Name</th>
<th>Phone</th>
<th>Links</th>
</tr>
</thead>
<tbody>
<?php
foreach ($data as $row) {
echo "<tr>";
echo "<th>".$row['name']."</th>";
echo "<td>".$row['phone']."</td>";
echo "<td>".implode('<br/>', $row['links'])."</td>";
echo "<td>";
echo "</td>";
echo "</tr>";
}
?>
</table>
I want to add a delete functionality to delete some of these data , Like a checkBox next to each query and a button called "Delete Selected" for example, I know how to add the checkBox and the button but I don't know the code to achieve this funcunality .
Hope it works for add please check the code shown here.
On html include this:
<form action="action.php" method="POST">
<button type="submit">delete</button>
<table>
<thead>
<tr>
<th><input type="checkbox" name="check_select" readonly="readonly" onclick="toggleselect(this,'delete_data');"></th>
<th>Name</th>
<th>Phone</th>
<th>Links</th>
</tr>
</thead>
<tbody>
<?php
foreach ($data as $row) {
echo "<tr>";
echo '<input type="checkbox" class="check_box" name="checkbox_delete_data[]" value="'.$row['primary_id_of_your_table'].'">';
echo "<td>".$row['name']."</td>";
echo "<td>".$row['phone']."</td>";
echo "<td>".implode('<br/>', $row['links'])."</td>";
echo "<td>";
echo "</td>";
echo "</tr>";
}
?>
</table>
</form>
In Javascript:
function toggleselect(source,chkbox_name)
{
checkboxes = document.getElementsByName('checkbox_'+chkbox_name+'[]');
for(var i=0, n=checkboxes.length;i<n;i++) {
checkboxes[i].checked = source.checked;
}
}
Now you can access the selected check box in your action.php file and use it to delete with query.
For example:
<?php
if (isset($_POST["submit"])) {
$selected_check_boxes = implode(',', $_POST["checkbox_delete_data"]);
$query= $connect->prepare("Delete from table where table.primary id IN ($selected_check_boxes)");
$query->execute();
}
?>
I would recomnend to make whole table in < form > and add some "data-" attribute with personId to each checkbox. Then handle (when form submitted) these attributes with javascript and merge the SQL query like
delete from data where personid (1,2,3, ...)
If you need more help i can write some code, but develop it by own is always better :)
Cheers

How to add a Checkbox beside each row in table

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>

How to make usernames display in rows and columns in a Table

I have this table im trying to display users, being 2 users per 2 columns, then list down. Here is what i have so far:
<?php $result = mysql_query("SELECT * from users WHERE adminlevel='5'");
while($row = mysql_fetch_array($result)) { echo
" <table>
<tr>
<td width='85' align='left'><br><center>". $row['username'] . "</center>
</td>
<td align='right'><center></center>
</td>
</tr>
<td width='85' align='left'><center></center>
</td>
<td align='right'><center></center>
</td>
</table>";
} ?>
This just displays the members as rows going down, and missing out the other column completely. I was hoping that it would display the username in each of the table fields. I also did try putting ". $row['username'] ." in the other fields too, but that just duplicated it.
EDIT:
So iv'e changed it to this, I can't see going down as I only have 2 members, Would this work:
<?php $result = mysql_query("SELECT * from users WHERE adminlevel='5'"); ?>
<table>
<tr>
<?php while($row = mysql_fetch_array($result)) { echo
"<td width='85' align='left'><font size='1px'><center>". $row['username'] . "</font></center></td>
<td align='right'><center></center></td>";
} ?>
</tr>
</table>
example:
try something like this
<table>
<tr>
<th>name</th>
<th>name</th>
</tr>
<?php
$result = mysql_query("SELECT * from users WHERE adminlevel='5'");
$i = 0;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
if ($i == '0') echo "<tr>";
echo "<td>{$row['username']}</td>";
if ($i == '1') echo "</tr>";
$i++;
if($i =='2')$i='0';
}
?>
</table>
I think you're asking why the other fields in your "user" mysql table aren't showing up.
They aren't there because you only asked for the username:
$row['username']
If you have first/last name in your mysql table, you can retrieve it in the same way:
$row['firstname']
$row['lastname']
In your code you got the row as a key/value array like this:
$row = mysql_fetch_array($result)
The "key" is the name of the mysql column, like username, lastname, firstname. And the value is what is stored in the mysql table under that row/column, like joecool, smith, joe.

Categories