用于比较数组,$ _ post数据和$ _Session Array的PHP

发布于 2025-02-01 20:39:55 字数 1359 浏览 3 评论 0原文

因此,我已经卡住了几个小时,试图将存储为变量的$ _session数据与来自更新的表单字段中的$ _post数据存储在一起,但是当我使用array array_diff_assoc或array_diff时(取决于我如何在数组函数中订购它们),而不是区别。我希望能够仅输出差异。

<?php
if(isset($_SESSION['username'])) {
$username = $_SESSION['username'];
$query = "SELECT * FROM updatetest WHERE username = '{$username}' ";       
$select_users= mysqli_query($db, $query);
 while ($row = mysqli_fetch_array($select_users)) {
             $user_info['name'] =  $row['name'];
             $user_info['phone'] =  $row['phone'];
             $user_info['dob'] =  $row['dob'];
             $user_info['email'] =  $row['email'];
             $user_info['address_line_1'] = $row['address_line_1'];
             $user_info['town_city'] =  $row['town_city'];
             $user_info['postcode'] =  $row['postcode'];
             $your_data[]=$user_info;
               }
}
?> 

 <?php
 if(isset($_POST['submit'])) {

         $name= $_POST['name'];
         $phone = $_POST['phone'];
         $dob = $_POST['dob'];
         $email = $_POST['email'];
         $address_line_1 = $_POST['address_line_1'];
         $town_city = $_POST['town_city'];
         $postcode = $_POST['postcode'];

$result = array_diff_assoc($_SESSION['your_data'],$_POST);
 print_r($result);
}

?>

注意:当我print_r $ _session ['your_data']或$ _ post时,我可以看到数组。目的是使用phpmail()发送包含表格更新字段的电子邮件

So I've been stuck for a few hours trying to compare $_SESSION data stored as a variable with $_POST data from updated form fields but when I use array array_diff_assoc or array_diff i get either the whole session data array or the whole post data array(depending oh how I order them in the array function), and not the difference. I want to be able to output the difference only.

<?php
if(isset($_SESSION['username'])) {
$username = $_SESSION['username'];
$query = "SELECT * FROM updatetest WHERE username = '{$username}' ";       
$select_users= mysqli_query($db, $query);
 while ($row = mysqli_fetch_array($select_users)) {
             $user_info['name'] =  $row['name'];
             $user_info['phone'] =  $row['phone'];
             $user_info['dob'] =  $row['dob'];
             $user_info['email'] =  $row['email'];
             $user_info['address_line_1'] = $row['address_line_1'];
             $user_info['town_city'] =  $row['town_city'];
             $user_info['postcode'] =  $row['postcode'];
             $your_data[]=$user_info;
               }
}
?> 

 <?php
 if(isset($_POST['submit'])) {

         $name= $_POST['name'];
         $phone = $_POST['phone'];
         $dob = $_POST['dob'];
         $email = $_POST['email'];
         $address_line_1 = $_POST['address_line_1'];
         $town_city = $_POST['town_city'];
         $postcode = $_POST['postcode'];

$result = array_diff_assoc($_SESSION['your_data'],$_POST);
 print_r($result);
}

?>

Note: when I print_r either $_SESSION['your_data'] or $_POST, I can see the arrays. The goal is to send an email containing the form's updated fields using phpmail()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷情 2025-02-08 20:39:56
  • 我花了一些时间以尽我所能重写您的代码,而没有与您相同的DB信息。

  • 让我知道它是否有效/错误! :)

密钥更改:

  1. 将“ mysqli_assoc”添加到“ mysqli_fetch_array”
'$your_data[] = $user_info;' 

'_SESSION['your_data'] = $user_info;'
$_SESSION['username'] = 'userone';

