I have this query, where I want to delete a single row from DB when clicking on delete button. Right now, it's deleting the whole DB row. but I want to delete a single row by ID.
Here's the code
add_action('admin_menu', 'conference_register');
function conference_register(){
add_options_page('Register Conference', 'Conference Registration', 'manage_options', 'conference-registration', 'display_conference');
}
function display_conference(){
echo "<form method='post'>";
echo "<div class='wrap'>";
echo "<h2>Conference Registration User Details</h2>";
global $wpdb;
$results = $wpdb->get_results("SELECT `id`,`email`,`details` FROM `conference_register`"); ?>
<table class="wp-list-table widefat fixed striped pages">
<thead>
<tr>
<th id="posts" class="manage-column column-posts num">ID</th>
<th id="email" class="manage-column column-email">Email</th>
<th id="description" class="manage-column column-description">Details</th>
<th id="posts" class="manage-column column-posts num">Delete</th>
</tr>
</thead>
<tbody id="the-list">
<?php foreach($results as $value){
echo "<tr>";
echo "<td class='posts column-posts'>".$value->id."</td>";
echo "<td class='email column-email'>".$value->email."</td>";
echo "<td class='description column-description'><div id='col-container'>".$value->details."</div></td>";
echo "<td class='posts column-posts'><input type='submit' name='delete_registration' value='delete'/></td>";
echo "</tr>";
if(isset($_POST['delete_registration'])){
$wpdb->delete( 'conference_register', array( 'id' => $value->id ) );
}
}
echo "</tbody></table></div></form>";
}
The code for clicking action is set like this in above code:
if(isset($_POST['delete_registration'])){
$wpdb->delete( 'conference_register', array( 'id' => $value->id ) );
}
Am I missing anything here?
Any help will be appreciated.
P.S. I want to delete a row without using jQuery.
Use this as you are deleting the row if delete_registration is set. You need to set/check it for each row and delete that particular row.
<?php foreach($results as $value){
echo "<tr>";
echo "<td class='posts column-posts'>".$value->id."</td>";
echo "<td class='email column-email'>".$value->email."</td>";
echo "<td class='description column-description'><div id='col-container'>".$value->details."</div></td>";
$delRow = "delete_registration_{$value->id}";
echo "<td class='posts column-posts'><input type='submit' name= $delRow value='delete'/></td>";
echo "</tr>";
if(isset($_POST[$delRow])){
$wpdb->delete( 'conference_register', array( 'id' => $value->id ) );
}
}
You are not passing the id or the database query syntax is not okay
Use this:
echo "<td class='posts column-posts'><input type='submit' name= 'delete_registration_{$value->id}' value='delete'/></td>";
echo "</tr>";
if(isset($_POST[delete_registration_{$value->id}])){
$wpdb->delete( 'conference_register', array( 'id' => $value->id ) );
}
This should work
How I do this is in each row I have a hidden input field and have default value = "x". When user changes any field I use javascript to change the value to "e" and if user clicks "delete" I change the value to "d" and submit the form.
On Submit I use a switch . Loop through all records, if e - edit, d-delete.
Related
I have created custom table in WordPress database and I also created edit form to update the values. Here is my code
<form method="post">
<table class="table">
<thead>
<tr>
<th></th>
<th>Item</th>
<th>Stock</th>
<th>Update</th>
</tr>
</thead>
<tbody>
<?php
$i=1;
global $wpdb;
$results = $wpdb->get_results( 'SELECT * FROM `wp_dvs_stock_details` ORDER BY item ASC', OBJECT );
foreach ($results as $key => $row) {
echo "<tr>";
echo "<td>".$i."</td>";
echo "<td id='item$i'><input type='text' value='{$row->item}' name='item$i'0 id='item$i' class='form-control' /></td>";
echo "<td><input type='number' value='{$row->stock}' name='stock$i' id='stock$i' class='form-control' readonly /></td>";
echo "<td id='change$i'><input class='form-control change' type='number' name='change$i'0 id='change$i' tag=$i /></td>";
echo "</tr>";
$i++;
}
?>
</tbody>
</table>
<input type="submit" name="edit" id="edit" class="btn btn-success" style="float: right;">
</form>
and here is my code to update the fields
if(isset($_POST['edit'])) {
$i = 1;
$table_name = $wpdb->prefix.'dvs_stock_details';
$results = $wpdb->get_results("SELECT * FROM {$table_name} ORDER BY item ASC");
foreach ( $results as $row) {
$item = $_POST['item'.$i.''];
$sql = $wpdb->update($table_name,
array(
'item'=>$item,
array('id'=>$row->id),
array('%s'),
array('%d'))
);
print_r($sql);
$i++;
}
}
When I click submit I get this error.
with Edit Form my intensions are to update item name and stock. Please help.
You can use like this
$wpdb->update($table_name,
array(
'item' => //item value,
'stock' => //stock value,
),
array(
'id' => $row->id ,
)
);
I want to change td color using if statement but somehow my code is not affecting all rows
this is my code :
require_once("../model/materiel.class.php" . "");
$mt=new materiel();
$data=$mt->afficher_tous1();
echo '<table id="customers2" class="table datatable table-striped">';
echo "<thead>
<tr>
<th>Qte disponible</th>
<th>Alert</th>
</tr>
</thead>";
echo "<tbody>";
foreach($data as $t){
echo "<tr>";
if ($t['qte_disponible_m'] == 0){
echo "<td bgcolor='red'>".$t['qte_disponible_m']."</td>";
}else if ($t['qte_disponible_m'] > $t['alert_m']){
echo "<td bgcolor='green'>".$t['qte_disponible_m']."</td>";
}else if ($t['qte_disponible_m'] == $t['alert_m']){
echo "<td bgcolor='yellow'>".$t['qte_disponible_m']."</td>";
}
echo "<td>".$t['alert_m']."</td>";
echo "</tr>";
}
echo "</tbody>";
echo"</table>";
the problem i have see the screenshot below :
If statement is like jumping next row
Add a class with the CSS background-color property (with !important if needed) to the TD instead of bgcolor. The bgcolor gets overwritten by the table-striped class.
I am trying to figure out how to update a "date" result from a query I perform.
Here is the code im using to perform the initial query:
<?php
include ("config.php");
$result = mysqli_query($con,"SELECT * FROM redmine.issues join redmine.projects ON issues.project_id=projects.id where projects.name='".$_SESSION['name']."';");
echo "<table border='0' width='80%' cellpadding='0' cellspacing='2' id='datatable' class='table table-bordered table-condensed table-hover table-striped'>
<tr>
<th width='50%' align='left'>Milestone</th>
<th width='25%' align='left'>Template Date</th>
<th width='15%' align='left'>Due Date</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td align='left'><input class='hidden' type='text' name='subject' id='subject' value='" . $row['subject'] . "'>" . $row['subject'] . "</td>";
echo "<td align='left'>" . $row['created_on'] . "</td>";
echo "<td align='left'><input type='text' name='due_date' id='due_date' value='".$row['due_date']."'></td>";
echo "<td align='left'><input type='submit' value='Submit New Date'></td>";
echo "</tr>";
}
echo "</table>";
echo "<br />";
mysqli_close($con);
?>
This will give me this result:
----------------------------------------
Milestone | Created Date | Due Date
----------------------------------------
Milestone 1 | 01/22/2015 | 01/22/2015
----------------------------------------
I want to be able to then type in a new date in the "Due Date" textbox for each milestone and update the due date in my mysql table for that milestone.
Any help is appreciated!
I now have this script.. but it only seems to update the last mysql query result. for example, if I have 5 milestones retrieved, I can only update the last mile stone date. If I try to update the other 4 milestone dates, they wont update.
<?php
include ("config.php");
// Get values from form
$subject=$_POST['subject'];
$due_date=$_POST['due_date'];
$result = mysqli_query($con,"UPDATE redmine.issues join redmine.projects ON issues.project_id=projects.id SET issues.due_date='$due_date' WHERE issues.subject='$subject' AND projects.name='".$_SESSION['name']."'");
// close connection
mysqli_close($con);
?>
Create a input[type="text"] field like this:
<input type="text" value="something" id="DueDate" data-id="12" onchange="function_update()"/>
In function_update() get current id by data-id="12" and value by id="DueDate" using ajax for onchange event.
I have a script written to grab a row from my database based on current session user, which outputs the row correctly, however I want to insert a small image to be displayed alongside of the echo'd row, and cannot figure out the proper syntax.
if ($row['lifetime']!="")
echo "<div style ='font:12px Arial;color:#2F6054'> Lifetime Member: </div> ".$row['lifetime'];
else
echo '';
?>
basically I want the image to appear right before or after the .$row appears, either or.
You can try:
<?php
if ($row['lifetime'] !== "") {
echo "<div style ='font:12px Arial;color:#2F6054'> Lifetime Member: </div>";
echo $row['lifetime'];
echo "<img src='' alt='' style='width:100px'/>";
}
?>
Just put the HTML for the image into the string you're echoing:
echo "<div style ='font:12px Arial;color:#2F6054'><img src="fill in URL here"> Lifetime Member: </div> ".$row['lifetime'];
You can try as below example
HTML
<table>
<thead>
<tr>
<th>No.</th>
<th>Customer Name</th>
<th>Photo</th>
<th ></th>
</tr>
</thead>
<tbody>
<?php
$tst=0;
$result = mysql_query("select * from acc_cust");
while($row = mysql_fetch_array($result))
{
echo "<tr class='odd gradeX'>";
echo "<td width=5%'>" . $row['ent_no']. "</td>";
echo "<td>" . $row['cust_name']. "</td>";
echo "<td><img src='[path]" . $row['cust_img'] . "' /></td>";
}
?>
</tbody>
</table>
I want to display the results of my select query on my webpage in a table, however the code im using at the moment uses a pre-defined set of columns. My code is this:
$result= mysqli_query($con,"SELECT * FROM Customers where First_name = '$specFirstName'");
echo "<table border=\"1px solid black\" width=\"80%\"><tr><th>Customer ID</th><th >First Name</th><th>Last Name</th></tr>" ;
while($row = mysqli_fetch_array($result)){
echo "<tr><td style=\"text-align:center;\">". $row[('CustomerID')] . "</td><td style=\"text-align:center;\">".$row[('First_name')]."</td><td style=\"text-align:center;\">".$row[('Last_name')];
echo "</td></tr>";
}
echo "</table>"
Now, How can I do the same, but use the database headers in an automatically generated table? Can I also use this method to allow the user to specify the required columns in the query?
Many thanks,
Tommy
You can fetch the fields before the while loop like
$fields = ($result->fetch_fields());
?> <table border="1px solid black" width="80%"><tr> <?php
foreach($fields as $field) {
?> <th><?php echo $field->name; ?></th> <?php
}
?> </tr> <?php
while($row = mysqli_fetch_array($result)){
?><tr><?php
foreach($row as $val) {
?> <td style="text-align: center;"> <?php echo $val; ?> </td> <?php
}
?> </tr> <?php
}
?> </table> <?php
Something like this should work. You only have to define the $columns you want to display in the array map. This also works once you write the code to allow the user to select the columns she/he wants, you will only have to set this map with the selected options.
// Define the columns title and name in this array map.
$columns = array(
'Customer ID' => 'CustomerID',
'First Name' => 'First_name',
'Last Name' => 'Last_name'
);
// Run the query
$result= mysqli_query($con,"SELECT * FROM Customers WHERE First_name = '$specFirstName'");
// Output table header
echo "<table border=\"1px solid black\" width=\"80%\"><tr>";
foreach ($column as $name => $col_name) {
echo "<th>$name</th>";
}
echo "</tr>";
// Output rows
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
foreach ($column as $name => $col_name) {
echo "<td style=\"text-align:center;\">". $row[$col_name] . "</td>";
}
echo "</tr>";
}
// Close table
echo "</table>"