Construct your query within the loop:
<?php
...
$id_array = $_POST['id'];
$name_array = $_POST['name'];
$age_array = $_POST['age'];
for (
i < count($id_array); $i++) {
//count($id_array) --> if I input 4 fields, count($id_array) = 4)
$id = mysql_real_escape_string($id_array[
name = mysql_real_escape_string($name_array[
age = mysql_real_escape_string($age_array[
query .= "UPDATE member SET name = '$name', age = '$age' WHERE id = '$id';";
}
mysql_query($query);
}
...
?>
Hope that helps..!
Answer from BizzyBob on Stack OverflowConstruct your query within the loop:
<?php
...
$id_array = $_POST['id'];
$name_array = $_POST['name'];
$age_array = $_POST['age'];
for (
i < count($id_array); $i++) {
//count($id_array) --> if I input 4 fields, count($id_array) = 4)
$id = mysql_real_escape_string($id_array[
name = mysql_real_escape_string($name_array[
age = mysql_real_escape_string($age_array[
query .= "UPDATE member SET name = '$name', age = '$age' WHERE id = '$id';";
}
mysql_query($query);
}
...
?>
Hope that helps..!
Answer to your Question
MySQL updates all rows matching the WHERE clause, so to update multiple rows with the same value, you should use a condition matching all rows. To update all rows, dont set any where clause.
To update multiple rows with different values, you can't, use several queries.
Answer to your issue
In your code, name and $age are arrays so you can not use it in a string, this will not work. You should do the update in your FOR loop.
I advise you to try to respect resource oriented principe that all properties are assigned to their item (with associative array or object).
If you dont check the result, you could do all queries in one using a semi-colon.
sql - Multiple Updates in MySQL - Stack Overflow
mysql - Update Multiple Rows at Once in PHP - Stack Overflow
Updating multiple MYSQL rows with one submit button - PHP - SitePoint Forums | Web Development & Design Community
mysql - php update multi rows at once - Stack Overflow
Videos
Yes, that's possible - you can use INSERT ... ON DUPLICATE KEY UPDATE.
Using your example:
INSERT INTO table (id,Col1,Col2) VALUES (1,1,1),(2,2,3),(3,9,3),(4,10,12)
ON DUPLICATE KEY UPDATE Col1=VALUES(Col1),Col2=VALUES(Col2);
Since you have dynamic values, you need to use an IF or CASE for the columns to be updated. It gets kinda ugly, but it should work.
Using your example, you could do it like:
UPDATE table SET Col1 = CASE id
WHEN 1 THEN 1
WHEN 2 THEN 2
WHEN 4 THEN 10
ELSE Col1
END,
Col2 = CASE id
WHEN 3 THEN 3
WHEN 4 THEN 12
ELSE Col2
END
WHERE id IN (1, 2, 3, 4);
You need to use input arrays. So your inputs should look like this instead (notice the name attribute):
echo "<td>Sex :<input type='text' name='name[]' value='".$row['name']."' /></td>";
Then in PHP $_POST['name'] will be an array:
foreach($_POST['name'] as $name){
Hope this helps...
You should create your sql dynamicly and execute the latest sql sentence. You can use a foreach loop to check all ids and simply append each update statement to a string variable. You can find an example here: http://www.youtube.com/watch?v=ImirOX73atc
You could use VALUES to get the new value you are using in the update portion. Also, if you use prepare and bind_param you will prevent SQL injection:
$mysqli = new mysqli('host', 'user', 'password', 'db');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$stmt = $mysqli->prepare("INSERT INTO content(name, fieldcontent)
VALUES ('home_title', ?), ('home_text', ?)
ON DUPLICATE KEY UPDATE fieldcontent = VALUES(fieldcontent)");
$stmt->bind_param('ss', $home_title, $home_text);
$stmt->execute();
Your second SQL statement is being added to your first creating one long statement that doesn't make sense. Separate these into two different statements.
Hello,
Been stuck on this one for a while.
Using Mysqli, how can I insert (or update if the key already exists) multiple rows in one transaction?
I would like to loop over this array and execute the query only once, instead of on each iteration of the loop
foreach($challengeData as $challengeId => $status) {
$this->conn->execute_query('
INSERT INTO my_table
(
challenge_id,
status
)
VALUES
(
?,?
)
ON DUPLICATE KEY UPDATE
status = VALUES(status)
',
[
$challengeId,
$status
]
);
}