php - 如何将输入值保存到php的多维数组中
问题描述
当我运行我的代码时,它会出现按摩
致命错误:无法在第 173 行的 C:\xampp\htdocs\PA2\PracticalAssignment2.php 中的写入上下文中使用临时表达式
这是我的代码:
<body>
<form action="practical assignment2.php" methos="POST">
<table class="Table" id='STable'>
<tr>
<th rowspan='2'>No</th>
<th colspan='2'>Student</th>
<th colspan='5'>Subject's Score</th>
<th rowspan='2'>Total</th>
<th rowspan='2'>Average</th>
</tr>
<tr>
<td id='SId'>ID</td>
<td id='Sname'>Name</td>
<td id='Eng'>English</td>
<td id='Math'>Mathematic</td>
<td id='His'>History</td>
<td id='Sc'>Science</td>
<td id='Phy'>Physics</td>
</tr>
<tr id='Sdata' onmouseover="this.style.backgroundColor='yellow';" onmouseout="this.style.backgroundColor='white';">
<td id="Number" value="1">1.</td>
<td><input type="number" id='idS' name="id[]" min="1" onmouseover="this.style.backgroundColor='#CCFFFF';" onmouseout="this.style.backgroundColor='white';"></td>
<td><input type="text" id="nameS" name="name[]" onmouseover="this.style.backgroundColor='#CCFFFF';" onmouseout="this.style.backgroundColor='white';"></td>
<td><input type="number" id='EngS' name="eng[]" min="1" max="100" onmouseover="this.style.backgroundColor='#CCFFFF';" onmouseout="this.style.backgroundColor='white';"></td>
<td><input type="number" id='MathS' name="marh[]" min="1" max="100" onmouseover="this.style.backgroundColor='#CCFFFF';" onmouseout="this.style.backgroundColor='white';"></td>
<td><input type="number" id='HisS' name="his[]" min="1" max="100" onmouseover="this.style.backgroundColor='#CCFFFF';" onmouseout="this.style.backgroundColor='white';"></td>
<td><input type="number" id='ScS' name="sc[]" min="1" max="100" onmouseover="this.style.backgroundColor='#CCFFFF';" onmouseout="this.style.backgroundColor='white';"></td>
<td><input type="number" id='PhyS' name="phy[]" min="1" max="100"onmouseover="this.style.backgroundColor='#CCFFFF';" onmouseout="this.style.backgroundColor='white';"></td>
<td><input type="number" id='totS' name="total[]" min="1" readonly></td>
<td><input type="number" id='avgS' name="average[]" min="1" max="100" readonly></td>
</tr>
<tr>
<td colspan='10' id="but"><button id="add_row" onclick="AllAction()">Submit</button></td>
</tr>
</table>
这是我将上表中的输入值保存到多维数组的部分:
<?php
// add item to array and display in text file
$studentsArray = [
'StudentID' => &_POST['id'],
'StudentName' => &_POST['name'],
'SubjectEnglish' => &_POST['eng'],
'SubjectMaths' => &_POST['math'],
'SubjectHistory' => &_POST['his'],
'SubjectSciences' => &_POST['sc'],
'SubjectPhysics' => &_POST['phy'],
'ScoreTotal' => &_POST['total'],
'ScaoreAverage' => &_POST['average']];
$file = 'studentscore.txt';
$fh = fopen($file, 'w') or die("can't open file");
fwrite($fh, print_r($studentsArray,true));
fclose($fh);
?>
解决方案
我认为您应该研究应该在以后保存和解析的数据结构化,例如 XML,或者更简单:https ://www.php.net/manual/de/function.json-encode.php
print_r 用于输出,json_encode 生成一个可以保存到文件中的字符串。json_decode 可以读取该字符串并再次生成 PHP 数组。
推荐阅读
- node.js - Heroku 仅显示 MERN 应用程序的后端。我怎样才能解决这个问题?
- c - 在第一个空格之后返回 c 中字符串的其余部分
- python-3.x - Youtube Data API v.3 - 全自动 oAuth 流程(Python)?
- ios - 定向光受相机移动的影响
- r - 在 ggplot geom_freqpoly 函数中停止图形接触零
- azure - azure kubernetes 服务中的网络
- kotlin - 参数包中“大小”的参数类型错误。字符串 [] 预期
- .htaccess - 将 codeignitor easyappointments /index.php 重定向到 /index.php/backend 页面?
- python - 将 json 文件解析为 pandas 数据框
- javascript - store.destroy() 和 session.destroy() 有什么区别?我们需要同时使用两者吗?