0

so I have in my website a table of clients with several columns such as name, age, etc, etc... and I have added a new column which is "Delete" and I added checkboxes, and there's also a button, what I want to do is, if I select one or more checkboxes, and then click on the button to delete, I want them to be deleted off the website but then on the database I want to a field to be updated from 0 to 1 whereas 0 means the client exists on both website and database and 1 means that it no longer exists on the website but it still exists on database.

I'm new in the area of programming but this is the code I got so far:

$id = $_POST['id'];

if(isset($_POST['option']) == 'check' && ($_POST['button']) == 'submit') {

$query = "UPDATE commande

SET traiter = 1

WHERE id = '%$id%'";

var_dump($query);

}

Now this doesn't work, he is not doing the update correctly, I select the checkbox and then click on the button and it doesn't delete from the website nor updates the database specific field from the right client based on its' id, and I have no idea why!

Can anyone help me out here please?

3
  • You're using LIKE syntax in a simple comparison. Remove the percent signs after WHERE. Also: obligatory Bobby Tables reference. Commented Dec 8, 2015 at 9:18
  • You don't seem to be executing the query? Commented Dec 8, 2015 at 9:20
  • You're right @Epodax and that's the problem, I don't get it why it's not working right Commented Dec 8, 2015 at 9:35

4 Answers 4

2

You can use this by two ways:

1-If more than one id(s) are coming then :

$query = "UPDATE commande SET traiter = 1 WHERE id IN ($id)";

2-If only one id will update at a time then:

$query = "UPDATE commande SET traiter = 1 WHERE id = '$id'";
Sign up to request clarification or add additional context in comments.

Comments

0

if you use = remove %

$query = "UPDATE commande SET traiter = 1 WHERE id = '$id'";

or use LIKE with %

$query = "UPDATE commande SET traiter= 1 WHERE id LIKE '%$id%'";

11 Comments

Using 'like' with $id doesn't make sense
Oh true, lack of attention of mines, already corrected that small detail but still doesn't work, I'm using var_dump to debug it, and when I execute the action I want to do I get this message from var_dump: string(56) "UPDATE commande SET traiter = 1 WHERE id LIKE '%%'"
run this query on UPDATE commande SET traiter = 1 WHERE id = '$id' MySQL and check working fine or not ???? @LeFuture
But usually we use id as an primary key. I don't think id will be like 'abc001'
check first is variable $id = $_POST['id']; contain value or NOT.. debug your script... solution would be there
|
0

Multiple select checkbox for delete

Considering that $id is an array, You can useIN clause to replace many OR conditions.

You can use in clause in where condition like

$query = "UPDATE commande SET traiter = 1 WHERE id IN ($id)";

Reference

Update

You should treat it as an array like this,

 <input name="id[]" type="checkbox" value="1">
 <input name="id[]" type="checkbox" value="2">
 <input name="id[]" type="checkbox" value="3">
 <input name="id[]" type="checkbox" value="4">

Then only, you can take it as an array. So, $_POST['id'] will provide array of selected checkbox.

12 Comments

$id is not an array, I just created a php variable named $id = $_POST['id']; to say that $id is equal to database field 'id'
There are many checkbox, right ? Keep that each checkbox name same and post the data. then you will get array
All the checkboxes already have the same name, I've already covered that, the problem here is directly related to the query
Dude, your multiple condition in query can be done as I shown in my answer. Checkbox name should be like this id[] for each checkbox. So, $_POST['id'] will provide array of selected checkbox.
so basically you're saying, in checkbox I put name="id[]" is that it?
|
0
after you get id/s of delete record 

    $query = "UPDATE commande SET traiter = 0 WHERE id IN ($id)";//For multiple multiple



    $$query = "UPDATE commande SET traiter = 0 WHERE id = '$id'";//for single record
 Set traiter to '0' means deleted record and '1' for active record

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.