By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,069 Members | 1,249 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,069 IT Pros & Developers. It's quick & easy.

Form With Selection to MYSQL - PHP

P: 1
I want to use this form with multiple select box to put data to database. I can put all data, but I can't put selections. What I am missing?

HTML FORM:

Expand|Select|Wrap|Line Numbers
  1. <form action="insertcheckbox.php" method="POST">
  2.  
  3.         <div class="modal-body">
  4.  
  5.             <div class="form-group">
  6.                 <label> Ime i Prezime </label>
  7.                 <input type="text" name="ime" class="form-control" placeholder="Upišite Ime i Prezime">
  8.             </div>
  9.             <div class="form-group">
  10.                 <label> Upišite Spol </label>
  11.                 <input type="text" name="spol" class="form-control" placeholder="Upišite Spol (M/Ž)">
  12.             </div>
  13.             <div class="form-group">
  14.                 <label>Grad</label>
  15.                 <input style="font-style: #f89900" type="text" name="grad" class="form-control" placeholder="Upišite Grad iz kojeg dolazi Student">
  16.             </div>
  17.             <div class="form-group">
  18.                 <label>Telefon</label>
  19.                 <input style="font-style: #f89900" type="text" name="tel" class="form-control" placeholder="Upišite Telefon">
  20.             </div>
  21.             <div class="form-group">
  22.                 <label>Email</label>
  23.                 <input style="font-style: #f89900" type="email" name="email" class="form-control" placeholder="Upišite Email">
  24.             </div>
  25.             <div class="form-group">
  26.                 <label>Uzrast</label>
  27.                 <input style="font-style: #f89900" type="text" name="uzrast" class="form-control" placeholder="Upišite Uzrast">
  28.             </div>
  29.             <div class="form-group">
  30.                 <label>Škola</label>
  31.                 <input style="font-style: #f89900" type="text" name="skola" class="form-control" placeholder="Upišite Obrazovnu Ustanovu">
  32.             </div>
  33.  
  34.             <div class="form-group">
  35.               <label>Izaberite Programe sa liste</label>
  36.              <select name="naziv[]" id="naziv" class="form-control action" multiple="">
  37.               <option disabled selected>-- Izaberi Program --</option>
  38.               <?php
  39.                 include "config/database.php";  // Using database connection file here
  40.                 $records = mysqli_query($conn, "SELECT naziv FROM program");  // Use select query here 
  41.  
  42.                 while($data = mysqli_fetch_array($records))
  43.                 {
  44.                 echo "<option value='". $data['naziv'] ."'>" .$data['naziv'] ."</option>";  // displaying data in option menu
  45.                 } 
  46.               ?>  
  47.             </select> 
  48.             </div>
  49.         </div>
  50.         <div class="modal-footer">
  51.             <button type="button" class="btn btn-secondary" data-dismiss="modal">Zatvori</button>
  52.             <button type="submit" value="submit" name="registerbtn" class="btn btn-primary">Pošalji</button>
  53.         </div>
  54.       </form>
PHP FORM:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include_once 'config/database.php';
  3. if(isset($_POST['Register']))
  4. {  
  5.    $ime = $_POST['ime'];
  6.    $spol = $_POST['spol'];
  7.    $grad = $_POST['grad'];
  8.    $tel = $_POST['tel'];
  9.    $email = $_POST['email'];
  10.    $uzrast = $_POST['uzrast'];
  11.    $skola = $_POST['skola'];
  12.    $naziv = $_POST['naziv'];
  13.    $sql = "INSERT INTO students (ime, spol, grad, tel, email, uzrast, skola, naziv)
  14.    VALUES ('$ime','$spol','$grad','$tel', '$email', '$uzrast', '$skola', '$naziv')";
  15.    if (mysqli_query($conn, $sql)) {
  16.     echo "New record created successfully !";
  17.    } else {
  18.     echo "Error: " . $sql . "
  19. " . mysqli_error($conn);
  20.    }
  21.    mysqli_close($conn);
  22. }
  23. ?>
1 Week Ago #1
Share this Question
Share on Google+
1 Reply


P: 21
You are Storing multiple select box values to an array so, save the values to the database using comma-separated.

Expand|Select|Wrap|Line Numbers
  1. $naziv = implode(',', $_POST['naziv']);
  2.  
2 Days Ago #2

Post your reply

Sign in to post your reply or Sign up for a free account.