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 Overflow
🌐
PHPpot
phppot.com › php › updatedelete-multiple-rows-using-php
Update/Delete Multiple Rows using PHP - PHPpot
July 14, 2022 - So, on submitting this multiple user information, an array of user information for each user will be passed to the PHP code. By iterating over this multidimensional array, each user record will be updated by setting current iteration values to the MySQL UPDATE query.
Top answer
1 of 2
1

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{
    .....
}
2 of 2
0

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.

🌐
Student Tutorial
studentstutorial.com › php › php-update-multiple-row
How to Update Multiple Row In PHP
*/ mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); /* Count table rows */ $count=mysql_num_rows($result); ?> <?php /* Check if button name "Submit" is active, do this */ if(isset($_POST['Submit'])) { $count=count($_POST["id"]); for($i=0;$i<$count;$i++){ $sql1="UPDATE $tbl_name SET name='" .
🌐
Stack Overflow
stackoverflow.com › questions › 47226675 › update-multiple-rows-with-single-query-using-mysql-and-php
Update multiple rows with single query using MySQL and PHP - Stack Overflow
implode(',', $userData); mysql_query($query); ?> The above code works perfectly for inserting multiple records into table users as seen above and it's very fast as well. However, I am trying to use the same method to update after going through ...
🌐
Etutorialspoint
etutorialspoint.com › index.php › mysql-exercises › mysql-update-multiple-rows-in-one-query
MySQL update multiple rows in one query
The best way to update multiple rows in just one statement is use CASE WHEN ELSE statement. In this, the statement will update the matched case and end otherwise, like- UPDATE 'table_name' SET 'field_name' = CASE 'id' WHEN '1' THEN 'value 1' WHEN '2' THEN 'value 2' WHEN '3' THEN 'value 3' ELSE ...
🌐
Edureka Community
edureka.co › home › community › categories › web development › php › mysql - update multiple rows with different...
MySQL - UPDATE multiple rows with different values in one query | Edureka Community
June 1, 2020 - 70956/mysql-update-multiple-rows-with-different-values-one-query · Home · Community · Categories · Web Development · PHP · MySQL - UPDATE multiple rows with different... How do risk management tools like RiskWatch use predictive analytics to forecast project risks?
Find elsewhere
🌐
Makitweb
makitweb.com › home › php › update multiple selected records with php
Update Multiple Selected Records with PHP - Makitweb
November 13, 2022 - <div class='container'> <!-- Form --> <form method='post' action=''> <!-- Submit button --> <input type='submit' value='Update Selected Records' name='but_update'><br><br> <!-- Record list --> <table border='1' style='border-collapse: collapse;' > <tr style='background: whitesmoke;'> <!-- Check/Uncheck All--> <th><input type='checkbox' id='checkAll' > Check</th> <th>Username</th> <th>First Name</th> <th>Last Name</th> <th>Age</th> <th>Salary</th> <th>Email</th> </tr> <?php $query = "SELECT * FROM users"; $result = mysqli_query($con,$query); while($row = mysqli_fetch_array($result) ){ $id = $ro
🌐
PHP Freaks
forums.phpfreaks.com › php coding › php coding help
Update multiple records in PHP / MYSQL - PHP Coding Help - PHP Freaks
June 29, 2023 - Hi there, I am trying to update a multiple rows for a single column and the code that I have wrote runs through fine and does not show any error messages but does not update the rows or column.
🌐
Stack Overflow
stackoverflow.com › questions › 18929978 › how-to-update-multiple-rows-in-php
mysql - how to update multiple rows in php - Stack Overflow
April 15, 2016 - extract($_POST); if (isset($Submit)) { for($i=0;$i<$count;$i++) { $update=("UPDATE tbl_name SET name='$name[$i]', lname='$lastname[$i]',email='$email[$i]' WHERE id='$id[$i]'"); $res=mysql_query($update); } } if(isset($res)) { header("locati...
🌐
SitePoint
sitepoint.com › php
How to update multiple rows with an array - PHP - SitePoint Forums | Web Development & Design Community
July 23, 2019 - I am trying to update a SQL table ... my Code. $property_img[]=$image; if (empty($property_img)){ return null;} $count=Count($property_img); if(Count($property_img)===0) { var_dump($count); $property_img=$property_img[0]; ...
🌐
TutorialsPoint
tutorialspoint.com › how-to-update-multiple-rows-using-single-where-clause-in-mysql
How to update multiple rows using single WHERE clause in MySQL?
mysql> update DemoTable1420 -> set FirstName='Carol',LastName='Taylor' -> where Id IN(1,3,4,5); Query OK, 4 rows affected (0.42 sec) Rows matched: 4 Changed: 4 Warnings: 0 ... +----+-----------+----------+------+ | Id | FirstName | LastName | Age | +----+-----------+----------+------+ | 1 | Carol | Taylor | 23 | | 2 | David | Miller | 22 | | 3 | Carol | Taylor | 24 | | 4 | Carol | Taylor | 21 | | 5 | Carol | Taylor | 25 | +----+-----------+----------+------+ 5 rows in set (0.00 sec)
🌐
DaniWeb
daniweb.com › programming › web-development › threads › 35096 › updating-multiple-rows-with-one-form
php - updating multiple rows with one form | DaniWeb
September 23, 2015 - Player ID: $player_id "); } } } break; // Default case: choose a team default: //teams dropdown menu $sql="SELECT * FROM teams"; $result=mysql_query($sql); $num_results = mysql_num_rows($result); $options=""; for ($i=0; $i<$num_results; $i++) { $row=mysql_fetch_array($result); $id=$row["id"]; $n=$row["tname"]; $options.="<OPTION VALUE=\"$id\">".$n."</option>"; } echo '<form name="team" method="post" action="pstats.php">'; echo '<select name="team" id="team"><option value=0>Choose a Team'; echo $options; echo '</select>'; echo '<input type="hidden" name="req" value="stats_form">'; echo '<input type="submit" name="Submit" value="Go">'; echo '</form>'; break; break; }