我只是在學習Mysql/PHP,我的下拉列表有一些問題。當我點擊提交時,Dropdownlist項目消失,當數值已經在DB
問題:當我點擊Sumbit時,查詢會運行並檢查插入的數字是否可用。當插入的號碼可用時,它將在數據庫中插入一些值。否則,當號碼已被佔用時,它將顯示errormsg。發生這種情況時,我的下拉列表突然變空,請參閱下面的截圖!
我想要什麼:當我提交時,我仍然希望顯示錯誤消息,只要號碼已被採用,並保持我的下拉列表完好無損。
希望有人能幫助!
代碼:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
include("/includes/connect.php");
if(isset($_SESSION['logged_in'])) {
\t \t $sql="SELECT id,model FROM `ipad-model`";
\t \t $stmt = $db->prepare($sql);
\t \t $stmt->execute();
\t \t $result = $stmt->fetchAll();
\t
\t if(isset($_POST['btnAdd'])) {
\t \t $errMsg = '';
\t \t $nr=$_POST['nr'];
\t \t $sql2="SELECT * FROM ipad WHERE nr=$nr";
\t \t $stmt = $db->prepare($sql2);
\t \t $stmt->execute();
\t \t $result = $stmt->fetchAll();
\t \t \t if($result == 0) {
\t \t \t \t $nr=$_POST['nr'];
\t \t \t \t $model=$_POST['model'];
\t \t \t \t $serienummer=$_POST['serienummer'];
\t \t \t \t $capaciteit=$_POST['capaciteit'];
\t \t \t \t $uptodate=$_POST['uptodate'];
\t \t \t \t $sql="INSERT INTO ipads(uitgeleend,nr,model,serienummer,capaciteit,uptodate) VALUES ('Nee','$nr','$model','$serienummer','$capaciteit', '$uptodate')";
\t \t \t \t $result=$db->query($sql);
\t \t \t \t header("location:index.php");
\t \t \t } else {
\t \t \t \t $errMsg = 'Nummer is al in gebruik!';
\t \t \t }
\t }
\t include("/includes/get_header.php");
?>
<h1 class="page-title">Nieuwe iPad toevoegen</h1>
<ul class="breadcrumb">
<li><a href="index.php">Home</a> </li>
<li class="active">Nieuwe iPad</li>
</ul>
</div>
<form id="gegevensForm1" class="col-xs-4" form method="POST" action="ipad-toevoegen.php">
<div class="form-group">
<label>Nr</label>
<input type="text" class="form-control" name="nr" />
\t \t <?php
\t \t \t if(isset($errMsg)){
\t \t \t \t echo '<div style="color:#FF0000;text-align:center;font-size:12px;">'.$errMsg.'</div>';
\t \t \t }
\t \t ?>
</div>
<div class="form-group">
<label>Model</label>
\t \t \t <select class="form-control" name="model">
\t \t \t \t <?php foreach($result as $row): ?>
\t \t \t \t <option value="<?= $row['model']; ?>"><?= $row['model']; ?></option>
\t \t \t \t <?php endforeach; ?>
\t \t \t </select>
\t \t <p class="text-right"><a href="ipad-model.php">Bewerken</a></p>
</div>
<div class="form-group">
<label>Serienummer</label>
\t \t \t <input type="text" class="form-control" name="serienummer" />
</div>
<div class="form-group">
<label>Capaciteit</label>
\t \t <select class="form-control" name="capaciteit">
\t \t \t <option value="16 GB">16 GB</option>
\t \t \t <option value="32 GB">32 GB</option>
\t \t \t <option value="64 GB">64 GB</option>
\t \t \t <option value="128 GB">128 GB</option>
\t \t </select>
</div>
<div class="form-group">
<label>Up-To-Date</label>
\t \t <select class="form-control" name="uptodate">
\t \t \t <option value="Ja">Ja</option>
\t \t \t <option value="Nee">Nee</option>
\t \t </select>
</div>
<button class="btn btn-primary pull-right" name="btnAdd" type="submit"><i class="fa fa-save"></i> Opslaan</button>
<a href="index.php"><input type="button" name="btnCancel" value="Annuleer" class="btn btn-primary pull-left"></a>
<?php
\t \t include('../includes/get_footer.php');
\t ?>
</form>
<?php
} else header("location: index.php")
?>
截圖:
你應該寫代碼,而不是截圖,你需要告訴更多關於你正在使用 – techblu3
什麼碼這是http://stackoverflow.com/questions/2246227/keep-values-的一個可能的重複選擇後提交表格 –