【问题标题】:How to add value into array php file When html form submit in php?在php中提交html表单时,如何将值添加到数组php文件中?
【发布时间】:2017-10-12 06:24:22
【问题描述】:

我有一个 html 表单作为 index.php 和另一个 mydata.php 文件。我想将数据放入 mydata.php 文件,但有一些问题

index.php

<form action="" method="POST">
    <input name="field1" type="text" />
    <input name="field2" type="text" />
    <input type="submit" name="submit" value="Save Data">
</form>

<?php
if (isset($_POST['field1']) && isset($_POST['field2'])) {
    if (!filesize('mydata.php')) {
        $data0 = '<?php $a = array(' . "\n";
        $ret = file_put_contents('mydata.php', $data0, FILE_APPEND | LOCK_EX);
    }

    $data = '"' . $_POST['field1'] . '"' . '=>' . '"' . $_POST['field2'] . '",' . "\n";
    $ret = file_put_contents('mydata.php', $data, FILE_APPEND | LOCK_EX);

    if ($ret === false) {
        die('There was an error writing this file');
    } else {
        echo "$ret bytes written to file";
    }
}
?>

我的数据.php

$array = array("a"=>"b");

当我添加提交新值时,我想像我的帖子数据一样推送新数组

   Array
(
    [field1] => c
    [field2] => d
    [submit] => Save Data
)

$array = array("a"=>"b","c"=>"d");

【问题讨论】:

  • 你可以这样做:$data[$_POST['field1']] = $_POST['field2']
  • 为什么不使用数据库(MySQL 或 SQLite)?如果您想将数据存储在文件中,那么我建议您使用 JSON。
  • @Saani OP 想要将数据添加(存储)到mydata.php 文件中。
  • 嗯,这就是他可以准备和排列然后可以将其保存到文件或数据库的方式。随心所欲。

标签: php html arrays


【解决方案1】:

您只需将数据添加到$array,生成PHP代码,然后将其保存到文件中。

PHP 文件示例:

<?php
// data for the form ($_POST), all data from the client (browser) MUST be validated and sanitized
$formData = [
    'field1' => 'c',
    'field2' => 'd'
];

// load mydata.php if it was not loaded before
require_once 'mydata.php';

// add new data or update existen
$array = array_merge($array, $formData);
$tab = '    ';

// generate the new content for the mydata.php file
$newContent = '<?php' . PHP_EOL . '$array = [' . PHP_EOL;
foreach ($array as $key => $value)
    $newContent .= $tab . "'$key' => '" . addslashes($value) . "'," . PHP_EOL;

$newContent .= '];' . PHP_EOL;

//save the new content into file
file_put_contents('mydata.php', $newContent);

但我真的建议您使用 JSON 文件。

JSON 文件示例:

<?php
// data for the form ($_POST), all data from the client (browser) MUST be validated and sanitized
$formData = [
    'field2' => 'c',
    'field3' => 'd'
];

$array = [];

// load data
if (file_exists('mydata.json'))
    $array = json_decode(file_get_contents('mydata.json'), true);

// add new data or update the existen
$array = array_merge($array, $formData);

// save the new data into file
file_put_contents('mydata.json', json_encode($array), LOCK_EX);

【讨论】:

  • 感谢新丹的回答
  • @DivyeshSigmate 不要忘记标记答案,因为否则这个问题将来将毫无用处。
  • 我没有 15 名声望,所以我无法标记答案
【解决方案2】:

我不确定您在问什么,因为问题不清楚,但如果我没说错,要向现有数组添加新的键值对,您可以尝试

$field1 = $_POST['field1']; // $field1 = "c"
$field2 = $_POST['field2']; // $field2 = "d"

$array[$field1] = $field2;

【讨论】:

    猜你喜欢
    • 2017-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    • 1970-01-01
    • 2014-01-22
    相关资源
    最近更新 更多