image button for deleting users from mysql - php

so I've got a problem
$query = "SELECT * FROM users;";
$result = mysql_query($query) or die(mysql_error());
while ($row=mysql_fetch_array($result)) {
?>
<tr>
<td><?php echo $row['fio']; ?></td>
<td><?php echo $row['born_date']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['number']; ?></td>
<td><?php echo $row['work_post']; ?></td>
<td><?php echo $row['inwork_date']; ?></td>
<td><center><form action="delete.php" method="POST"><input type="hidden" name="user_id" value="'.$row['user_id'].'"><input type="Submit" class="deleteButton" value="Delete"></form></center></td>
</tr>
<?php
and in delete.php I have
<?php
if(isset($_POST['Submit'])) {
$query = "DELETE FROM users WHERE user_id='$user_id'";
$result = mysql_query($query) or die(mysql_error());
header("Location: edit.php");
exit;
}
?>
I see list of users - it works! But I can not delete anybody.

You're not correctly inserting the ID due to the fact that the PHP interpreter isn't "active" where you seem to think it is.
As such, change...
<input type="hidden" name="user_id" value="'.$row['user_id'].'">
...to...
<input type="hidden" name="user_id" value="<?php echo $row['user_id']; ?>">
...and all will be well. (It happens to us all occasionally.)
Incidentally, wrapping each delete link in a form is perhaps a bit excessive - is there a reason you don't want to use a "normal" link?
UPDATE
You'll also most likely need to explicitly use the HTTP post variable on your delete page as follows:
$query = "DELETE FROM users WHERE user_id='" . intval($_POST['user_id']) . "'";

<input type="Submit" class="deleteButton" value="Delete">
You need a name on this field to pass your if(isset($_POST['Submit'])) {
<input type="Submit" class="deleteButton" value="Delete" name="Submit">
On a slightly different note, I hope you are doing some sort of check that the user executing the delete call has permissions.

Related

Why last row deleted when refresh page in wordpress

When I refresh page table row deleted and last query deleted when I click on any delete button of other row. Why is this happened?
This is table which I displayed
<?php
global $wpdb;
$table= 'wp_contact_form';
$result = $wpdb->get_results ( "SELECT * FROM $table" );
if(!empty($result)){
foreach ( $result as $print ) {
?>
<tr>
<td><?php echo $print->id;?></td>
<td><?php echo $print->names;?></td>
<td><?php echo $print->emails;?></td>
<td><?php echo $print->gender;?></td>
<td><?php echo $print->age;?></td>
<td><input type="submit" value="Edit" id="" name="update"></td>
<td><input type="submit" value="delete" id="delete" name="delete"></td>
</tr>
<?php
}
}
?>
this is delete query
$id= $print->id;
if(isset($_POST['delete']))
{
$result = $wpdb->delete($table, array('id' => $id));
if(!empty($result))
{
echo "success";
}
}
error screenshot - https://prnt.sc/wdg3xv
You're not specifying which ID should be deleted if $_POST['delete'] is set, right now you're asking WP to just delete something.
You need to wrap your elements in a form and add a field that contains the exact ID of the element you want to delete, like this:
<td>
<input type="submit" name="delete" value="delete" />
<input type="hidden" name="targetItem" value="<?php echo $print->id;?>" />
</td>
Then in your PHP use that field's ID to delete it from the DB:
if (isset($_POST['delete']))
$result = $wpdb->delete($table, array('id' => $_POST['targetItem']));

PHP value wont post

Hello for some reason job_id will not parse from myjobs.php to payment.php and I really cannot see how.Does anyone know why this may be?Similar files seem to work but for some reason this wont I am thinking maybe because I also have html code on payment.php which i haven't done before?
<?php while($row = mysqli_fetch_array($result)):?>
<tr>
<td><?php echo $row['job_id']; ?></td>
<td><?php echo $row['title'];?></td>
<td><?php echo $row['description'];?></td>
<td><?php if($row['accepted']==1 AND $row['start_escrow']==0):?><form action = "payment.php">
<input type="hidden" value="<?php echo $row['job_id']?>" name="job_id" />
<input type="submit" class="btn btn-xlarge btn-block btn-primary" value ="Start Escrow"></input></input><?php endif; ?></td>
<td><?php if($row['start_escrow']==1):?><form action = "review.php">
<input type="hidden" value="<?php echo $row['job_id']?>" name="job_id" />
<input type="submit" class="btn btn-xlarge btn-block btn-primary" value ="Start Escrow"></input></input><?php endif; ?></td>
</tr>
<?php endwhile;?>
</table>
payment.php
<?php
require 'config.php';
$jobid = $_POST['job_id'];
$query = "UPDATE job SET start_escrow = '1' WHERE job_id = '$jobid''";
$success = $conn->query($query);
if (!$success) {
die("Couldn't enter data: ".$conn->error);
}
echo "Thank You For Contacting Us <br>";
$conn->close();
?>
Set your method as POST in form.
<form action = "payment.php" method="POST">
<form action = "review.php" method="POST">

update multiple rows in one sql query

Im new in php. this problem stuck me for two days. ergh. I want to display a table that contain input field so that user can insert the data and update it into database. User can change all the data in the table. I want to update multiple rows at a time, but it ends up updating only 1 row (the last row). Anyone please help me for this. Thnks.
This are the form.
This are the following code.
[<table>
<thead>
<tr>
<td><b>Item Code</b></td>
<td><b>Item Barcode</b></td>
<td><b>Item</b></td>
<td><b>QOH</b></td>
<td><b>Quantity</br>Checked</b></td>
<td><b>Quantity</br>Order</b></td>
</tr>
</thead>
<?php
$Barcode=$_SESSION\["Barcode"\];
$code=$_SESSION\["Code"\];
$itemcode=$_SESSION\["Itemcode"\];
$guid=$_SESSION\["guid"\];
$sql = "SELECT itemmastersupcode.*, itembarcode.*, stock_count_item.*, stock_count.*, po_ex_c.*, d.itemlink_total_qty
FROM itemmastersupcode
WHERE stock_count.SupCode = '$code' and stock_count_item.TRANS_GUID = '$guid'
GROUP BY itemmastersupcode.Itemcode";
$result=mysqli_query($conn2,$sql);
$rowcount = mysqli_num_rows($result);
while($row = mysqli_fetch_assoc($result))
{
?>
<tbody>
<tr>
<td><?php echo $row\["Itemcode"\]; ?></td>
<td><?php echo $row\["Barcode"\]; ?></td>
<td><?php echo $row\["Description"\]; ?></td>
<td><?php echo $row\["itemlink_total_qty"\]; ?></td>
<td><?php echo $row\["qty"\]; ?></td>
<form class="form-inline" role="form" method="POST" id="myForm">
<td><input type="number" name="qty" value=""/></td>
</tr>
</tbody>
<input type="hidden" name="itemcode" value="<?php echo $row\["itemcode"\]; ?>"/>
<input type="hidden" name="supcode" value="<?php echo $_SESSION\["Code"\] ?>"/>
<input type="hidden" name="guid" value="<?php echo $_SESSION\["guid"\] ?>"/>
<?php
}
?>
</table>
<button name="save" type="submit" ><b>SUBMIT</b></button>
</form>
<?php
if (isset($_POST\["save"\]))
{
$itemcode=$_POST\['itemcode'\];
$qty=$_POST\['qty'\];
$supcode=$_POST\['supcode'\];
$guid=$_POST\['guid'\];
$sql = "UPDATE stock_count, stock_count_item SET stock_count.posted = '1', stock_count_item.qty_order = '$qty'
WHERE stock_count.TRANS_GUID = '$guid' AND stock_count_item.Itemcode ='$itemcode'
and stock_count_item.TRANS_GUID = '$guid' ";][1]
An UPDATE query will update all records that are filtered by the WHERE clause. If no WHERE is given, all records are updated:
UPDATE table1 SET field1='value1';
will set the field1 column of all records to value1.
UPDATE table1 SET field1='value1' WHERE field2='value2';
will set the field1 column to value1 or all records where field2 is equal to value2.
So, in your case, the records that are found through the WHERE clause are updated. This is all basic SQL stuff by the way, so I advise you to read up on SQL.
Finally, also use prepared statements in your code to prevent SQL injection.
<table>
<thead>
<tr>
<td><b>Item Code</b></td>
<td><b>Item Barcode</b></td>
<td><b>Item</b></td>
<td><b>QOH</b></td>
<td><b>Quantity</br>Checked</b></td>
<td><b>Quantity</br>Order</b></td>
</tr>
</thead>
<?php
$Barcode=$_SESSION["Barcode"];
$code=$_SESSION["Code"];
$itemcode=$_SESSION["Itemcode"];
$guid=$_SESSION["guid"];
$sql = "SELECT itemmastersupcode.*, itembarcode.*, stock_count_item.*, stock_count.*, po_ex_c.*, d.itemlink_total_qty
FROM itemmastersupcode
WHERE stock_count.SupCode = '$code' and stock_count_item.TRANS_GUID = '$guid'
GROUP BY itemmastersupcode.Itemcode";
$result=mysqli_query($conn2,$sql);
$rowcount = mysqli_num_rows($result);
while($row = mysqli_fetch_assoc($result))
{
?>
<tbody>
<tr>
<td><?php echo $row["Itemcode"]; ?></td>
<td><?php echo $row["Barcode"]; ?></td>
<td><?php echo $row["Description"]; ?></td>
<td><?php echo $row["itemlink_total_qty"]; ?></td>
<td><?php echo $row["qty"]; ?></td>
<form class="form-inline" role="form" method="POST" id="myForm">
<td><input type="number" name="itemcode[<?php echo $row["itemcode"]; ?>][qty]" value=""/></td> //update here
</tr>
</tbody>
<?php
}
?>
</table>
<input type="hidden" name="supcode" value="<?php echo $_SESSION["Code"] ?>"/> //update here
<input type="hidden" name="guid" value="<?php echo $_SESSION["guid"] ?>"/>//update here
<button name="save" type="submit" ><b>SUBMIT</b></button>
</form>
Php content
<?php
if (isset($_POST["save"]))
{
$itemcodes=$_POST['itemcode'];
$qty=$_POST['qty'];
$supcode=$_POST['supcode'];
$guid=$_POST['guid'];
$sql = "UPDATE stock_count, stock_count_item SET
stock_count.posted = '1',
stock_count_item.qty_order = CASE stock_count_item.Itemcode";
foreach( $itemcodes as $itmcode=>$qty)
{
$sql.=" WHEN ".$itmcode." THEN ".$qty
}
$sql.=" END WHERE stock_count.TRANS_GUID =$guid AND stock_count_item.TRANS_GUID=$guid AND stock_count_item.Itemcode IN (".implode(",",array_keys( $itemcodes)).") ";
?>
Hope it will help

Form action not allowing the delete of a record

I had to add a form action to go to a different page to edit a specific record, but with doing that, it won't allow me to delete a record because it is taking me away from it before it will do the query. I am unsure of how to make this work and still get to the new page when I hit the "Edit" button.
<table id="tableid">
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Product</th>
<th>Save</th>
<th>Delete</th>
<th></th>
</tr>
</thead>
<tbody>
<?php
$stmt = $dbc->query("SELECT `id`,`first`,`last`,`product` FROM users");
$stmt->setFetchMode(PDO::FETCH_ASSOC);
while($row = $stmt->fetch()) {
?>
<form method="POST" action="edit-product">
<tr>
<td><?php echo $row['id'];?>"</td>
<td><?php echo $row['first'];?></td>
<td><?php echo $row['last'];?></td>
<td><?php echo $row['product'];?></td>
<input name="id" type="hidden" value="<?php echo $row['id'];?>" readonly>
<input name="first" type="hidden" value="<?php echo $row['first'];?>">
<input name="last" type="hidden" value="<?php echo $row['last'];?>">
<input name="product" type="hidden" value="<?php echo $row['product'];?>">
<td><input name="save" type="submit" value="Save"></td>
<td><div class="delete-class" name="delete" id="<?php echo $row['id']; ?>">Delete</div></td>
<td><input name="edit" type="submit" value="Edit"></td>
</tr>
</form>
<?php } ?>
</tbody>
</table>
PHP DELETE query
if(isset($_POST['delete'])) {
$id = $_POST['id'];
$stmt = $dbc->prepare("DELETE FROM users WHERE id = :id");
$stmt->bindParam(':id', $id);
$stmt->execute();
}
How can I change the markup to still get both the edit (going to another page with the action) and the delete to work on this page?
UPDATE AJAX code:
$(function() {
$(".delete_class").click(function(){
var del_id = $(this).attr('id');
$.ajax({
type:'POST',
url:'delete-product.php',
data:'delete_id='+del_id,
success:function(data) {
if(data) { // DO SOMETHING
} else { // DO SOMETHING }
}
)); //here
});
});
This is not very ideal but one way to do it. You change the HTML markup to create two forms, one for edit case and another one for Delete. The delete case sends the request to the current page like this:
while($row = $stmt->fetch()) {
?>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['first'];?></td>
<td><?php echo $row['last'];?></td>
<td><?php echo $row['product'];?></td>
<td>
<form method="post" action="edit-product">
<input name="id" type="hidden" value="<?php echo $row['id'];?>">
<input name="first" type="hidden" value="<?php echo $row['first'];?>">
<input name="last" type="hidden" value="<?php echo $row['last'];?>">
<input name="product" type="hidden" value="<?php echo $row['product'];?>">
<input name="save" type="submit" value="Edit">
</form>
</td>
<td>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input name="id" type="hidden" value="<?php echo $row['id'];?>">
<input name="delete" type="submit" value="Delete">
</form>
</td>
</tr>
<?php }
?>
</tbody>
</table>
<?php
// the delete code goes here
if(isset($_POST['delete'])) {
$id = $_POST['id'];
$stmt = $dbc->prepare("DELETE FROM users WHERE id = :id");
$stmt->bindParam(':id', $id);
$stmt->execute();
}
?>
I left out the save case but it will be similar to edit case.
Rather than having a form you could just have links instead of form submit buttons. That way each link could take you to a specific page that performs a specific task. By passing query string variables and using $_GET rather than $_POST, you can retain the product ID information.
Example:
<td>Save</td>
<td>Delete</td>
<td>Edit</td>
These individual pages could then perform your tasks in the database. Both save and delete could use Header re-directs to get back to your original page with your product list.
delete_product.php Example:
// delete product from database
$id = $_GET['id']; // Note that it's a $_GET
$stmt = $dbc->prepare("DELETE FROM users WHERE id = :id");
$stmt->bindParam(':id', $id);
$stmt->execute();
// re-direct to original list page
header("Location: product_list.php"); // or whatever you have your list page named.
The edit_product.php page would then have your typical form for making changes to a specific product. You will need to lookup the product again in the database using $_GET['id'] value to match it with.
As it was suggested in the comments, using AJAX would make the whole process appear to be seamless, rather than jumping around between pages and being re-directed.

updating mysql database after an entry is deleted

Working on deleting an entry from my database table.
Currently the delete is working. However it deletes the "id + 1"
My script populates a "delete " button right next to an entry populated from mysql table.
When I hit delete next to an entry, it deletes the item +1.
I think it has to do with the while loop, the way I populate items.
Any ideas on how to fix this?
<?php
while($row = mysqli_fetch_array($result)) : ?>
<tr>
</table border = '1'><br><table border = '1'><th>Subject</th><th>Sender</th> <th>Message</th><th>Delete</th>
<?php
echo "<tr><td>";
echo $row['subject'];
echo "<td>";
echo $row['sender'];
echo "<td>";
echo $row['msgText'];
echo "<td>";
?>
<form action="delete.php" method="post">
<input type="hidden" name="delete_id" value="<?php $_SESSION['id'] = $row['id']; ?>" />
<input type="submit" value="Delete" />
</form>
</tr>
<?php endwhile; ?>
here is the script in delete.php
<?php
session_start();
$id = $_SESSION['id'];
$con=mysqli_connect("localhost","root","","something");
$sql = "DELETE FROM `mailbox` WHERE `id`= '$id'";
$result = mysqli_query($con,$sql);
header('Location: loggedin.php');
?>
<input type="hidden" name="delete_id" value="<?php $_SESSION['id'] = $row['id']; ?>" />
change it to:
<input type="hidden" name="delete_id" value="<?php echo $row['id']; ?>" />
and in your delete.php, simply replace $_SESSION['id'] to $_POST['delete_id']
P/S: By the way, you've forgot to add session_start(); in your form's php, if you do really want to use it, but not a good idea for this time.
The $_SESSION['id'] gets overridden each time in the loop.
Why not just simply use $_POST["delete_id"] to retrieve row id.
Your Table Structure is wrong
<?php
while($row = mysqli_fetch_array($result)) :
?>
<tr>
<th>Subject</th>
<th>Sender</th>
<th>Message</th>
<th>Delete</th>
</tr>
<tr>
<?php
echo "<td>";
echo $row['subject'];
echo "</td><td>";
echo $row['sender'];
echo "</td><td>";
echo $row['msgText'];
echo "</td>";
?>
<td>
<form action="delete.php" method="post">
<input type="hidden" name="delete_id" value="<?php $_SESSION['id'] = $row['id']; ?>" />
<input type="submit" value="Delete" />
</form>
</td>
</tr>
<?php endwhile; ?>
also don't use $_SESSION['id'] while deleting the records. use $_POST['delete_id']

Categories