有人可以帮我弄清楚为什么这不起作用吗?
我的更新功能不能处理我的PHP代码。我有一个断点,它列出了for循环末尾的所有信息,这是正确的信息。但是它每个ID打印约50次。此外,它不会更新SQL表。
我该如何解决这个问题?
段:
if ( isset( $_POST[ 'RemarketingRollCall' ] ) )
{
echo 'RollCall called ';
foreach($_POST['userid'] as $param1){
foreach($_POST['name'] as $param2){
foreach($_POST['job'] as $param3){
foreach($_POST['monday'] as $param4){
foreach($_POST['tuesday'] as $param5){
foreach($_POST['wednesday'] as $param6){
foreach($_POST['thursday'] as $param7){
foreach($_POST['friday'] as $param8){
$idnumber = mysqli_real_escape_string( $con, $param1 );
$Name = mysqli_real_escape_string( $con, $param2 );
$Job = mysqli_real_escape_string( $con, $param3 );
$Monday = mysqli_real_escape_string( $con, $param4 );
$Tuesday = mysqli_real_escape_string( $con, $param5 );
$Wednesday = mysqli_real_escape_string( $con, $param6 );
$Thursday = mysqli_real_escape_string( $con, $param7 );
$Friday = mysqli_real_escape_string( $con, $param8 );
echo "(". $idnumber . " " . $Name . " " . $Job . " " . $Monday . " " . $Tuesday . " " . $Wednesday . " " . $Thursday . " " . $Friday . ")";
$sql = "UPDATE `Employee` SET `Job`='$Job', `Monday`='$Monday', `Tuesday`='$Tuesday', `Wednesday`='$Wednesday', `Thursday`='$Thursday', `Friday`='$Friday' WHERE `ID`=$idnumber";
mysqli_query($con,$sql);
if ( !mysqli_query( $con, $sql ) )
{
die( 'Error: ' . mysqli_error( $con ) );
}
}
}
}
}
}
}
}
}
mysqli_close( $con );
?>
//putting a script here
这是输出:http://gyazo.com/3006df320d4857314ea486936f656ba3
这是我的表生成:
while($row = mysqli_fetch_array($remarketingRoll)) {
echo "<form id='RemarketingRollCall' method='POST'>";
echo "<tr>";
echo "<td contenteditable='false' align='center' width='11%'><input style='verticle-align:center;' type='text' name='userId[]' value=". $row['userId'] ." readonly></td>";
echo "<td contenteditable='false' align='center' width='11%'><input style='verticle-align:center;' type='text' name='name[]' value=". $row['Name'] ." readonly></td>";
echo "<td contenteditable='false' align='center' width='11%'>
<select name='job[]'>
<option value='Remarketing'>Remarketing</option>
<option value='IOS'>IOS</option>
<option value='Computers'>Computers</option>
</select></td>";
if ( strcmp( $row['Monday'], 'Attended' ) == 0 ){
echo "<td contenteditable='false' align='center' width='11%'>
<select name='monday[]'>
<option value='Attended''>Attended</option>
<option value='Absent'>Absent</option>
</select></td>";
} else if ( strcmp( $row['Monday'], 'Absent' ) == 0 ){
echo "<td contenteditable='false' align='center' width='11%'>
<select name='monday[]'>
<option value='Absent'>Absent</option>
<option value='Attended''>Attended</option>
</select></td>";
} else {
echo "<td contenteditable='false' align='center' width='11%'>
<select name='monday[]'>
<option> </option>
<option value='Absent'>Absent</option>
<option value='Attended''>Attended</option>
</select></td>";
}
if ( strcmp( $row['Tuesday'], 'Attended' ) == 0 ){
echo "<td contenteditable='false' align='center' width='11%'>
<select name='tuesday[]'>
<option value='Attended''>Attended</option>
<option value='Absent'>Absent</option>
</select></td>";
} else if ( strcmp( $row['Tuesday'], 'Absent' ) == 0 ){
echo "<td contenteditable='false' align='center' width='11%'>
<select name='tuesday[]'>
<option value='Absent'>Absent</option>
<option value='Attended''>Attended</option>
</select></td>";
} else {
echo "<td contenteditable='false' align='center' width='11%'>
<select name='tuesday[]'>
<option> </option>
<option value='Absent'>Absent</option>
<option value='Attended''>Attended</option>
</select></td>";
}
if ( strcmp( $row['Wednesday'], 'Attended' ) == 0 ){
echo "<td contenteditable='false' align='center' width='11%'>
<select name='wednesday[]'>
<option value='Attended''>Attended</option>
<option value='Absent'>Absent</option>
</select></td>";
} else if ( strcmp( $row['Wednesday'], 'Absent' ) == 0 ){
echo "<td contenteditable='false' align='center' width='11%'>
<select name='wednesday[]'>
<option value='Absent'>Absent</option>
<option value='Attended''>Attended</option>
</select></td>";
} else {
echo "<td contenteditable='false' align='center' width='11%'>
<select name='wednesday[]'>
<option> </option>
<option value='Absent'>Absent</option>
<option value='Attended''>Attended</option>
</select></td>";
}
if ( strcmp( $row['Thursday'], 'Attended' ) == 0 ){
echo "<td contenteditable='false' align='center' width='11%'>
<select name='thursday[]'>
<option value='Attended''>Attended</option>
<option value='Absent'>Absent</option>
</select></td>";
} else if ( strcmp( $row['Thursday'], 'Absent' ) == 0 ){
echo "<td contenteditable='false' align='center' width='11%'>
<select name='thursday[]'>
<option value='Absent'>Absent</option>
<option value='Attended''>Attended</option>
</select></td>";
} else {
echo "<td contenteditable='false' align='center' width='11%'>
<select name='thursday[]'>
<option> </option>
<option value='Absent'>Absent</option>
<option value='Attended''>Attended</option>
</select></td>";
}
if ( strcmp( $row['Friday'], 'Attended' ) == 0 ){
echo "<td contenteditable='false' align='center' width='11%'>
<select name='friday[]'>
<option value='Attended''>Attended</option>
<option value='Absent'>Absent</option>
</select></td>";
} else if ( strcmp( $row['Friday'], 'Absent' ) == 0 ){
echo "<td contenteditable='false' align='center' width='11%'>
<select name='friday[]'>
<option value='Absent'>Absent</option>
<option value='Attended''>Attended</option>
</select></td>";
} else {
echo "<td contenteditable='false' align='center' width='11%'>
<select name='friday[]'>
<option> </option>
<option value='Absent'>Absent</option>
<option value='Attended''>Attended</option>
</select></td>";
}
echo "<td name='week[]' contenteditable='false' align='center' width='11%'>" . $row['Week'] . "</td>";
echo "</tr>";
echo "</form>";
}
答案 0 :(得分:1)
$index = 0;
foreach ($_POST as $key => $value){
$idnumber = $_POST['userId'][$index];
$Name = $_POST['name'][$index];
$Job = $_POST['job'][$index];
$Monday = $_POST['monday'][$index];
$Tuesday = $_POST['tuesday'][$index];
$Wednesday = $_POST['wednesday'][$index];
$Thursday = $_POST['thursday'][$index];
$Friday = $_POST['friday'][$index];
$index++;
// put your sql UPDATE here
}
您没有显示while {
的右括号 </select></td>";
}
echo "<td name='week[]' contenteditable='false' align='center' width='11%'>" . $row['Week'] . "</td>";
echo "</tr>";
echo "</form>";
如果它在这里:
</select></td>";
}
echo "<td name='week[]' contenteditable='false' align='center' width='11%'>" . $row['Week'] . "</td>";
echo "</tr>";
echo "</form>";
} // is it here????????????
如果大括号是我认为的那个,你需要把它移到这里
</select></td>";
}
echo "<td name='week[]' contenteditable='false' align='center' width='11%'>" . $row['Week'] . "</td>";
echo "</tr>";
} // <==========================
echo "</form>";
然后移动:
while($row = mysqli_fetch_array($remarketingRoll)) {
echo "<form id='RemarketingRollCall' method='POST'>";
要:
echo "<form id='RemarketingRollCall' method='POST'>";
while($row = mysqli_fetch_array($remarketingRoll)) {
替换:
$idnumber = mysqli_real_escape_string( $con, $param1 );
$Name = mysqli_real_escape_string( $con, $param2 );
$Job = mysqli_real_escape_string( $con, $param3 );
$Monday = mysqli_real_escape_string( $con, $param4 );
$Tuesday = mysqli_real_escape_string( $con, $param5 );
$Wednesday = mysqli_real_escape_string( $con, $param6 );
$Thursday = mysqli_real_escape_string( $con, $param7 );
$Friday = mysqli_real_escape_string( $con, $param8 );
使用:
$idnumber = $_POST['userId'][0];
$Name = $_POST['name'][0];
$Job = $_POST['job'][0];
$Monday = $_POST['monday'][0];
$Tuesday = $_POST['tuesday'][0];
$Wednesday = $_POST['wednesday'][0];
$Thursday = $_POST['thursday'][0];
$Friday = $_POST['friday'][0];
更新结束2
<强>更新强>
添加此行代码以转储正在提交的内容:
var_export($_POST);
不要使用foreach。
改变所有的foreach:
$param2 = $_POST['name'];
$param3 = $_POST['job'];
$param4 = $_POST['monday'];
$param5 = $_POST['tuesday'];
$param6 = $_POST['wednesday'];
$param7 = $_POST['thursday'] ;
$param8 = $_POST['friday'];
要:
$param2 = $_POST['name'];
$param3 = $_POST['job'];
$param4 = $_POST['monday'];
$param5 = $_POST['tuesday'];
$param6 = $_POST['wednesday'];
$param7 = $_POST['thursday'] ;
$param8 = $_POST['friday'];