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'=>'[email protected]'),
array('Email'=>'[email protected]'),
array('Email'=>'[email protected]'),
array('Email'=>'[email protected]'),
array('Email'=>'[email protected]'));
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,
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.
CodePudding user response:
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'=>'[email protected]', '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'=>'[email protected]', '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();
?>