Search code examples
phpforeachsql-updatewhere-clauseassociative-array

why is update only last value of array rather than all 5?


i am trying to update quary of "email" by associtive array loop through but when i try this there are take only one last value of array and upadat.

here is code...

<?php

include_once('db_connect.php');

$update_array=array();
                $update_array=array(
                                            array('Email'=>'richard123@gmail.com'),
                                            array('Email'=>'hennry123@gmail.com'),
                                            array('Email'=>'loster123@gmail.com'),
                                            array('Email'=>'luffy123@gmail.com'),
                                            array('Email'=>'zoro123@gmail.com'));
print_r($update_array)."<br>";
echo "<pre>";

            
           foreach ($update_array as  $value ) {        

            
                $update_Email=$value['Email'];

                $update="UPDATE form SET email='$update_Email' where id";

                    $result= mysqli_query($conn, $update);
          
            }               
                            
if ($result) {
  echo "New record Updated successfully";
} 
else {

  echo "Error:not created ". mysqli_error($conn);
}

mysqli_close($conn);

?>

image of data update,

enter image description here

i tried nasted foreach to featch array value and than passing into upadate but error apper: array ofset on strin in.

help to update all email's data in once and all.


Solution

  • you need to define the id in your where clause, id needs to be of the row you want to update.

    <?php
    
    include_once('db_connect.php');
    
    $update_array=array();
    $update_array=array(
        array('Email'=>'richard123@gmail.com', 'id'=>'1'),
    );
    
    foreach ($update_array as $value ) {        
        $update_Email=$value['Email'];
        $update_id = $value['id'];
        
        $update="UPDATE form SET email='$update_Email' where id = $update_id";
        $result= mysqli_query($conn, $update);
    }               
    
    if ($result) {
        echo "New record Updated successfully";
    } else {
        echo "Error:not created ". mysqli_error($conn);
    }
    mysqli_close($conn);
    
    ?>    
    

    i would also highly recommend you reading about prepared statements to make sure you wont be a target of sql injection.

    <?php
    
    include_once('db_connect.php');
    
    $update_array=array();
    $update_array=array(
        array('Email'=>'richard123@gmail.com', 'id'=>'1'),
    );
    
    foreach ($update_array as $value ) {        
        
        $stmt = $conn->prepare("UPDATE form SET email = ? WHERE id = ?");
        $stmt->bind_param("si", $update_Email, $update_id);
    
        $update_Email=$value['Email'];
        $update_id = $value['id'];
    
        $stmt->execute();
    }           
    $stmt->close();
    $conn->close();
    ?>
    

    php.net // Prepared statements