I have form where I get data from table to listbox. I can insert data, but no any from listbox. It says array for one option, and for another one I get from array to string conversion error.
<form action="dodajstudenta.php" method="POST">
<div class="modal-body">
<div class="form-group">
<label> Ime i Prezime </label>
<input type="text" name="ime" class="form-control" placeholder="Upišite Ime i Prezime">
</div>
<div class="form-group">
<label> Upišite Spol </label>
<input type="text" name="spol" class="form-control" placeholder="Upišite Spol (M/Ž)">
</div>
<div class="form-group">
<label>Grad</label>
<input style="font-style: #f89900" type="text" name="grad" class="form-control" placeholder="Upišite Grad iz kojeg dolazi Student">
</div>
<div class="form-group">
<label>Telefon</label>
<input style="font-style: #f89900" type="text" name="tel" class="form-control" placeholder="Upišite Telefon">
</div>
<div class="form-group">
<label>Email</label>
<input style="font-style: #f89900" type="email" name="email" class="form-control" placeholder="Upišite Email">
</div>
<div class="form-group">
<label>Uzrast</label>
<input style="font-style: #f89900" type="text" name="uzrast" class="form-control" placeholder="Upišite Uzrast">
</div>
<div class="form-group">
<label>Škola</label>
<input style="font-style: #f89900" type="text" name="skola" class="form-control" placeholder="Upišite Obrazovnu Ustanovu">
</div>
<div class="form-group">
<label>Izaberite Programe sa liste</label>
<select name="naziv[]" id="naziv" class="form-control action" id="naziv" multiple="">
<option disabled selected>-- Izaberi Program --</option>
<?php
include "config/database.php";
$records = mysqli_query($conn, "SELECT naziv FROM program");
while($data = mysqli_fetch_array($records))
{
echo "<option value='". $data['naziv'] ."'>" .$data['naziv'] ."</option>";
}
?>
</select>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Zatvori</button>
<button type="submit" value="Add Student" name="addstudent" class="btn btn-primary">Pošalji</button>
</div>
</form>
OPTION ONE PHP: called action dodajstudenta.php -> this works fine, but I get inserted in database table Array not, options form listbox.
<?php
require('config/database.php');
if (isset($_REQUEST['ime'])) {
$ime = stripslashes($_REQUEST['ime']);
$ime = mysqli_real_escape_string($conn, $ime);
if (isset($_REQUEST['spol'])) {
$spol = stripslashes($_REQUEST['spol']);
$spol = mysqli_real_escape_string($conn, $spol);
if (isset($_REQUEST['grad'])) {
$grad = stripslashes($_REQUEST['grad']);
$grad = mysqli_real_escape_string($conn, $grad);
if (isset($_REQUEST['telefon'])) {
$telefon = stripslashes($_REQUEST['telefon']);
$telefon = mysqli_real_escape_string($conn, $telefon);
if (isset($_REQUEST['email'])) {
$email = stripslashes($_REQUEST['email']);
$email = mysqli_real_escape_string($conn, $email);
if (isset($_REQUEST['uzrast'])) {
$uzrast = stripslashes($_REQUEST['uzrast']);
$uzrast = mysqli_real_escape_string($conn, $uzrast);
if (isset($_REQUEST['skola'])) {
$skola = stripslashes($_REQUEST['skola']);
$skola = mysqli_real_escape_string($conn, $skola);
if (isset($_REQUEST['naziv'])) {
$naziv = stripslashes($_REQUEST['naziv']);
$naziv = mysqli_real_escape_string($conn, $naziv);
$naziv=$_POST["naziv"];
$query = "INSERT into `students` (ime, spol, grad, tel, email, uzrast, skola, naziv) VALUES ('$ime', '$spol', '$grad', '$tel', '$email', '$uzrast', '$skola', '$naziv')";
$result = mysqli_query($conn, $query);
if ($result) {
header('Location: studenti.php');
echo "<div class='form'>
<h3>Uspješno ste dodali program.</h3><br/>
</div>";
} else {
echo "<div class='form'>
<h3>Niste sva polja popunili.</h3><br/>
</div>";
}
}
}
}
}
}
}
}
} else {
}
?>
OPTION TWO PHP: here I get problem with array string coversion
<?php
include("config/database.php");
if (isset($_POST['addstudent'])) {
$ime = $_POST['ime'];
$spol = $_POST['spol'];
$grad = $_POST['grad'];
$tel = $_POST['tel'];
$email = $_POST['email'];
$uzrast = $_POST['uzrast'];
$skola = $_POST['skola'];
$naziv = $_POST['naziv'];
$query = "INSERT INTO multiple `students`(`ime`, `spol`, `grad`, `tel`, `email`, `uzrast`, `skola`, `naziv`) VALUES ('$ime', '$spol', '$grad', '$tel', '$email','$uzrast','$skola','$naziv');";
if (mysqli_query($conn,$query)) {
$datainsert['insertsucess'] = '<p style="color: green;">Student Inserted!</p>';
}else{
$datainsert['inserterror']= '<p style="color: red;">Student Not Inserted, please input right informations!</p>';
}
}
?>
How acctually I can insert data, I am not so much good with javascript. Can I do it only with php?
What I have tried:
I tried different options, I would like to make to insert it automatically from listbox, or to put data into text box, and than insert into datatable.