2012-04-01 15 views
0

このページでは、基本的には、テーブルと列の下を示し私は「2 & 3、割り当て1」と同じ時間

更新ページに複数の行&と列を更新することでグレードのテーブルを更新することができるようにしたいですユーザーが学生の課題のために新しい等級を入力するための入力ボックスがあります。入力ボックスは、データベース内の現在の値を表示するようにコード化されているため、ユーザーがレコードを1つだけ更新すると、ユーザーによって変更されない限り、すべてが現在の値で更新されます。

<?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> 

INSERTページ

私は私の問題は、データが更新/挿入できるように、前のページのフォームはここに指示し、ここで、これがあると信じています。前のページのコードは各行の入力フィールドを繰り返すため、すべての生徒の成績を更新できる必要があります。

<?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

inputボックスのそれぞれにjQuery/JavaScriptリスナを添付して、ダーティ(ユーザが変更した)ときにそれらの属性にマークを付けることができます。次に、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; 
}); 
(これは、私はちょうどそれをここに書いたので、それは擬似コードを検討テストされていません)
関連する問題