我使用“php do”和“php while”来显示所有记录。 并且每个记录都有“更新记录”按钮,我试图按一个按钮来更新这些记录。 我需要的只是一个按钮来更新它们,任何人都可以帮我制作它吗?
这是代码:
<?php require_once('Connections/tlsc_conn.php'); ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $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;
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
$updateSQL = sprintf("UPDATE tb_exam SET answer1=%s, answer2=%s, answer3=%s, answer4=%s, correct=%s WHERE question_id=%s",
GetSQLValueString($_POST['answer1'], "text"),
GetSQLValueString($_POST['answer2'], "text"),
GetSQLValueString($_POST['answer3'], "text"),
GetSQLValueString($_POST['answer4'], "text"),
GetSQLValueString($_POST['correct'], "text"),
GetSQLValueString($_POST['question_id'], "int"));
mysql_select_db($database_tlsc_conn, $tlsc_conn);
$Result1 = mysql_query($updateSQL, $tlsc_conn) or die(mysql_error());
}
mysql_select_db($database_tlsc_conn, $tlsc_conn);
$query_Recordset1 = "SELECT * FROM tb_exam";
$Recordset1 = mysql_query($query_Recordset1, $tlsc_conn) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript">
function myFunction()
{
alert("Record has been updated!");
}
</script>
<style type="text/css">
.style13 {color: #000000}
.style12 {color: #FFFFFF}
</style>
</head>
<body>
<tr>
<td> </td>
</tr>
<p> </p>
<?php do { ?>
<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
<p> </p>
<table align="center">
<tr valign="baseline">
<td nowrap align="right">Question_id:</td>
<td><?php echo $row_Recordset1['question_id']; ?></td>
</tr>
<tr valign="baseline">
<td nowrap align="right"><?php echo $row_Recordset1['answer1']; ?></td>
<td><input type="checkbox" name="correct" value="<?php echo $row_Recordset1['answer1']; ?>" size="32">
<input name="answer1" type="hidden" value="<?php echo $row_Recordset1['answer1']; ?>" /></td>
</tr>
<tr valign="baseline">
<td nowrap align="right"><?php echo $row_Recordset1['answer2']; ?></td>
<td><input type="checkbox" name="correct" value="<?php echo $row_Recordset1['answer2']; ?>" size="32">
<input name="answer2" type="hidden" value="<?php echo $row_Recordset1['answer2']; ?>" /></td>
</tr>
<tr valign="baseline">
<td nowrap align="right"><?php echo $row_Recordset1['answer3']; ?></td>
<td><input type="checkbox" name="correct" value="<?php echo $row_Recordset1['answer3']; ?>" size="32">
<input name="answer3" type="hidden" value="<?php echo $row_Recordset1['answer3']; ?>" /></td>
</tr>
<tr valign="baseline">
<td nowrap align="right"><?php echo $row_Recordset1['answer4']; ?></td>
<td><input type="checkbox" name="correct" value="<?php echo $row_Recordset1['answer4']; ?>" size="32">
<input name="answer4" type="hidden" value="<?php echo $row_Recordset1['answer4']; ?>" /></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">Correct:</td>
<td><?php echo $row_Recordset1['correct']; ?></td>
</tr>
<tr valign="baseline">
<td nowrap align="right"> </td>
<td><input name="submit" type="submit" value="Update record" /></td>
</tr>
</table>
<p> </p>
<p>
<input type="hidden" name="MM_update" value="form1">
<input type="hidden" name="question_id" value="<?php echo $row_Recordset1['question_id']; ?>">
</p>
</form>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>