I have this form that send tags into my database separated with comas when i click create
POST
$query = "INSERT INTO data(tags) VALUES ('$tags')";
and receive them when i click edit
GET
$query = "SELECT * FROM data WHERE id = $id";
$edit = mysqli_query($dbc, $query);
while($row = mysqli_fetch_assoc($edit)){$tags = $row['tags'];}
POST
$tags = implode(",",$_POST['tags'];
$query = "UPDATE data SET tags= '$tags' WHERE id = $id";
And here is the problem, When i click update button I send both previous selected tags with new selected tags and add them on the previous tags an example to understand what i typed:
- Database Tags: tag1,tag2
- Form Edit Tags: [tag1][tag2]
- Form New Tags: [tag1][tag2][tag3][tag4]
Database Updated Tags: tag1,tag2,tag1,tag2,tag3,tag4
<form method="post" action=""> <select id="tags" name="tags[]" multiple="multiple"> <?php foreach ($tags as $tag) {echo "<option value'$tag' selected>$tag</option>";} ?> <option>tag1</option> <option>tag2</option> <option>tag3</option> <option>tag4</option> </select> <button type="submit" name="update">Submit</button> </form>
$tags = implode(",",$_POST['tags']problem is this section, as you are using$_POSTso it going to send what ever in$_POSTin short your form is the problem, can you post your form code as well - arif_suhail_123