if (!empty($_SESSION['username'])) {

  // $_SESSION variables are safely stored server side
  //$username = $_SESSION['username'];
  $select = '*';
  $table = 'updatetest';
  $column = $_SESSION['username'];

  $query = 'SELECT ' . $select . ' FROM ' . $table . ' WHERE username = ' . $column;
  //$query = "SELECT * FROM updatetest WHERE username = '{$username}' ";

  $select_users = mysqli_query($db, $query);

  /* associative array 
  * https://www.php.net/manual/en/mysqli-result.fetch-array.php
  */
  while ($row = mysqli_fetch_array($select_users, MYSQLI_ASSOC)) {
    //while ($row = mysqli_fetch_array($select_users)) {

    $user_info['name'] =  $row['name'];
    $user_info['phone'] =  $row['phone'];
    $user_info['dob'] =  $row['dob'];
    $user_info['email'] =  $row['email'];
    $user_info['address_line_1'] = $row['address_line_1'];
    $user_info['town_city'] =  $row['town_city'];
    $user_info['postcode'] =  $row['postcode'];
    //$your_data[] = $user_info;
    $_SESSION['your_data'] = $user_info;
  }
}

// form submission
if (isset($_POST['submit'])) {

  $name = $_POST['name'];
  $phone = $_POST['phone'];
  $dob = $_POST['dob'];
  $email = $_POST['email'];
  $address_line_1 = $_POST['address_line_1'];
  $town_city = $_POST['town_city'];
  $postcode = $_POST['postcode'];

  $result = array_diff_assoc($_SESSION['your_data'], $_POST);
  // '<pre></pre>' formats array output for html
  echo '<pre>';
  print_r($result);
  echo '</pre>';
}
  • i took some time to re-write your code to the best of my ability without having the same DB information as you.

  • let me know if it works/errors! :)

key changes:

  1. added 'MYSQLI_ASSOC' to 'mysqli_fetch_array'
  2. changed:
'$your_data[] = $user_info;' 

to

'_SESSION['your_data'] = $user_info;'
$_SESSION['username'] = 'userone';

if (!empty($_SESSION['username'])) {

  // $_SESSION variables are safely stored server side
  //$username = $_SESSION['username'];
  $select = '*';
  $table = 'updatetest';
  $column = $_SESSION['username'];

  $query = 'SELECT ' . $select . ' FROM ' . $table . ' WHERE username = ' . $column;
  //$query = "SELECT * FROM updatetest WHERE username = '{$username}' ";

  $select_users = mysqli_query($db, $query);

  /* associative array 
  * https://www.php.net/manual/en/mysqli-result.fetch-array.php
  */
  while ($row = mysqli_fetch_array($select_users, MYSQLI_ASSOC)) {
    //while ($row = mysqli_fetch_array($select_users)) {

    $user_info['name'] =  $row['name'];
    $user_info['phone'] =  $row['phone'];
    $user_info['dob'] =  $row['dob'];
    $user_info['email'] =  $row['email'];
    $user_info['address_line_1'] = $row['address_line_1'];
    $user_info['town_city'] =  $row['town_city'];
    $user_info['postcode'] =  $row['postcode'];
    //$your_data[] = $user_info;
    $_SESSION['your_data'] = $user_info;
  }
}

// form submission
if (isset($_POST['submit'])) {

  $name = $_POST['name'];
  $phone = $_POST['phone'];
  $dob = $_POST['dob'];
  $email = $_POST['email'];
  $address_line_1 = $_POST['address_line_1'];
  $town_city = $_POST['town_city'];
  $postcode = $_POST['postcode'];

  $result = array_diff_assoc($_SESSION['your_data'], $_POST);
  // '<pre></pre>' formats array output for html
  echo '<pre>';
  print_r($result);
  echo '</pre>';
}
只为守护你 2025-02-08 20:39:56

经过社区成员提供的一些解决方案后,我编辑了我的代码并奏效了。我的代码现在看起来像这样;

