开发者

For Loop + Wrong Record Inserted

Hello I have a form which have subj_ids. So here's the scenario. I have subj_ids 1, 2, 3, 4, 6 but what happens everytime I am submitting the form it inserts a record with 0 value plus it doesn't insert a record with a subj_id of 6.

FORM:

    <tr>
        <td width="225"><?= $row_subjects[1];?>
        <?php if($row_active_term[0] == '1st' && $row_active_term[1] == '1' ) {?>
        <td align="center"  width="225">
            <?php if($row_subjects['indicator'] == 'editable'){?>
                <input type="hidden" name="subj_id_<?= $row_subjects[0]?>" id="subj_id_<?= $row_subjects[0]?>" value="<?= $row_subjects[0]?>" />
                <input type="text" name="grade_<?= $row_subjects[0]?>" id="grade_<?= $row_subjects[0]?>" size="5" value="<?= number_format($row_subjects[4], 1)?>" />
            <?php } else {?>
                <a href="#" title="This is your downfall">?</a>
            <?php } ?>
        </td>
        <?php } ?>
    </tr>

Submission: I guess this is where the bad code lies. . .

    <?php
include('conn.php');
$uid = $_SESSION['uid'];
$rs_count_subjs = mysql_query("select count(*) as x from teachers_subjects where tid = $uid");
$row_count_subjs = mysql_fetch_array($rs_count_subjs);
$x = $row_count_subjs['x'];
$term = $_POST['term'];
//echo "select count(*) as x from teachers_subjects where tid = $uid";

$rs_subjs = mysql_query("select * from teachers_subjects where tid = $uid");
$row_subjs = mysql_fetch_array($rs_count_subjs);

for($i=0; $i<=$x; $i++) {
$t = $row_subjs[0];
$sid = $_POST['sid'];
$s = $_POST['subj_id_'.$i];
$y = $_POST['grade_'.$i];
$rs = mysql_query("select * from student_friggin_grades where sid = '$sid' AND subj_id = '$s' AND term = '$term' AND tid = $uid");
$row = mysql_fetch_array($rs);
$nu开发者_Go百科m = mysql_num_rows($rs);
    if($num > 0) {
        $update_grade = "update set grade = '$y' where sid = '$sid' AND subj_id = '$s' AND term = '$term' AND tid = $uid";
        $updated_grade = mysql_query($update_grade);
        $is_updated = mysql_affected_rows();
        echo "update set grade = '$y' where sid = '$sid' AND subj_id = '$s' AND term = '$term' AND tid = $uid";
    } else {
        $update_grade = "insert into student_friggin_grades(`sid`, `subj_id`, `term`, `grade`, `tid`, `date_added`, `date_updated`) 
                values('$sid', '$s', '$term', '$y', $uid, NOW(), NOW())";
        $updated_grade = mysql_query($update_grade);
        $is_updated = mysql_affected_rows();
    }
}

if($is_updated > 0) {
    header("Location: show_grades.php?sid=$sid");
}
?>

Please help me,

Xxxxxxxxxxxxxxxx


assuming $x is equal to 6,

for($i=0; $i<=$x; $i++) {

will give you 0 through 6 if you want to traverse 1 through 6

for($i=1; $i <= $x; $i++) {

however this does not answer why you did not see 6, unless $x is in fact equal to 5.


one based vs zero based ? I think you have a loop where it terminates prior to the last record ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