foreach数据不在数据库中更新

时间:2015-11-19 11:29:23

标签: php mysql pdo

在我的本地服务器中,此脚本运行正常。当我在现场上传此脚本时,它无法正常工作。

它只将126行数据插入数据库,但我需要一次上传至少500行。

<?php
include 'database-config.php';
foreach($_POST['classroll'] as $row=>$classroll)
{
    $sclassroll = $classroll; 
    $mark = $_POST['mark'][$row];
    $type = $_POST['rtype'];
    $session = $_POST['rsession'];
    $department = $_POST['rdepartment'];
    $examtype = $_POST['rextype'];
    $examyear = $_POST['rexyear'];
    $examsubject = $_POST['rexmarksubject'];

$stmt = $dbh->prepare("INSERT INTO exammarks(studnettype, studentsession, studentdepartment, studentclassroll, examtype, examyear, examsubjec, exammarks) VALUES (:studnettype, :studentsession, :studentdepartment, :studentclassroll, :examtype, :examyear, :examsubjec, :exammarks)");

  $stmt->bindParam('studnettype', $type);
  $stmt->bindParam('studentsession', $session);
  $stmt->bindParam('studentdepartment', $department);
  $stmt->bindParam('studentclassroll', $sclassroll);
  $stmt->bindParam('examtype', $examtype);
  $stmt->bindParam('examyear', $examyear);
  $stmt->bindParam('examsubjec', $examsubject);
  $stmt->bindParam('exammarks', $mark);


$stmt->execute();
}
header('Location: ../home.php');
?>

1 个答案:

答案 0 :(得分:0)

您的实时服务器上的exammarks表定义可能包含本地主机服务器上不存在的唯一索引。如果确实如此,您的某些INSERT操作可能会失败。

您向我们展示的代码不会检查错误。显然,当您的程序处理高价值数据(例如学生考试的结果)时,您应该检查错误。

请改为尝试:

if( !$stmt->execute()) {
    print_r( $arr = $stmt->errorInfo() );
}
else {
    /* INSERT statement completed correctly */
}