<?php
  if(isset($_SESSION['username'])) {
                                  $username = $_SESSION['username'];
                                  $query = "SELECT * FROM updatetest WHERE username = '{$username}' ";
                                      $select_users= mysqli_query($db, $query);
                                      while ($row = mysqli_fetch_array($select_users, MYSQLI_ASSOC)) {
                                $user_info['name'] =  $row['name'];
                                $user_info['phone'] =  $row['phone'];
                                $user_info['dob'] =  $row['dob'];
                                $user_info['email'] =  $row['email'];
                                $user_info['address_line_1'] = $row['address_line_1'];
                                 $user_info['address_line_2'] =  $row['address_line_2'];
                                 $user_info['town_city'] =  $row['town_city'];
                                 $user_info['county'] =  $row['county'];
                                 $user_info['postcode'] =  $row['postcode'];
                               //  $your_data[] = $user_info;
                                $_SESSION['your_data'] = $user_info;
                                          
                                      }

                                 }
                                ?>

<?php
if (isset($_POST['submit'])) {

  $name = $_POST['name'];
  $phone = $_POST['phone'];
  $dob = $_POST['dob'];
  $email = $_POST['email'];
  $address_line_1 = $_POST['address_line_1'];
  $town_city = $_POST['town_city'];
  $postcode = $_POST['postcode'];

  $result = array_diff_assoc($_POST, $_SESSION['your_data']);
  // '<pre></pre>' formats array output for html
  echo '<pre>';
  print_r($result);
  echo '</pre>';
}
?>

注意:$ result = array_diff_assoc($ _ post,$ _session ['your_data'])。订单被颠倒了,以获取差异。

After going through some solutions provided by community members, I edited my code and it worked. My code now looks like this;

<?php
  if(isset($_SESSION['username'])) {
                                  $username = $_SESSION['username'];
                                  $query = "SELECT * FROM updatetest WHERE username = '{$username}' ";
                                      $select_users= mysqli_query($db, $query);
                                      while ($row = mysqli_fetch_array($select_users, MYSQLI_ASSOC)) {
                                $user_info['name'] =  $row['name'];
                                $user_info['phone'] =  $row['phone'];
                                $user_info['dob'] =  $row['dob'];
                                $user_info['email'] =  $row['email'];
                                $user_info['address_line_1'] = $row['address_line_1'];
                                 $user_info['address_line_2'] =  $row['address_line_2'];
                                 $user_info['town_city'] =  $row['town_city'];
                                 $user_info['county'] =  $row['county'];
                                 $user_info['postcode'] =  $row['postcode'];
                               //  $your_data[] = $user_info;
                                $_SESSION['your_data'] = $user_info;
                                          
                                      }

                                 }
                                ?>

<?php
if (isset($_POST['submit'])) {

  $name = $_POST['name'];
  $phone = $_POST['phone'];
  $dob = $_POST['dob'];
  $email = $_POST['email'];
  $address_line_1 = $_POST['address_line_1'];
  $town_city = $_POST['town_city'];
  $postcode = $_POST['postcode'];

  $result = array_diff_assoc($_POST, $_SESSION['your_data']);
  // '<pre></pre>' formats array output for html
  echo '<pre>';
  print_r($result);
  echo '</pre>';
}
?>

Note: $result = array_diff_assoc($_POST, $_SESSION['your_data']). The order was reversed, to get the difference.

羁〃客ぐ 2025-02-08 20:39:55

我认为两个阵列中都没有匹配的键和值!
这就是为什么您将整个数组返回到$ result变量中的原因。

我已经解释了为什么下面发生这种情况的原因:

array_diff_assoc

array_diff_assoc在第一个数组中将 values 比较了第二个数组。 第一个数组中存在的键和值,但不在第二个数组中返回$ $ result

例如,

<?php

$array1 = array("a" => "apple", "b" => "banana", "c" => "mango");
$array2 = array("a" => "apple",  "b" => "banana", "c" => "mango");
$result = array_diff_assoc($array1, $array2);
print_r($result)
?>

