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.
Videos
I googled a lot the last days and a few times i´ve seen postings that say that the $wpdb->update function doens´t support multiple updates and that this is not well documented. -> Sorry i don´t have links anymore...
I think that this is true because i couldn´t get it to work and i found not a single example witch works this way. So i use this way now to update multiple rows and still be able to handle errors.
The function:
function update_queries( $queries ) {
global $wpdb;
// set array
$error = array();
// run update commands
foreach( $queries as $query ) {
$query = str_replace( '[wp-prefix]', $wpdb->prefix, $query );
$last_error = $wpdb->last_error;
$wpdb->query( $query );
// fill array when we have an error
if( (empty( $wpdb->result ) || !$wpdb->result ) && !empty( $wpdb->last_error ) && $last_error != $wpdb->last_error ) {
$error[]= $wpdb->last_error." ($query)";
}
}
// when we have an error
if( $error ) {
return $error;
// when everything is fine
}else{
return false;
}
}
If we want to update a few things:
// update database
$queries = array();
$queries[] = "UPDATE `[wp-prefix]table` SET `value` = '$value' WHERE `name` = 'name';";
$queries[] = "UPDATE `[wp-prefix]table` SET `value` = '$value2' WHERE `name` = 'name2';";
$error = update_queries( $queries );
// if we have an error
if( !empty( $error ) ) {
.....
// when everything is fine
}else{
.....
}
Your syntax looks okay. My guess is your first two variables are empty.
Try hardcoding in values to see if those save.
$tmp_mail_smtp = 'Test Mail SMTP';
$tmp_mail_smtp_host = 'Test Mail SMTP Host';
$tmp_mail_smtp_auth = 'Test Mail SMTP Auth';
$my_update = $wpdb->update( $wpdb->prefix . 'my_settings',
array( 'value' => $tmp_mail_smtp,
'value' => $tmp_mail_smtp_host,
'value' => $tmp_mail_smtp_auth,
),
array( 'name' => 'mail-smtp',
'name' => 'mail-smtp-host',
'name' => 'mail-smtp-auth',
)
);
Update #1
Since the above doesn't work for you, remove the $wpdb->update code and try updating each option separately and see if that works.
if ( $settings_message == '0' ) {
update_option( 'role', $tmp_role );
update_option( 'mail-smtp', $tmp_mail_smtp );
update_option( 'mail-smtp-host', $tmp_mail_smtp_host );
update_option( 'mail-smtp-auth', $tmp_mail_smtp_auth );
}
P.S. Since this is a wp-admin page, I'd read more about the Settings API which provides a more structured way of adding/managing option pages.
Your SQL looks syntactically correct (unless I've missed something simple). The actual problem is because you're using mysql_query() - which does not support multiple statements; therefore, you can't run two UPDATE queries in one with this method.
From the manual:
mysql_query() sends a unique query (multiple queries are not supported)
On the same note, the mysql_ methods are being deprecated so I (and the community) would suggest you update your code to use mysqli_ or PDO methods - both of which support multiple queries in a single statement.
If you need to stick with mysql_query() (instead of restructuring your entire application), just split the queries and run them back-to-back.
You need to update them separately if you're using mysql_query.
To do it in a single run of a query, you'd need to use a CASE and assemble the parameters programmatically. SQL doesn't support variadic prepared statements, and only simple values can be parameterized.
Alternatively, define a statement to only take data for one row at a time and run the query in a loop. Repeated execution is how prepared statements are designed to be used for cases like this.
try {
$query = $db->prepare('UPDATE table SET a = ? WHERE b = ? AND c = 1');
foreach ($as as $i => $a) {
$query->execute(array($a, $bs[$i]));
}
} catch (PDOException $e) {
...
}
Use the CASE method as described in the link you provided, but build the query dynamically with the values you want.
Likely, this will be built with a for loop similar to how you're already doing it, but you will end up with a single query rather than querying your database every iteration.
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);
Use a case expression:
UPDATE $userName
SET P1 = case day
when 'MON1' then '$p1MON'
when 'TUE1' then '$p1TUE'
when 'WED1' then '$p1WED'
end
where day IN ('MON1', 'TUE1', 'WED1')
is the field day a unique key field?
If so you can use on duplicate key update syntax.
INSERT INTO $userName(day, P1) VALUES
('MON1', '$p1MON'),
('TUE1', '$p1TUE'),
('WED1', '$p1WED')
ON DUPLICATE KEY UPDATE P1=VALUES(P1)
$sql = "UPDATE `product_list` SET
`product_name` = '$product_name',
`product_category` = '$product_category',
`product_price` = '$product_price',
`product_description` = '$product_description',
`product_size_category` = '$size_category'
where clause..... (if required) ";
Try like this :
$sql = "UPDATE product_list SET product_name='".$product_name."',product_category='".$product_category."',product_price='".$product_price."',product_description='".$product_description."',size_category='".$size_category."' WHERE product_id=".$product_id;
Reference : https://dev.mysql.com/doc/refman/5.0/en/update.html