在php中提交html表单时,如何向数组php文件添加值?
我有一个html表单index.php和另一个mydata.php文件。我想将数据放入mydata.php文件中,但存在一些问题 index.php在php中提交html表单时,如何向数组php文件添加值?,php,html,arrays,Php,Html,Arrays,我有一个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" valu
<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";
}
}
?>
当我添加提交新值时,我需要像我的post数据一样推送新数组
Array
(
[field1] => c
[field2] => d
[submit] => Save Data
)
$array = array("a"=>"b","c"=>"d");
我不确定您在问什么,因为问题不清楚,但如果我没有弄错,要在现有数组中添加新的键值对,您可以尝试
$field1 = $_POST['field1']; // $field1 = "c"
$field2 = $_POST['field2']; // $field2 = "d"
$array[$field1] = $field2;
您只需将数据添加到
$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);
您可以这样做:$data[$\u POST['field1']]=$\u POST['field2']
为什么不使用DB(MySQL或SQLite)?如果您想将数据存储在文件中,那么我建议您使用JSON。@Saani OP想将数据添加(存储)到mydata.php
文件中。这就是他准备和排列数据的方法,然后可以将其保存到文件或数据库中。他想要什么都行,谢谢你的回答neodan@DivyeshSigmate别忘了记下答案,否则这个问题在将来就没用了。我没有15个名声,所以我不能记下答案
<?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);
<?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);