输出:

Array ( ) 

如果您现在,将$ array2与$ array1

<?php
$result = array_diff_assoc($array2, $array1);
print_r($result);
?>

输出进行比较:

array([d] =&gt; salad)

,您可以看到唯一的非匹配键,值为“ d” d“ =&gt; “沙拉”

现在让我们尝试一下:

<?php
$array1 = array("a" => "apple", "b" => "banana", "c" => "mango");
$array2 = array("m" => "apple",  "n" => "banana", "o" => "mango");
$result = array_diff_assoc($array1, $array2);
print_r($result)
?>

输出:

Array ( [a] => apple [b] => banana [c] => mango ) 

您可以看到两个数组中的值相同,但是键不同,因此$ array1被返回到$ result。因此,如果要仅比较,则必须使用array_diff()

array_diff

array_diff仅比较数组中的值。现在让我们再次尝试以上示例!

<?php
$array1 = array("a" => "apple", "b" => "banana", "c" => "mango");
$array2 = array("m" => "apple",  "n" => "banana", "o" => "mango");
$result = array_diff($array1, $array2);
print_r($result)
?>

输出:

Array ( ) 

这里的键不同,但值相同,因此所有结果都是匹配的,因此返回空数组!

结论您的问题,

因为您同时尝试了array_diff()和array_diff_assoc(),所以两个数组都不具有相同的值,也不具有相同的键和值,因此在两种情况下,第一个参数中的整个数组都会返回!

我认为您必须使用其他一些方法来比较它们!

[编辑:您可以使用Array_diff()比较$ row和$ _post,如该问题的评论中所述
参考:

https://www.php.net/manual/ en/function.Array-diff-assoc.php

https ://www.php.net/manual/en/function.array-diff.php

I think there are no matching keys and values in both the arrays!
That is why you are getting the whole array returned in the $result variable.

I have explained why this happens below:

array_diff_assoc

array_diff_assoc compares both the keys and values in the first array to the second array. The keys and values which are present in the first array but not in the second array is returned to the $result

For example

<?php

$array1 = array("a" => "apple", "b" => "banana", "c" => "mango");
$array2 = array("a" => "apple",  "b" => "banana", "c" => "mango");
$result = array_diff_assoc($array1, $array2);
print_r($result)
?>

Output:

Array ( ) 

If you now compare $array2 with $array1

<?php
$result = array_diff_assoc($array2, $array1);
print_r($result);
?>

Output:

Array ( [d] => salad )

As you can see the only non-matching key and value is "d" => "salad"

Now let's try this:

<?php
$array1 = array("a" => "apple", "b" => "banana", "c" => "mango");
$array2 = array("m" => "apple",  "n" => "banana", "o" => "mango");
$result = array_diff_assoc($array1, $array2);
print_r($result)
?>

Output:

Array ( [a] => apple [b] => banana [c] => mango ) 

As you can see the values are same in both the arrays but the keys are different, so the $array1 gets returned to the $result. So you have to use array_diff(), if you want to compare the values only.

array_diff

array_diff compares only the values in the array. Now lets try the above same example again!

<?php
$array1 = array("a" => "apple", "b" => "banana", "c" => "mango");
$array2 = array("m" => "apple",  "n" => "banana", "o" => "mango");
$result = array_diff($array1, $array2);
print_r($result)
?>

Output:

Array ( ) 

Here the keys are different but the values are same so all results are matching, so empty array is returned!

Conclusion to your problem

Since you tried both array_diff() and array_diff_assoc(), both of your arrays neither have same values nor both same key and value therefore the whole array in the first argument gets returned in both the cases!

I think you have to use some other method to compare them!

[Edit: You could compare the the $row and $_POST using array_diff() as mentioned under the comment of the question]
References:

https://www.php.net/manual/en/function.array-diff-assoc.php

https://www.php.net/manual/en/function.array-diff.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文