0

I'm just learning Mysql/PHP andI'm having some issues with my dropdownlist.

Problem : When I hit Sumbit, a query will run and check if the inserted number is availabe. When the inserted number is available, it will insert some values in the DB. Else, when the number is already been taken, it will show a errormsg. When this happens, my dropdownlist is suddenly empty, see screenshot below!

What I want : When I submit, I still want the error message to be shown whenever the number is already been taken and also to keep my dropdownlist intact.

Hope someone can help!

Code :

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
include("/includes/connect.php");
if(isset($_SESSION['logged_in'])) {

  $sql="SELECT id,model FROM `ipad-model`";
  $stmt = $db->prepare($sql);
  $stmt->execute();
  $result = $stmt->fetchAll();
 
 if(isset($_POST['btnAdd'])) {
  $errMsg = '';
  $nr=$_POST['nr'];
  $sql2="SELECT * FROM ipad WHERE nr=$nr";
  $stmt = $db->prepare($sql2);
  $stmt->execute();
  $result = $stmt->fetchAll();
   if($result == 0) {
    $nr=$_POST['nr'];
    $model=$_POST['model'];
    $serienummer=$_POST['serienummer'];
    $capaciteit=$_POST['capaciteit'];
    $uptodate=$_POST['uptodate'];
    $sql="INSERT INTO ipads(uitgeleend,nr,model,serienummer,capaciteit,uptodate) VALUES ('Nee','$nr','$model','$serienummer','$capaciteit', '$uptodate')";
    $result=$db->query($sql);
    header("location:index.php");
   } else {
    $errMsg = 'Nummer is al in gebruik!';
   }
 }
 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" />
  <?php
   if(isset($errMsg)){
    echo '<div style="color:#FF0000;text-align:center;font-size:12px;">'.$errMsg.'</div>';
   }
  ?>
    </div>

    <div class="form-group">
        <label>Model</label>
   <select class="form-control" name="model">
    <?php foreach($result as $row): ?>
    <option value="<?= $row['model']; ?>"><?= $row['model']; ?></option>
    <?php endforeach; ?>
   </select>
  <p class="text-right"><a href="ipad-model.php">Bewerken</a></p>
    </div>

    <div class="form-group">
        <label>Serienummer</label>
   <input type="text" class="form-control" name="serienummer" />
    </div>

    <div class="form-group">
        <label>Capaciteit</label>
  <select class="form-control" name="capaciteit">
   <option value="16 GB">16 GB</option>
   <option value="32 GB">32 GB</option>
   <option value="64 GB">64 GB</option>
   <option value="128 GB">128 GB</option>
  </select>
    </div>
    
    <div class="form-group">
        <label>Up-To-Date</label>
  <select class="form-control" name="uptodate">
   <option value="Ja">Ja</option>
   <option value="Nee">Nee</option>
  </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
  include('../includes/get_footer.php');
 ?>
    
</form>

<?php
} else header("location: index.php")
?>

Screenshots :

Before submit & errormsg : enter image description here

After Submit & errormsg : enter image description here

Khiem
  • 157
  • 6
  • 18
  • 1
    You should write code instead of screenshots, you need to tell more about what code you are using – techblu3 Mar 21 '16 at 15:10
  • This is a possible duplication of http://stackoverflow.com/questions/2246227/keep-values-selected-after-form-submission – Dan Costinel Mar 21 '16 at 15:24

1 Answers1

1

It's because your $result variable is overwritten.

You should use different names for different result sets.

So for example, I'd do something like this:

$sql="SELECT id,model FROM `ipad-model`";
$stmt = $db->prepare($sql);
$stmt->execute();
$ipadModels = $stmt->fetchAll();

And below, in the "view" part, where you render the dropdown:

<?php foreach($ipadModels as $ipadModel): ?>
    <option value="<?= $ipadModel['id']; ?>"><?= $ipadModel['model']; ?></option>
<?php endforeach; ?>

Hope this helps!

Radu