2012-04-01 36 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; 
}); 
相關問題