我有一个html格式作为index.php和另一个mydata.php文件。我想将数据放入mydata.php文件,但有一些问题如何将值添加到数组php文件当html表单提交在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";
}
}
?>
mydata.php
$array = array("a"=>"b");
当我添加提交新的价值,我想需要像我的发布数据推新阵列
Array
(
[field1] => c
[field2] => d
[submit] => Save Data
)
$array = array("a"=>"b","c"=>"d");
你可以做这样的:'$数据[$ _ POST [ '字段1'] = $ _ POST [ '字段2']' – Saani
为什么你不使用DB (MySQL或SQLite)?如果你想将数据存储在一个文件中,那么我建议你使用JSON。 – Neodan
@Saani OP想要将数据添加(存储)到'mydata.php'文件中。 – Neodan