Jump to content

syntax and duplicates


turpentyne

Recommended Posts

I'm figuring out a code that runs through several checkboxes, then checks the mysql table to enter any checked boxes if they don't already exist, and to delete any entries that might exist matching the unchecked boxes.

 

This is as far as I've gotten. I had it working, but it chokes on the table key. I want to skip over existing entries...

 

if (isset($_POST['submit'])){

require ('db.php');

$id1 = $_POST['plant_id'];
$imploder=implode(',', $_POST['option2']); 
$exploder = explode(',', $imploder);

foreach($exploder as $value) {
$add = "INSERT INTO table(plant_id, edible_id) VALUES ($id1, $value) WHERE plant_id = $id AND edible_id != $value";
$delete = "DELETE FROM table WHERE plant_id = $id1 and edible_id NOT IN $exploder";
};

$result2 = mysql_query($add) or die(mysql_error());
if (mysql_affected_rows() > 0) {
   echo '<div style="position:relative; left:16px; top:216px;"><font color="red">updated!</font></div>';
  }
}
?>

Link to comment
Share on other sites

oops! the execute mistake was just from copying/pasting into this forum.. The where clause is what I was trying to use to see if it would work...

 

Everything works, but I don't know how to overlook duplicates. I put a key on the table, and that's what's halting the query.

 

It's a page that has several checkboxes to loop through and a hidden field that generates $id. I'm not sure if I understand the question on $value... I guess I thought that was being created by the " foreach($exploder as $value) " part.

 

here's what I have now. it works, but it's giving me the notice that there is a " Duplicate entry '24-1' for key 'plant_id' ". I'm not sure where to go from here.

if (isset($_POST['submit'])){


require ('db');

$id1 = $_POST['plant_id'];

$imploder=implode(',', $_POST['option2']); 
//echo $imploder."<br>";
$exploder = explode(',', $imploder);

//print_r($exploder);

//$add_plants = array_diff($cats,$mysql_cats);
//$delete_plants = array_diff($mysql_cats,$cats); 

foreach($exploder as $value) {

$add = "INSERT INTO table(plant_id, edible_id) VALUES ($id1, $value)";

$delete = "DELETE FROM table WHERE plant_id = $id1 and edible_id NOT IN $exploder";

$result2 = mysql_query($add) or die(mysql_error());
if (mysql_affected_rows() > 0) {
   echo '<div style="position:relative; left:16px; top:216px;"><font color="red">your plant has been updated</font></div>';
  }
};

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.