<form class="appnitro" method="post" action="">
<div class="form_description">
<center><h2>NOMINATE ENTRY</h2></center>
<p><center><font size='3'>
<?php
$con=mysqli_connect("localhost","user",pass","db");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$dept = $_POST['department'];
$class = $_POST['class'];
$result = mysqli_query($con,"SELECT * FROM prizemaster");
$result1 = mysqli_query($con,"SELECT * FROM studentmaster WHERE dept='$dept' and class='$class'");
echo "<table border='1'>
<tr>
<th>Prize ID        </th>
<th>Prize Name         </th>
<th>Name             </th>
</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['prizeid'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td><select name='name'>";
echo "<option></option>";
while($drop = mysqli_fetch_array($result1))
{
echo "<option value='".$drop['name']."'>" . $drop['name'] . "</option>";
}
mysqli_data_seek($result1, 0);
echo "</select></td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?></center></font></div>
<p>
<center><button type="submit" formaction="stnomins.php">Nominate</button></center>
</form>
以上是表单,当点击提名按钮时,我希望为所选的每个下拉值执行此代码:
<?php
$con=mysqli_connect("localhost","user","pass","db");
$myname = $_POST['name'];
$sql2="SELECT * FROM studentmaster WHERE name='$myname'";
$result = mysqli_query($con,$sql2)or die(mysqli_error());
$row = mysqli_fetch_array($result);
$mydept=$row['dept'];
$myclass=$row['class'];
$myregno=$row['regno'];
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql1="INSERT INTO studenttransaction (`transid`, `date`, `prizeid`, `regno`, `name`, `class`, `department`, `status`) VALUES ('' , CURRENT_TIMESTAMP() , '', '$myregno', '$myname', '$myclass', '$mydept', '1')";
if (!mysqli_query($con,$sql1))
{
die('Error: ' . mysqli_error($con));
}
mysqli_close($con);
?>
请帮助我,我需要在2天内提交。请用代码解释,因为我是php的新手并且不完全知道它
答案 0 :(得分:1)
我更新了你的html&amp; php代码,请看一下:
在表单文件中:
echo "<td><select name='name[]'>"; // added [] here to make it array
echo "<option value=''>Select student</option>"; // added value & option item
上面这些代码行已在相应的
文件中进行了相应的修改 <form class="appnitro" method="post" action="">
<div class="form_description">
<center><h2>NOMINATE ENTRY</h2></center>
<p><center><font size='3'>
<?php
$con=mysqli_connect("localhost","user",pass","db");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$dept = $_POST['department'];
$class = $_POST['class'];
$result = mysqli_query($con,"SELECT * FROM prizemaster");
$result1 = mysqli_query($con,"SELECT * FROM studentmaster WHERE dept='$dept' and class='$class'");
echo "<table border='1'>
<tr>
<th>Prize ID        </th>
<th>Prize Name         </th>
<th>Name             </th>
</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['prizeid'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td><select name='name[]'>";
echo "<option value=''>Select student</option>";
while($drop = mysqli_fetch_array($result1))
{
echo "<option value='".$drop['name']."'>" . $drop['name'] . "</option>";
}
mysqli_data_seek($result1, 0);
echo "</select></td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?></center></font></div>
<p>
<center><button type="submit" name="nominate" formaction="stnomins.php">Nominate</button></center>
</form>
略微更新您的PHP代码。如果点击/提交按钮,则为每个下拉名称启动一个循环。
<?php
$con=mysqli_connect("localhost","user","pass","db");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_POST['nominate'])){
foreach($_POST['name'] as $key => $myname){
//$myname = $_POST['name'];
$sql2="SELECT * FROM studentmaster WHERE name='$myname'";
$result = mysqli_query($con,$sql2)or die(mysqli_error());
if($row = mysqli_fetch_array($result)){
$mydept=$row['dept'];
$myclass=$row['class'];
$myregno=$row['regno'];
$sql1="INSERT INTO studenttransaction (`transid`, `date`, `prizeid`, `regno`, `name`, `class`, `department`, `status`) VALUES ('' , CURRENT_TIMESTAMP() , '', '$myregno', '$myname', '$myclass', '$mydept', '1')";
if (!mysqli_query($con,$sql1)){
die('Error: ' . mysqli_error($con));
}
}//if mysqli_fetch_array condition closed
}// for loop closed
}// if submit button(nominate) closed
mysqli_close($con);
?>
希望这对你有用。
答案 1 :(得分:-1)
工作正常。我还有一个疑问是,当选择下拉值时,如何回显下拉值。任何知道答案的人,请提供解决方案。