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 ,
)
);
Related
I have a really weird issue in my code. To be short, I created a system that has a dashboard called dashboard_engineer.php. This dashboard will display only the first 30 data rows of the SQL database. This dashboard also contains 3 filtered inputs which are:
team
date from
date to
Users can use this filter to find their exact data rows. The user just fills in the input and presses the button Search, and this will redirect the user to dashboard_engineer2.php.
In this dashboard, all the filtered data rows will be displayed. Each data row contains a View and a Remove button. The remove button is used to remove the details of the data row by updating it.
My problem is, at the filtered data, when I click the remove button, the data is update at SQL database, but it will display this error:
PHP Fatal error: Uncaught PDOException: SQLSTATE[42000]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near '10'. in C:\inetpub\wwwroot\tgotworker_testing\pages\dashboard\engineer\dashboard_engineer2.php:157
When I click remove button from a data row that not being filtered (dashboard_engineer.php), the data removed and no error display after that.
Any suggestions on how to resolve this will be appreciated. Below is my code:
dashboard_engineer.php
<form method = 'post' action = 'dashboard_engineer2.php' target="_blank">
<td width="40%">
<select class="form-control" name="team" id="team" required>
<option value="">Please select...</option>
<?php foreach ($data as $row2): ?>
<option value="<?php echo $row2["team_id"]; ?>"><?php echo $row2["fullname"]; ?></option>
<?php endforeach ?>
</select>
</td>
<td width="1%"></td>
<td width="20%"><input type="text" name="from" id="from" class="form-control" placeholder="From" required></td>
<td width="1%"></td>
<td width="20%"><input type="text" name="to" id="to" class="form-control" placeholder="To" required></td>
<td width="1%"></td>
<td width="10%"><button type="submit" class="btn-primary" >Search</button><td>
</form>
</tr>
</table><br>
<div class="row" style='height: 300px; overflow-y: scroll;'>
<div class="col-lg-12 grid-margin stretch-card">
<?php
$query = $conn->query("SELECT TOP 30 * FROM ot_report LEFT JOIN ot_users ON ot_report.badgeid = ot_users.badgeid WHERE ot_users.team_id <> 1 AND ot_report.status = 'yes' ORDER BY ot_report.report_id DESC");
$query -> execute();
$results = $query -> fetchAll(PDO::FETCH_OBJ);
if(empty($results)){
echo "<table class = 'table-bordered' width ='100%'>";
echo "<thead>";
echo "<tr>";
echo "<th width = '5%'>id</th>
<th width = '17%'>Date</th>
<th width = '27%'>Officer/ Asst. Engineer</th>
<th width = '32%'>Task Name</th>
<th width = '12%'>Status</th>
<th width = '7%'>Action</th>
</tr>
</thead>
<tbody >
<tr>
<td colspan='8'>No report at this moment</td>
</tr>
</tbody>
</table>";
}else{
echo "<table class = 'table-bordered' width ='100%'>";
echo "<thead>";
echo "<tr>";
echo "<th width = '5%'>id</th>
<th width = '17%'>Date</th>
<th width = '27%'>Officer/ Asst. Engineer</th>
<th width = '32%'>Task Name</th>
<th width = '12%'>Status</th>
<th colspan = '2' width = '7%'>Action</th>
</tr>
</thead>
<tbody >";
$query = $conn->query("SELECT TOP 30 * FROM ot_report LEFT JOIN ot_users ON ot_report.badgeid = ot_users.badgeid WHERE ot_users.team_id <> 1 AND ot_report.status = 'yes' ORDER BY ot_report.report_id DESC");
while($row = $query->fetch(PDO::FETCH_ASSOC)){
$status=$row['report_status'];
if($status=="Pending")
{
$color="color:blue";
}
else
{
$color="color:green";
}
$report_id = $row['report_id'];
$datereport = $row['report_date'];
$datereport2 = strtotime($datereport);
$report_date = date('d M Y', $datereport2);
$fullname = $row['fullname'];
$task_name = $row['task_name'];
echo "<tr>";
echo "<td>". $report_id. "</td>";
echo "<td>". $report_date . "</td>";
echo "<td>". $fullname . "</td>";
echo "<td>". $task_name . "</td>";
echo "<td align='center' style='$color'><strong>". $status . "</strong></td>";
echo "<td align='center'>";
echo "<form method = 'post' action = 'view_task/view_task.php' target='_blank'>";
echo "<input type = 'hidden' name = 'report_id' value = '".$report_id."'>";
echo "<button type = 'submit' class='btn-primary'>View</button>";
echo "</form>";
echo "</td>";
echo "<td align='center'>";
echo "<form method = 'post' action = 'remove.php' onClick=\"return confirm('Do you want to remove this report?')\">";
echo "<input type = 'hidden' name = 'report_id' value = '".$report_id."'>";
echo "<button type = 'submit' class='btn-danger'>Remove</button>";
echo "</form>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table><br>";
}
?>
dashboard_engineer2.php
<?php
if(isset($_REQUEST["from"], $_REQUEST["to"], $_REQUEST["team"])){
$from = $_REQUEST['from'];
$to = $_REQUEST['to'];
$team = $_REQUEST['team'];
$result = '';
$query = "SELECT * FROM ot_report LEFT JOIN ot_users ON ot_report.badgeid = ot_users.badgeid WHERE ot_report.status = 'yes' AND ot_users.team_id = '".$team."' AND report_date BETWEEN '".$from."' AND '".$to."' ORDER BY ot_report.report_id DESC";
$sql = $conn->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$sql -> execute(); //line 157
if($sql->rowCount() > 0){
echo'
<div><u>PDF View</u></div><br>
<div class="row">
<div class="col-lg-12 grid-margin stretch-card">
<div class="row" style="height: 300px; overflow-y: scroll;">
<div class="col-lg-12 grid-margin stretch-card">
<table class = "table-bordered" width = "100%">
<thead>
<tr>
<th width = "10%"><input type="checkbox" id="checkAl"> All</th>
<th width = "3%">id</th>
<th width = "15%">Date</th>
<th width = "25%">Supervisor</th>
<th width = "30%">Task Name</th>
<th width = "10%">Status</th>
<th colspan = "2" width = "7%">Action</th>
</tr>
</thead>
<tbody>';
$i=0;
while($row = $sql->fetch(PDO::FETCH_ASSOC)){
$datereport = $row['report_date'];
$datereport2 = strtotime($datereport);
$report_date = date('d M Y', $datereport2);
$status=$row['report_status'];
if($status=="Pending"){
$color="color:blue";
}
else {
$color="color:green";
}
$report_id = $row["report_id"];
echo'<tr>';
echo '<td><input type="checkbox" id="checkItem" name="check[]" value='.$row['report_id'].'></td>';
echo '<td>'.$report_id.'</td>';
echo '<td>'.$report_date.'</td>';
echo '<td>'.$row["fullname"].'</td>';
echo '<td>'.$row["task_name"].'</td>';
echo '<td align="center" style='.$color.'><strong>'.$status.'</strong></td>';
echo '<td align="center">';
echo '<form action = "view_task/view_task.php" method = "post" target="_blank">';
echo '<input type = "hidden" name = "report_id" value = "'.$report_id.'">';
echo '<button type = "submit" class="btn-primary">View</button>';
echo '</form>';
echo '</td>';
// echo '<a class="btn-view btn-primary btn-sm" href="view_task/view_task.php?report_id='. $report_id .'" data-toggle="tooltip" >View</a></td>';
echo '<td align="center">';
echo "<form action = 'remove2.php' method = 'post' onClick=\"return confirm('Do you want to remove this reports?')\">";
echo '<input type = "hidden" name = "from" value = "'.$from.'">';
echo '<input type = "hidden" name = "to" value = "'.$to.'">';
echo '<input type = "hidden" name = "team" value = "'.$team.'">';
echo '<input type = "hidden" name = "report_id" value = "'.$report_id.'">';
echo '<button type = "submit" class="btn-danger">Remove</button>';
echo '</form>';
echo '</td>';
echo '</tr>';
$i++;
}
echo '<tr>';
echo '<td><p align="center"><button type="submit" class="btn-danger btn-sm" name="save">DELETE</button></p></td>';
echo '</tr>';
}
else
{
echo '
<table class = "table-bordered" width = "100%">
<thead>
<tr>
<th width = "5%">id</th>
<th width = "12%">Date</th>
<th width = "29%">Supervisor</th>
<th width = "23%">Task Name</th>
<th width = "10%">Status</th>
<th width = "7%">Action</th>
</tr>
<tr>
<td colspan="6">No report found</td>
</tr>';
}
echo '</body></table></div></div>';
}
?>
remove2.php
<?php
include('../../../config/configPDO.php');
$report_id = $_POST['report_id'];
$from = $_POST['from'];
$to = $_POST['to'];
$team = $_POST['team'];
$sql = "UPDATE ot_report SET status = 'no' WHERE report_id=:report_id";
$query = $conn->prepare($sql);
$query->execute(array(':report_id' => $report_id));
header("Location: dashboard_engineer2.php?from='".$_POST["from"]."'&to='".$_POST["to"]."' &team='".$_POST["team"]."'");
?>
Use a parametrized query instead of concatenating variables.
$query = "
SELECT *
FROM ot_report
LEFT JOIN ot_users ON ot_report.badgeid = ot_users.badgeid
WHERE ot_report.status = 'yes'
AND ot_users.team_id = :team
AND report_date BETWEEN :from AND :to
ORDER BY ot_report.report_id DESC";
$sql = $conn->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$sql->bindParam(':team', $team, PDO::PARAM_INT);
$sql->bindParam(':from', $from, PDO::PARAM_INT);
$sql->bindParam(':to', $to, PDO::PARAM_INT);
$sql -> execute();
I found the answer, problem at remove2.php. Below is the code:
<?php
include('../../../config/configPDO.php');
$report_id = $_POST['report_id'];
$from = $_POST['from'];
$to = $_POST['to'];
$team = $_POST['team'];
$sql = "UPDATE ot_report SET status = 'no' WHERE report_id=:report_id";
$query = $conn->prepare($sql);
$query->execute(array(':report_id' => $report_id));
header("Location: dashboard_engineer2.php?from=".$from."&to=".$to."&team=".$team."");
?>
My table is not filling all the data i need. Is missing some columns be filled with form multiple. The column where the th is Renault need to be the same for all the rest are empty.
In next picture, you can see when i select options in dynamic selectbox multiple and submit, is only filling in the first column when the expectable is fill all the columns selected.
Next is the code where is giving the value of the select box multiple.
if ($consulta !=0) {
}//
else{
echo "";
}
#$s1= $_POST['s1'];
if( is_array($s1)){
while (list ($key, $val) = each ($s1)) {
}
}//
else{
echo "";
}
/////////////////
#$s2= $_POST['s2'];
if( is_array($s2)){
?>
Next is the code where data is filled in the html table. That line of code<td align=\"center\" bgcolor='FFFFFF'> need to be associate the result of: <?php echo "$key => $val1\n";?> to which form:
The result of <?php echo "$key => $val1\n";?>
0 => Renault 1 => Opel 2 => Mercedes 3 => Audi
The expected output is missing.
Renault
<form onsubmit=\"return validate();\" id=\"teste\" method=\"post\" action=\"teste.php\">
Opel
<form onsubmit=\"return validate();\" id=\"teste\" method=\"post\" action=\"teste.php\">
Mercedes
<form onsubmit=\"return validate();\" id=\"teste\" method=\"post\" action=\"teste.php\">
Audi
<form onsubmit=\"return validate();\" id=\"teste\" method=\"post\" action=\"teste.php\">
That is dynamic, will get the values from mysql database and i can select which i want.
There is all the table where is filled the data. Static and dynamic.
<table>
<tbody>
<tr>
<th>Parametro:</th>
<?php
while (list($key, $val1) = each ($s2)) {
?>
<th><?= htmlspecialchars($val1, ENT_HTML5 | ENT_COMPAT, 'UTF-8') ?></th>
<?php
echo "$key => $val1\n";
?>
<?php
}
?>
The values down of the Parametro:
{
echo("<tr>
<td align=\"center\" bgcolor='FFFFFF'>$utilizador</td>
<td align=\"center\" bgcolor='FFFFFF'>
<form onsubmit=\"return validate();\" id=\"teste\" method=\"post\" action=\"teste.php\">
");
Code from the form:
$con = mysqli_connect( "localhost","root","","teste" ); // ◄■ CONNECT TO DATABASE.
$dat_menuid = mysqli_query( $con,"SELECT * FROM menu WHERE menuId = '".$menuId."'" ) // ◄■ MENUIDs.
or die( mysqli_error($con) );
$con1 = mysqli_connect( "localhost","root","","teste" ); // ◄■ CONNECT TO DATABASE.
$idtem = mysqli_query($con1, "SELECT MAX(id_tem) as id_tem FROM tem")
or die( mysqli_error($con) );
// Print out result
while($row = $idtem->fetch_assoc())
$toEcho = $row["id_tem"];
//echo $toEcho;
$toEchosave = $toEcho + 1;
$_SESSION['myvar'] = $toEchosave;
$options = array( "0.00","0.05","0.10","0.15","0.20","0.25","0.30","0.35","0.40","0.45","0.50","0.55","0.60","0.65","0.70","0.75","0.80","0.85","0.90","0.95","1" ); // ◄■■ OPTIONS ARE STATIC (ALWAYS THE SAME).
while ( $row_menuid = mysqli_fetch_array( $dat_menuid ) ) // ◄■ DISPLAY <SELECT>s.
{ echo "<select class='sumselect' name='corp_resp&{$row_menuid['menuId']}&{$_SESSION['UtilizadorID']}&{$dateTime}&{$toEchosave}' multiple>"; // ◄■■ CORP_RESP&1,CORP_RESP&2.
foreach ($options as $opt)
echo "<option value='$opt'>$opt</option>\n";
echo "</select>\n";
} ?>
</form>
</td>
</tr>
<?php } ?>
<?php }
}
}
?>
<div id='sum'></div>
<br><br><br>
</tbody>
</table>
follow this code
<?php
$sql = "SELECT * FROM menu WHERE $id = menuIdPai ORDER BY menuId ";
$consulta = mysql_query($sql);
while ($mostrar = mysql_fetch_array($consulta)) {
$id = $mostrar['menuId'];
$utilizador = $mostrar['menuNome'];
?>
<tr>
<td align=\"center\" bgcolor='FFFFFF'>Test1</td>
<td align=\"center\" bgcolor='FFFFFF'>Test2</td>
<td align=\"center\" bgcolor='FFFFFF'>Test3</td>
<td align=\"center\" bgcolor='FFFFFF'>Test4</td>
</tr>
<?php
}
?>
this is main format if work then update your code . you add 2 td but your main th 4 so need 4 td
your this tag not close
<td align="center" bgcolor='FFFFFF'>
<form onsubmit="return validate();" id="teste" method="post" action="teste.php">
so close this in your script
<div id='sum'></div>
<br><br><br>
</form>
</td>
</tr>
move this code before end loop
foreach ($options as $opt)
echo "<option value='$opt'>$opt</option>\n";
echo "</select>\n";
} ?>
</form>
</td>
</tr>
<?php } ?>
<?php }
}
}
?>
<div id='sum'></div>
<br><br><br>
</tbody>
</table>
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.
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'];
<?php
require 'database.php';
$query = "SELECT id, date, ponumber FROM so";
$result = $mysqli->query($query) or die(mysqli_error($mysqli));
if ($result) {
echo "<form method='post' action='delete.php'>";
echo "<table cellspacing='0' cellpadding='15' border='1'>
<th >DELETE</th>
<th >VIEW</th>
<th >ID</th>
<th >DATE</th>
<th >PO NUMBER</th>";
while ($row = $result->fetch_object()) {
$date = $row->date ;
$ponumber = $row->ponumber;
$id = $row->id;
//put each record into a new table row with a checkbox
echo "<tr>
<td>
<input type='checkbox' name='checkbox[]' id='checkbox[]' value=$id />
</td>
<td>
$id
</td>
<td>
view
</td>
<td>
$date
</td>
<td>
$ponumber
</td>
</tr>";
}
echo "</table><p><input id='delete' type='submit' class='button' name='delete'
value='Delete Selected Items'/></p></form>";}
?>
i have a sort of an online order form which enable the sales rep to input sales order,
i have done the insert and delete using the code above now i want every row to be a hyperlink so that when they click view it will display only row that has been clicked, in my code above if you click :view" all the detail will display, how can i display only the row that i will click will display the detail of the record!
you need to pass the id in the url and you need to read it if it's there.
e.g.
<?php
require 'database.php';
$query = "SELECT id, date, ponumber FROM so";
/* Edit 1 */
if (!empty($_GET['id'])) {
$query .= " WHERE id = " . mysql_real_escape_string($_GET['id']);
}
/* Edit 1 end */
$result = $mysqli->query($query) or die(mysqli_error($mysqli));
if($result) {
echo "<form method='post' action='delete.php'>";
echo "<table cellspacing='0' cellpadding='15' border='1'>
<th >DELETE</th><th >VIEW</th><th >ID</th><th >DATE</th><th >PO NUMBER</th>";
while ($row = $result->fetch_object()) {
$date = $row->date ;
$ponumber = $row->ponumber;
$id = $row->id;
//put each record into a new table row with a checkbox
echo "<tr>
<td><input type='checkbox' name='checkbox[]' id='checkbox[]' value=$id /></td>
<td>$id</td>
<td>";
/* Edit 2 */
echo "<a href='view.php?id=$id'>view</a>";
/* Edit 2 End */
echo "</td>
<td>$date</td>
<td>$ponumber</td></tr>";
}
echo "</table><p><input id='delete' type='submit' class='button' name='delete' value='Delete Selected Items'/></p></form>";}
?>
A style suggestion:
Don't do/stop doing this:
echo "<form method='post' action='delete.php'>";
echo ...
while
Where what you are echoing is a static string. Instead, do:
?>
<form method='post' action='delete.php'>
...
<?php
while
it's simply easier to read and maintain.