如何根据您在组合框中选择的数据更新数据库中的数据

时间:2016-08-30 22:18:17

标签: php mysql mysqli

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
//Create connection
$con = mysqli_connect($servername,$username,$password,$dbname);
$name = $_POST['stud_id'];
$rank = $_POST['rank'];
if (!$name || !$rank) {
    $error="Please fill empty fields";
    include"login_reg.php";
    exit();
}
$sql = 'UPDATE login SET rank="'.$_POST['rank'].'" WHERE username="'.$_POST['stud_id'].'"';
$result = mysqli_query($con,$sql);
if (!$result && !$result2) {
   die("Error on mysql query".mysql_error());
} else {
    $nam="<center><h4><font color='#00FF00'>Successful update,</h4></center>   </font>";
    include "login_reg.php";
}         
?>

从组合框中选择数据时,我在更新数据时出现问题。这是我的代码。我不知道什么是错的。 我每次点击更新&#34;成功更新&#34;显示但数据没有更新。

这是另一个代码。

<!-- the login form-->
<form action="login_reg_action.php" method="post" id="myform">
            <div class="login">
            <table width="400px">

<tr>
<td><label>Username</label></td>
<td colspan="2"><span class="style8">
 <div align="center">
      <?php
      include 'connection.php';
      $sql = 'SELECT * FROM student ORDER BY firstname';
      $sele=mysqli_query($conn,$sql);
      ?>
      <select name="stud_id" style="width:250px; height:34px; border:1px solid #336666;">
        <option value="000" > [SELECT STUDENT]</option>
        <?php
       while($numu=mysqli_fetch_array($sele))
       {
      ?>
        <option value="<?php echo $numu['stud_id'];?> "> <?php echo   $numu["stud_id"];echo'  -  ';echo $numu["firstname"] ?></option>
        <?php
      }
      ?>
      </select>
      </label>
      </div>
</td>

</tr>
<tr>
<td><label>Rank</label></td>
<td><select name="rank" id="rank" style="width:250px; height:34px; border:1px solid #336666;">
<option value="000">Choose user rank</option>
<option value="student">Student</option>
<option value="candidate">Candidate</option>
<option value="administrator">Administrator</option>
</select></td>
</tr>
<tr><td colspan="2" align="left"><input type="submit" name="register"  value="Update" style="width:100px; height:34px; border:1px solid #336666; border-radius:4px;"></td></tr>
             </table>
            </div>
</form>
<script type="text/javascript">
 var frmvalidator = new Validator("myform"); 
 frmvalidator.addValidation("username","req","Please enter student username"); 
 frmvalidator.addValidation("username","maxlen=50");
 frmvalidator.addValidation("rank","dontselect=000","You don't select rank");
</script>
    </fieldset>
    </div>

</div>
</div>

0 个答案:

没有答案