-4

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.

sid
  • 1
  • 3
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman Dec 08 '22 at 19:17
  • 3
    The where clause. Are you going to compare against anything? – Jason K Dec 08 '22 at 19:24
  • `where id"` -- Where `id` what? Just where it has an id? – aynber Dec 08 '22 at 19:49
  • 2
    Actually all updates take place. Since your `WHERE` doesn't specify an actual criteria, _each query will update all records in the table_. If you only say `id`, it means "any id" and will match all rows. By what magic do you figure MySQL would place each email in the row you wanted? – Markus AO Dec 08 '22 at 19:56
  • Please get in the habit of indenting your code consistently, what you've posted is very difficult to read. – Sammitch Dec 08 '22 at 21:27

1 Answers1

0

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

Mercilus
  • 34
  • 4