하나의 제출에서 여러 업데이트를 달성하려고합니다. 여러 행이있는 표가 있고 다음 삽입 상자로 이동하여 각 행의 한 필드를 업데이트 할 수 있기를 원합니다.배열 PHP로 mysql 데이터베이스 필드를 업데이트
내 코드는 다음과 같습니다. - // 테이블 시작 echo ' ';
//start header of table
echo '<tr>
<td width="60" align="center"><strong>Lab Item ID</strong></td>
<td width="60" align="center"><strong>Test Suite Name</strong></td>
<td width="60" align="center"><strong>Test Name</strong></td>
<td width="50" align="center"><strong>Result</strong></td>
</tr>';
//loop through all results
while ($row=mysql_fetch_object($sql)) {
//print out table contents and add id into an array and email into an array
echo '<tr>
<td align="center"><input name="id[]" value='.$row->lab_item_id.' readonly> </td>
<td align="center">'.$row->test_suite_name.'</td>
<td align="center">'.$row->test_name.'</td>
<td><input name="test_result[]" type="text" value="'.$row->test_result.'"></td>
</tr>';
}
//submit the form
echo'<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>';
//if form has been pressed proccess it
if($_POST["Submit"])
{
//get data from form
//$name = $_POST['name'];
//$_POST['check_number'] and $_POST['check_date'] are parallel arrays
foreach($_POST['id'] as $id) {
$tresult = trim($_POST['test_result']);
$query = "UPDATE tbl_lab_item SET test_result='$tresult' WHERE lab_item_id = '$id'";
//execute query
}
print_r($_POST);
var_dump($tresult);
//redirect user
$_SESSION['success'] = 'Updated';
//header("location:index.php");
}
?>
$ _POST 배열을 인쇄 할 때 변수는 모두 Null입니다. 나는 여러 배열에 대해 foreach를 할 수 없다는 것을 안다. (최소한 내가 할 수 있다고 생각하지는 않는다.) 그래서 다른 트릭이 내가 놓친다. 나는 $ _Post 인쇄물에 올바른 데이터가 있으므로 멀리 떨어져있을 수 없습니다.
덧붙여서, 모든 것은 쿼리에 의해 생성되므로 업데이트해야 할 레코드의 수를 모릅니다.
나는이 포럼을보고 있었지만 해결책을 얻지 못했습니다. 나는 배열을 이해할 줄 알았다. 그러나 이제는 궁금해지기 시작했다.
편집 - 작동하지 않는 $ tresult 변수입니다.
많은 감사, 제이슨
편집 목요일 2월 21일 (5시 41분 영국 시간) 사용자의 입력 모두를 위해 감사합니다. 나는 이것을 지금 풀었고 집단적 조언이 도움이되었습니다. 마지막으로 금이 간 코드는 다음과 같습니다. -
변수가 채워진 변수와 채워지는 변수가 핵심이었습니다. 첫 번째 원칙으로 돌아 가야하지 않습니까?
건배 모두. J
'변수'는 무엇입니까?코드에는 많은 변수가 있습니다. –
죄송합니다, 좋은 지적입니다. 업데이트 쿼리에서 업데이트하려고하는 test_result입니다. – Jason