首页 > 解决方案 > 如何将输入值保存到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);


    ?>

标签: phphtml

解决方案


我认为您应该研究应该在以后保存和解析的数据结构化,例如 XML,或者更简单:https ://www.php.net/manual/de/function.json-encode.php

print_r 用于输出,json_encode 生成一个可以保存到文件中的字符串。json_decode 可以读取该字符串并再次生成 PHP 数组。


推荐阅读