插入多个基于搜索结果的记录时出现问题。搜索字段工作正常。但问题是插入不起作用。另外,在我插入记录之前,我必须根据我的需要编辑“等级”字段。这是我的代码。
<?php
require_once('../../_includes/initialize.php');
?>
<html><br />
<form action="search.php" method="POST">
<?php
echo "<b><center>Search Results</center></b><br><br>";
if (isset($_POST['search_name'])) {
$search_name = $_POST['search_name'];
if (!empty($search_name)) {
$query="SELECT * FROM students WHERE course like '%".mysql_real_escape_string($search_name)."%'";
$result=mysql_query($query);
$num=mysql_num_rows($result);
if (($num) >=1) {
//echo 'Found.';
$i=0;
while ($i < $num) {
$id=mysql_result($result,$i,"id");
$f_name=mysql_result($result,$i,"f_name");
$g=mysql_result($result,$i,"dump");
?>
<input name="id" type="text" value="<?php echo @$id; ?>" size="40" />
<input name="f_name" type="text" value="<?php echo @$f_name; ?>" size="40" />
<input name="a" type="text" value="<?php echo $g; ?>" size="40" />
<br />
<?php
$i++;
}
} else {
echo 'No results found.';
}
}
}
?>
<?php
if (isset($_POST['Submit_Grade'])) {
$query = "INSERT INTO samplestudgrade (id, name, grade) VALUES ('".$_POST["id"]."','".$_POST["f_name"]."','".$_POST["a"]."')";
$query_run = mysql_query($query);
}
?>
<input name="search_name" type="text" value="search..." />
<input type="submit" value="Search"/>
<input type="submit" name="Submit_Grade" value="submit" />
</form>
</html>
答案 0 :(得分:0)
试试这段代码:
已编辑:
<html><br />
<form action="search.php" method="POST">
<?php
echo "<b><center>Search Results</center></b><br><br>";
if (isset($_POST['search_name'])) {
$search_name = $_POST['search_name'];
if (!empty($search_name)) {
$query = "SELECT * FROM students WHERE course like '%" . mysql_real_escape_string($search_name) . "%'";
$result = mysql_query($query);
$num = mysql_num_rows($result);
if (($num) >= 1) {
//echo 'Found.';
$i = 0;
while ($i < $num) {
$id = mysql_result($result, $i, "id");
$f_name = mysql_result($result, $i, "f_name");
$g = mysql_result($result, $i, "dump");
?>
<input name="id[]" type="text" value="<?php echo @$id; ?>" size="40" />
<input name="f_name[]" type="text" value="<?php echo @$f_name; ?>" size="40" />
<input name="a[]" type="text" value="<?php echo $g; ?>" size="40" />
<br />
<?php
$i++;
}
} else {
echo 'No results found.';
}
}
}
?>
<?php
if (isset($_POST['Submit_Grade'])) {
$ctr = 0;
foreach ($_POST['id'] as $id) :
if ($_POST["a"] >= 75) {
$status = "Passed";
} elseif ($_POST["a"] >= 73) {
$status = "Conditional";
} elseif ($_POST["a"] >= 70) {
$status = "Failed";
} else {
$status = "Incomplete";
}
$query = "INSERT INTO samplestudgrade (id, name, grade, status) VALUES ('" . $id . "','" . $_POST["f_name"][$ctr] . "','" . $_POST["a"][$ctr] . "','" . $status . "')";
$query_run = mysql_query($query);
$ctr++;
endforeach;
}
?>
<input name="search_name" type="text" value="search..." />
<input type="submit" value="Search"/>
<input type="submit" name="Submit_Grade" value="submit" />
</form>
</html>