2012-04-01 71 views
0

更新页我希望能够通过在同一时间

此页面基本上示出了表,并在所述列更新多个行和及列来更新等级的表“分配1,2 & 3”有一个输入框供用户输入学生作业的新成绩。输入框被编码以显示数据库中的当前值,因此如果用户只更新一条记录,则除非用户更改,否则所有数据都将使用当前值进行更新。

<?php 
if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 


if (!function_exists("GetSQLValueString")) { 
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{ 
    if (PHP_VERSION < 6) { 
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; 
    } 

    $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); 

    switch ($theType) { 
    case "text": 
     $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; 
     break;  
    case "long": 
    case "int": 
     $theValue = ($theValue != "") ? intval($theValue) : "NULL"; 
     break; 
    case "double": 
     $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; 
     break; 
    case "date": 
     $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; 
     break; 
    case "defined": 
     $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; 
     break; 
    } 
    return $theValue; 
} 
} 

mysql_select_db("kackieco_final",$con); 
$query_Recordset1 = "INSERT INTO grades (s_id, f_name, l_name)SELECT s_id, f_name, l_name FROM users WHERE NOT EXISTS (SELECT * FROM grades 
WHERE grades.s_id = users.s_id)"; 
$Recordset1 = mysql_query($query_Recordset1, $con) or die(mysql_error()); 


$query_Recordset2 = "SELECT * FROM grades"; 
$Recordset2 = mysql_query($query_Recordset2, $con) or die(mysql_error()); 
$row_Recordset2 = mysql_fetch_assoc($Recordset2); 
$totalRows_Recordset2 = mysql_num_rows($Recordset2); 
?> 
<form action="admin_post_grades.php" method="post"> 

<table class="sortable" border="1" cellspacing="2" cellpadding="4"> 
<tr> 
<th width="78"><div align="center">Student ID</div></th> 
<th width="78"><div align="center">First Name</div></th> 
<th width="78"><div align="center">Surname</div></th> 
<th width="78"><div align="center">Assignment 1</div></th> 
<th width="78"><div align="center">Assignment 2</div></th> 
<th width="78"><div align="center">Assignment 3</div></th> 
<th width="78"><div align="center">Grade</div></th> 
</tr> 




<?php do { ?> 
    <tr> 

    <td><?php echo $row_Recordset2['s_id']; ?></td> 
    <td><?php echo $row_Recordset2['f_name']; ?></td> 
    <td><?php echo $row_Recordset2['l_name']; ?></td> 
    <td><input type="text" name="ass1" maxlength="3" size="10" value="<?php echo $row_Recordset2['ass1']; ?>"/> %</td> 
    <td><input type="text" name="ass2" maxlength="3" size="10" value="<?php echo $row_Recordset2['ass2']; ?>" /> %</td> 
    <td><input type="text" name="ass3" maxlength="3" size="10" value="<?php echo $row_Recordset2['ass3']; ?>" /> %</td> 
    <td><?php echo $row_Recordset2['grade']; ?>%</td>  
    </tr> 

<?php } while ($row_Recordset2 = mysql_fetch_assoc($Recordset2)); ?> 
</table> 


<?php 
mysql_free_result($Recordset2); 
?> 
<input type="hidden" name="s_id" value="<?=$row_Recordset2["s_id"]; ?>" > 
<input id="but" type="submit" value="Add"/> 
     </form> 

插入页

我相信,这是我的问题是,前面的页面上的形式引导到这里,这样的数据可以被插入/更新。由于前一页上的代码重复每行的输入字段,这意味着它需要能够更新所有学生的成绩。

<?php $con = mysql_connect("localhost","*******","*******"); 
if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 

$a1 = $_POST['ass1']; 
$a2 = $_POST['ass2']; 
$a3 = $_POST['ass3']; 
$sid = $_POST['s_id']; 

mysql_select_db("kackieco_final",$con); 
$q = "UPDATE grades SET ass1='$a1', ass2='$a2', ass3='$a3'"; 

$r = mysql_query($q, $con) or die(mysql_error()); 

if (mysql_affected_rows($con) == 1) { 

    echo '<script type="text/javascript" language="javascript"> 
    alert("The record has been successfully Added"); 
    window.back();</script>'; 
    } 

    else { 
    echo '<script type="text/javascript" language="javascript"> 
    alert("The entry could not be added due to a system error"); 
    window.back()</script>'; 
    } 

mysql_close($con); 

echo "<meta http-equiv='refresh' content='2;url=admin_grades.php'>"; 
?> 

如果您花时间阅读本文,那么我非常感谢您,并且非常感谢您能够给予的任何帮助。我希望它也有意义!

回答

0

您可以将jQuery/JavaScript侦听器附加到每个input框,并在脏(用户已更改它)时使用属性标记它们。然后将一个OnSubmit处理程序添加到表单中,以便在提交页面时清除任何没有设置为isDirty属性的输入。最后,在INSERT页面上,仅循环非空白字段并在数据库中更新它们。

(这是未经测试我只是写在这里这么认为伪代码)

$('input').change(function() { 
    $(this).attr('isDirty', 1); 
}); 

$('form').submit(function() { 
    $('input').each(function(){ 
    if($(this).attr('isDirty') != 1) 
    { 
     $(this).val(''); 
    } 
    }); 
    return true; 
}); 
相关问题