update查询不起作用。

问题是$rs->TestAssignmentId的值$currecord没有传递到if块。

This should happen when user presses the submit button

$sql="SELECT * FROM tbltestassignment
    WHERE TeacherId=".$_SESSION["UserId"]." AND StudentId=".$_GET["StudentId"]." AND TestId=".$_GET["TestId"];
$searchRes1 = Execute($sql);
while($rs=mysql_fetch_object($searchRes1)) {
    $temp = GetSingleReconrd('tbltestassignment','QuestionIds','StudentId',$rs->StudentId);
    $quesIds = explode(',',$temp);
    $totalQues = count($quesIds);
    $currecord = $rs->TestAssignmentId;
    $temp = GetSingleReconrd('tbltestassignment','AnswerIds','StudentId',$rs->StudentId);
    $ansIds = explode(',',$temp);
}

    //echo "<pre>".$currecord."</pre>";
if(isset($_REQUEST["epSave"])) {
    $score = array_filter($_REQUEST['score']);
    $sqlup = 'UPDATE tbltestassignment SET `Score` = "'.$score.'" WHERE `TestAssignmentId` = '.$currecord;
    mysql_query($sqlup);

    $_SESSION["_msg"] = "Marks Successfully Added..!";
    $_SESSION["_msgtype"] = 1;
    $a=8;
    $url = "correct.php?TestId=".$a;//$_GET['TestId'];
    header("location:".$url);
    die();
}




<?php
    for($i=0; $i<$totalQues; $i++){
?>
<tr>
    <td style="text-align:center">
        <input name="score[]" id="score<?php echo $i ?>" type="text" placeholder="Enter marks here">
    </td>
</tr><?php } ?>

最佳答案

停止使用mysql_*

开始使用类似PDO之类的东西,这是一个不错的tutorial for MySQL developers。您应该尽快进行更改,因为您的代码对sql injection开放。以简单的形式,带有一些表名:

$stmt = $db->prepare( "SELECT FROM tbltestassignment WHERE TeacherId=?" );
$stmt->execute(array(
    $_SESSION["UserId"]),
);
$rows = $stmt->fetch( \PDO::FETCH_ASSOC );




$currecord可能未定义,因为

while($rs=mysql_fetch_object($searchRes1)){


不会发生。该流程应进行修改,似乎只有在这种情况下才会发生,因为其结果仅在此处使用(AFAIK)

很简单,我删除了一部分内容以仅表示逻辑,该流程可能会帮助您:

if ( isset($_REQUEST["epSave"]) ) {
    $sql = "SELECT * FROM tbltestassignment WHERE TeacherId=? AND StudentId=? AND TestId=?";
    $stmt = $pdo ->prepare($sql);
    $stmt->execute([
        $_SESSION["UserId"],
        $_GET["StudentId"],
        $_GET["TestId"],
    ]);
    if ($stmt->rowCount() > 0) {
        while( $rs = $stmt->fetch(\PDO::FETCH_ASSOC) ) {
            // here you know what to do, $rs is an array
        }
    }
}

关于php - PHP中的MySQL更新无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30976918/

10-11 12:58