I hope my question will be clear and perfect enough to not get down votes – or even the closed status.
I have simple HTML form:
<form ..>
<input type="hidden" name="user_id" value="1" />
<input type="text" name="tag[]" value="css" />
<input type="text" name="tag[]" value="php" />
<input type="text" name="tag[]" value="sql" />
...
<input type="text" name="email" value="love@stackoverflow.com" />
</form>
MySQL Schema:
id
user_id
tag
....
I want to update tag for the user_id = 1, So I write this code:
Note: I have just one user in the table.
$user_id = $_POST['user_id'];
$tag = mysql_escape_string($_POST['tag']); // $tag is an array - print $tag
$email = $_POST['email'];
foreach($tag as $value) {
$DB = "UPDATE table SET tag = '$value' AND email = '$email'
WHERE user_id = '$user_id'";
.... // run the query
}
I Get:
id user_id tag
1 1 sql
2 1 sql
3 1 sql
Instead of :
id user_id tag
1 1 css
2 1 php
3 1 sql
I ask If I missed something in the PHP code?
In the first update query, you set all records with a user_id of 1 (all 3 of them) to “css”, then “php”, and the finally “sql”. After the foreach loop is finished, they are all “sql”
You could try this: