Data aren't inserting into database PHP - php

I'm trying to add the data from my form into the database. But it's only refreshing. Here is my code for my form:
<form action="" method="post">
<div id="modal1" class="modal modal-fixed-footer">
<div class="modal-content">
<h4>Bedroom</h4><br><br>
<div class="row">
<div class="input-field col s6">
<i class="material-icons prefix">account_circle</i>
<input name="txt_room_ID" id="txt_room_ID" type="text" class="validate" readonly="true"
value="<?php
$sql= "select * from tblBedroom";
$stmt = sqlsrv_query($con,$sql);
while($rs = sqlsrv_fetch_array($stmt)){
$Setting_adminID = $rs['BedRoomID'];
$adminID_new = str_replace("B","",$Setting_adminID);
$adminID = str_pad($adminID_new + 1,11,0,STR_PAD_LEFT);}
echo htmlentities('B'. $adminID);
?>">
<label for="room_ID">Bedroom ID:</label>
</div>
<div class="input-field col s6">
<i class="material-icons prefix">account_circle</i>
<input name="txt_bedroom_no" id="txt_bedroom_no" type="text" class="validate">
<label for="bedroom_no">Room Number</label>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<select name="txt_Room_Type">
<option value="" disabled selected>Choose Room Type</option>
<?php
$sql = "select * from tblBedRoomType";
$stmt = sqlsrv_query($con,$sql);
while($rs = sqlsrv_fetch_array($stmt)){
$BedroomTypeID = $rs['BedroomTypeID'];
$BedroomName = $rs['BedroomName'];
echo"<option value='$bedroomTypeID'>$BedroomName</option>";
}
?>
</select>
<label>Room Type</label>
</div>
</div>
</div><!--END OF MODAL CONTENT-->
<div class="modal-footer">
<a class="modal-action modal-close waves-effect waves-green btn-flat ">Cancel</a>
<input id="btn_Save" name="btn_Save" type="submit" value="Save" class="waves-effect waves-green btn-flat " onclick="return confirm('Are You Sure You Want To Save?');">
</div>
</div>
</form>
The php below the form:
<?php
if(isset($_POST['btn_Save'])){
$txt_room_ID = $_POST['txt_room_ID'];
$txt_bedroom_no = $_POST['txt_bedroom_no'];
$txt_Room_Type = $_POST['txt_Room_Type'];
$AcctStat = 1;
$sql = "insert into tblBedRoom (BedRoomID,BedroomType,RoomNo,AcctStat)
values('$txt_room_ID','$txt_Room_Type','$txt_bedroom_no','$AcctStat')";
$stmt = sqlsrv_query($con,$sql);
echo "<meta http-equiv='refresh' content='0'>";
}
?>
And my columns in my tblBedRoom are:
BedRoomID | BedroomType | RoomNo |AcctStat

Related

how to separate multiple values or split multiple values using implode based on array?

I input data with dynamics form, where one column contains multiple data using implode (,). but when I input, the dynamic form manages to do as many inputs as I want, but for "mekanik" table it becomes one.
pict one : modal for input data
for "keluhan" has been filled each table is different, but "mekanik" become one value. how to separate it?
in the mechanical column, the first row should be the operator and the second row should be abdullah muis.
pict two : table
here's my code:
this is my input "keluhan" and "mekanik" modal code, i use dynamic_fuild for dynamic form.
enter image description here
<?php
// mengambil data barang dari tabel dengan kode terbesar
$query = mysqli_query($connect, "SELECT max(kode_pg) as kodeTerbesar FROM tb_pg_cs");
$data = mysqli_fetch_array($query);
$kodepg = $data['kodeTerbesar'];
// mengambil angka dari kode barang terbesar, menggunakan fungsi substr dan diubah ke integer dengan (int)
$urutan = (int) substr($kodepg, 2, 7);
// nomor yang diambil akan ditambah 1 untuk menentukan nomor urut berikutnya
$urutan++;
// membuat kode barang baru
// string sprintf("%03s", $urutan); berfungsi untuk membuat string menjadi 3 karakter
// misalnya string sprintf("%03s", 22); maka akan menghasilkan '022'
// angka yang diambil tadi digabungkan dengan kode huruf yang kita inginkan, misalnya PC
$huruf = "SC";
$kodepg1 = $huruf . sprintf("%07s", $urutan);
?>
<?php
include "support/fungsi_romawi.php";
$bulan = date('n');
$romawi = getRomawi($bulan);
// mengambil data barang dari tabel dengan kode terbesar
$query = mysqli_query($connect, "SELECT max(kode_pg) as kodeTerbesar FROM tb_pg_cs");
$data = mysqli_fetch_array($query);
$kodepg = $data['kodeTerbesar'];
// mengambil angka dari kode barang terbesar, menggunakan fungsi substr dan diubah ke integer dengan (int)
$urutan = (int) substr($kodepg, 6, 6);
// nomor yang diambil akan ditambah 1 untuk menentukan nomor urut berikutnya
$urutan++;
// membuat kode barang baru
// string sprintf("%03s", $urutan); berfungsi untuk membuat string menjadi 3 karakter
// misalnya string sprintf("%03s", 22); maka akan menghasilkan '022'
// angka yang diambil tadi digabungkan dengan kode huruf yang kita inginkan, misalnya PC
$tahun = date ('Y');
$nomor = "FORM/SC/".$romawi."/".$tahun."/";
$kode = sprintf("%07s", $urutan);
$noformpg = $nomor. $kode;
?>
<div class="modal fade" id="addnew" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<center><h4 class="modal-title" id="myModalLabel">Tambah Data</h4></center>
</div>
<div class="modal-body">
<div class="container-fluid">
<form method="POST" action="system/add_pg.php" id="form_materi">
<input class="form-control" type="hidden" name="kodepg" value="<?php echo $kodepg1 ?>" required>
<div class="row form-group">
<div class="col-sm-3">
<label class="control-label modal-label">NO. PENGAJUAN</label>
</div>
<div class="col-sm-9">
<input class="form-control" type="text" name="noformpg" value="<?php echo $noformpg ?>" required>
</div>
</div>
<div class="row form-group">
<div class="col-sm-3">
<label class="control-label modal-label">TANGGAL</label>
</div>
<div class="col-sm-9">
<input class="form-control" type="date" name="waktupg" placeholder="Masukkan Hari dan Tanggal Pengajuan" autocomplete="off" required>
</div>
</div>
<div class="row form-group">
<div class="col-sm-3">
<label class="control-label modal-label">DEPARTEMEN</label>
</div>
<div class="col-sm-9">
<select class="form-control selectpicker" type="text" name="deptpg" id="deptpg" data-live-search="true" required>
<option selected value> -- Pilih Departemen -- </option>
<?php
$query = mysqli_query($connect, "SELECT nama_dept FROM tb_dept");
while ($data = mysqli_fetch_array($query)) {
?>
<option value="<?=$data['nama_dept'];?>"><?php echo $data['nama_dept'];?></option>
<?php
}
?>
</select>
</div>
</div>
<div class="row form-group">
<div class="col-sm-3">
<label class="control-label modal-label">LOKASI</label>
</div>
<div class="col-sm-9">
<select class="form-control selectpicker" type="text" name="lokasipg" id="lokasipg" data-live-search="true" required>
<option selected value> -- Pilih Lokasi -- </option>
<option value="JLA 1">JLA 1</option>
<option value="JLA 2">JLA 2</option>
</select>
</div>
</div>
<div class="row form-group">
<div class="col-sm-3">
<label class="control-label modal-label">NAMA MESIN</label>
</div>
<div class="col-sm-9">
<select class="form-control selectpicker" type="text" name="merkpg" id="merkmes" onchange="auto_complete()" data-live-search="true" required>
<option selected value> -- Pilih Mesin -- </option>
<optgroup label="Mesin Forklift">
<?php
$query = mysqli_query($connect, "SELECT * FROM tb_mesin_forklift");
while ($data = mysqli_fetch_array($query)) {
?>
<label><label>
<option value="<?=$data['merk_mesin'];?>"><?php echo $data['merk_mesin'];?> (<?php echo $data['tahun_mesin'];?>)</option>
<?php
}
?>
</optgroup>
<optgroup label="Mesin Produksi">
<?php
$query = mysqli_query($connect, "SELECT * FROM tb_mesin_produksi");
while ($data = mysqli_fetch_array($query)) {
?>
<label><label>
<option value="<?=$data['nama_mesin'];?>"><?php echo $data['nama_mesin'];?> (<?php echo $data['tahun_mesin'];?>)</option>
<?php
}
?>
</optgroup>
</select>
</div>
</div>
<div class="row form-group">
<div class="col-sm-3">
<label class="control-label modal-label">TAHUN</label>
</div>
<div class="col-sm-9">
<input class="form-control" type="text" id="tahunmes" name="tahunpg" placeholder="Masukkan Tahun Mesin" required autocomplete="off">
</div>
</div>
<div class="row form-group">
<div class="col-sm-3">
<label class="control-label modal-label">OPERATOR</label>
</div>
<div class="col-sm-9">
<select class="form-control selectpicker" type="text" name="oppg[]" id="oppg" multiple data-live-search="true" data-none-selected-text="Pilih Operator">
<?php
$query = mysqli_query($connect, "SELECT nama_op FROM tb_operator");
while ($data = mysqli_fetch_array($query)) {
?>
<option value="<?=$data['nama_op'];?>"><?php echo $data['nama_op'];?></option>
<?php
}
?>
</select>
</div>
</div>
<div id="dynamic_field">
<div class="row form-group">
<div class="col-sm-3">
<label class="control-label modal-label">KELUHAN</label>
</div>
<div class="col-sm-7">
<textarea class="form-control" type="text" name="keluhanpg[]" placeholder="Masukkan Keluhan" autocomplete="off" rows="3"></textarea>
</div>
</div>
<div class="row form-group">
<div class="col-sm-3">
<label class="control-label modal-label">MEKANIK</label>
</div>
<div class="col-sm-7">
<select class="form-control selectpicker" type="text" name="mkpg[]" id="mkpg" multiple data-live-search="true" data-none-selected-text="Pilih Mekanik">
<?php
$query = mysqli_query($connect, "SELECT nama_mk FROM tb_mekanik");
while ($data = mysqli_fetch_array($query)) {
?>
<option value="<?=$data['nama_mk'];?>"><?php echo $data['nama_mk'];?></option>
<?php
}
?>
</select>
</div>
<div class="col-sm-2">
<button class="btn btn-success" type="button" name="add" id="add">
<i class="glyphicon glyphicon-plus"></i>
</button>
</div>
</div>
</div>
<div class="row form-group">
<div class="col-sm-3">
<label class="control-label modal-label">SHIFT</label>
</div>
<div class="col-sm-9">
<select class="form-control selectpicker" type="text" name="shiftpg" id="shiftpg" data-live-search="true">
<option selected value> -- Pilih Shift -- </option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</div>
</div>
<input type="hidden" class="form-control" name="statuspg" value="PROGRESS">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Cancel</button>
<button type="submit" name="add" class="btn btn-primary"><span class="glyphicon glyphicon-floppy-disk"></span> Save</a>
</div>
</form>
</div>
</div>
</div>
</div>
I use javascript to be able to add input fields because they are dynamics.
$(document).ready(function(){
var i=1;
$('#add').click(function(){
i++;
$('#dynamic_field').append('<div id="row'+i+'"><div class="row form-group"><div class="col-sm-3"><label class="control-label modal-label">KELUHAN</label></div><div class="col-sm-7"><textarea class="form-control" type="text" name="keluhanpg[]" placeholder="Masukkan Keluhan" autocomplete="off" rows="3" required></textarea></div></div><div class="row form-group"><div class="col-sm-3"><label class="control-label modal-label">MEKANIK</label></div><div class="col-sm-7"><select class="form-control selectpicker" type="text" name="mkpg[]" id="mkpg" multiple data-live-search="true" data-none-selected-text="Pilih Mekanik" required> <?php $query = mysqli_query($connect, "SELECT nama_mk FROM tb_mekanik"); while ($data = mysqli_fetch_array($query)) { ?> <option value="<?=$data['nama_mk'];?>"><?php echo $data['nama_mk'];?></option> <?php } ?></select></div><div class="col-sm-2"><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove"><i class="fa fa fa-trash"></i></button></div></div></div>');
$(".selectpicker").selectpicker('refresh');
});
$(document).on('click', '.btn_remove', function(){
var button_id = $(this).attr("id");
// alert(button_id);
$('#row'+button_id+'').remove();
});
});
and this is the code for my input process.
<?php
session_start();
include_once('koneksi.php');
if(isset($_POST['add'])){
$kodepg = $_POST['kodepg'];
$noformpg = $_POST['noformpg'];
$waktupg = $_POST['waktupg'];
$deptpg = $_POST['deptpg'];
$lokasipg = $_POST['lokasipg'];
$merkpg = $_POST['merkpg'];
$tahunpg = $_POST['tahunpg'];
$op = isset($_POST['oppg']) && is_array($_POST['oppg']) ? $_POST['oppg'] : [];
$oppg = implode(", ", $op);
$keluhanpg = isset($_POST['keluhanpg']) && is_array($_POST['keluhanpg']) ? $_POST['keluhanpg'] : [];
$mk = isset($_POST['mkpg']) && is_array($_POST['mkpg']) ? $_POST['mkpg'] : [];
$mkpg = implode(", ", $mk);
$shiftpg = $_POST['shiftpg'];
$statuspg = $_POST['statuspg'];
foreach ($_POST['keluhanpg'] as $key => $value) {
$sql = "INSERT INTO tb_pg_cs (kode_pg,noform_pg,waktu_pg,dept_pg,lokasi_pg,merk_pg,tahun_pg,op_pg,keluhan_pg,mk_pg,shift_pg,status_pg) VALUES ('" . $kodepg . "','".$noformpg . "','".$waktupg . "','".$deptpg . "','".$lokasipg . "','".$merkpg . "','".$tahunpg . "','".$oppg . "','".$_POST['keluhanpg'][$key] . "','".$mkpg. "','".$shiftpg . "','".$statuspg . "')";
//use for MySQLi OOP
if($connect->query($sql)){
$_SESSION['success'] = 'Data added successfully';
}
else{
$_SESSION['error'] = 'Something went wrong while adding';
}
}
}
header("location: ".$base_url."index.php?page=pengajuan");
?>

Function passing values into input boxes PHP using mysqli

Pretty much what I want to do is get ID from database and pass that ID values into input boxes so I can re-edit them when clicking on edit button
<td><a class= "waves-effect waves-light btn" href="http://localhost/robinzonlucica/public/includes/evidencija.inc.php?edit='.$row['evidencijaId'].'"><span class="fas fa-edit"></span></a></td>
This is my icon href to get Id and pass it to evidencija.inc.php
if(isset($_GET["edit"])){
$id = $_GET["edit"];
require_once $_SERVER['DOCUMENT_ROOT'].'/robinzonlucica/public/includes/dbh.inc.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/robinzonlucica/public/includes/funkcije.inc.php';
izmijeniEvidenciju($conn,$id);
}
else{
header("location: http://localhost/robinzonlucica/public/evidencija.php");
exit();
}
This is my evidencija.inc.php part to get id from that row and there is izmijeniEvidenciju($conn,$id); function to edit values for given edit=id
function izmijeniEvidenciju($conn, $id) {
$sql = "SELECT * FROM evidencija WHERE evidencijaId = ?;";
$stmt = mysqli_stmt_init($conn);
if(!mysqli_stmt_prepare($stmt, $sql)){
header("location: http://localhost/robinzonlucica/public/evidencija.php?error=stmtgreska");
exit();
}
mysqli_stmt_bind_param($stmt, "s", $id);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
}
That is my function to get data I want data to show in input fields not sure where I went wrong I am very confused
if(isset($_SESSION["username"])) {
echo '<h2 class ="center-align evidencijanaslov">Evidencija</h2>
<div class="row">
<form action="http://localhost/robinzonlucica/public/includes/evidencija.inc.php" class="col s12 registerforma" method="post">
<div class="row">
<div class="input-field col s6">
<input id="ime" type="text" class="validate" name="ime">
<label for="last_name">Ime Turista</label>
</div>
</div>
<div class="row">
<div class="input-field col s6">
<input id="ime" type="text" class="validate" name="prezime" >
<label for="last_name">Prezime turista</label>
</div>
</div>
<div class="row">
<div class="input-field col s6">
<input id="ime" type="text" class="validate" name="oib">
<label for="last_name">OIB Turista</label>
</div>
</div>
<div class="row">
<div class="input-field col s6">
<input type=text name="datum" id="datum" class="datepicker">
<label for="datum">Datum odlaska</label>
</div>
</div>
<button class="btn waves-effect waves-light gumbsalji" type="submit" name="submit">Upiši</button>
</div>
</form>';
}
This is html input

Problem with return php to html on second page

Currently I am stuck when i click edit icon my page goes to
http://localhost/robinzonlucica/public/includes/evidencija.inc.php?edit=3
It should insert data into my form
if(isset($_GET["edit"])){
$id = $_GET["edit"];
require_once $_SERVER['DOCUMENT_ROOT'].'/robinzonlucica/public/includes/dbh.inc.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/robinzonlucica/public/includes/funkcije.inc.php';
izmijeniEvidenciju($conn,$id);
}
This form should call function izmijeniEvidenciju($conn,$id);
This is function code
function izmijeniEvidenciju($conn, $id) {
$sql = "SELECT * FROM evidencija WHERE evidencijaId = ?;";
$stmt = mysqli_stmt_init($conn);
if(!mysqli_stmt_prepare($stmt, $sql)){
header("location: http://localhost/robinzonlucica/public/evidencija.php?error=stmtgreska");
exit();
}
mysqli_stmt_bind_param($stmt, "s", $id);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
return $row;
mysqli_stmt_close($stmt);
}
And this is my html part form that values should be written in after clicking edit but when i click icon to edit it goes on blank page and stays like that
<?php
include_once 'header.php';
require_once 'includes/funkcije.inc.php';
require_once 'includes/dbh.inc.php';
?>
<section class="evidencija">
<?php
if(isset($_SESSION["username"])) {
$editrow = izmijeniEvidenciju($conn, $id);
echo '<h2 class ="center-align evidencijanaslov">Evidencija</h2>
<div class="row">
<form action="http://localhost/robinzonlucica/public/includes/evidencija.inc.php" class="col s12 registerforma" method="post">
<div class="row">
<div class="input-field col s6">
<input id="ime" type="text" class="validate" name="ime" value="'.$editrow['evidencijaTuristIme'].'">
<label for="last_name">Ime Turista</label>
</div>
</div>
<div class="row">
<div class="input-field col s6">
<input id="ime" type="text" class="validate" name="prezime" value="'.$editrow['evidencijaTuristPrezime'].' ">
<label for="last_name">Prezime turista</label>
</div>
</div>
<div class="row">
<div class="input-field col s6">
<input id="ime" type="text" class="validate" name="oib" value="'.$editrow['evidencijaTuristOIB'].'">
<label for="last_name">OIB Turista</label>
</div>
</div>
<div class="row">
<div class="input-field col s6">
<input type=text name="datum" id="datum" class="datepicker" value="'.$editrow['evidencijaTuristDatumOdlaska'].'">
<label for="datum">Datum odlaska</label>
</div>
</div>
<button class="btn waves-effect waves-light gumbsalji" type="submit" name="submit">Upiši</button>
</div>
</form>';
}
else {
echo '<h1>Prijavite se kako bi vidjeli evidenciju</h1>';
}
I am getting that $id variable is not declared too don't know where I went wrong PS hardcoding $id with id from table inputs values into form :/

Mysql and PHP Update Problems

I am working on a simple Userprofile, where the user can update some data over Mysql. But it doesn´t update anything. Why?
<?php
if (isset($_GET['pageID'])) {
$id = $_GET['pageID'];
include("connector.php");
$stmt = $conn->prepare(" SELECT
cl.clientID, cl.firstname, cl.lastname, cl.mobilephone, cl.phone, cl.client_ID,
ad.street, ad.city, ad.postalcode,
wo.work_ID,
ha.hardware1, ha.serialnumber, ha.hardware2, ha.serialnumber2
FROM client cl
JOIN adress ad ON cl.clientID = ad.clientID
LEFT JOIN work wo ON cl.clientID = wo.clientID
LEFT JOIN hardware ha ON ad.adressID = ha.adressID
WHERE cl.clientID = '$id'");
$stmt->execute();
}
?>
<?php while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) : ?>
<div class="container">
<div class="col-sm-12 col-md-12 col-xl-12">
<div class="col-sm-3 col-md-3 col-xl-3 profileNames">
<img src="Userfotos/jedi.jpg" class="img-responsive"><br>
<button type="submit" name="submit" class="btn btn-info">edit Foto</button>
</div>
<div class="col-sm-3 col-md-3 col-xl-3 profileNames">
<p><h4>Lastname:</h4><?php echo $row['lastname']; ?></p>
<p><h4>Firstname:</h4> <?php echo $row['firstname']; ?></p>
<p><h4>Street</h4> <?php echo $row['street']; ?></p>
<p><h4>City</h4> <?php echo $row['city']; ?></p>
<p><h4>Postalcode</h4> <?php echo $row['postalcode']; ?></p>
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#editPrivateData">Edit data
</button>
<div id="editPrivateData" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Edit Personal data</h4>
</div>
<div class="modal-body">
<form action="userProfile.php?pageID=1" method="post">
<div class="form-group">
<label for="_firstname">Firstname</label>
<input type="text" class="form-control" id="_firstname" name="firstname"
value="<?php echo $row['firstname']; ?>">
</div>
<div class="form-group">
<label for="_lastname">Lastname</label>
<input type="text" class="form-control" id="_lastname" name="lastname"
value="<?php echo $row['lastname']; ?>">
</div>
<div class="form-group">
<label for="_street">Street</label>
<input type="text" class="form-control" id="_street" name="street"
value="<?php echo $row['street']; ?>">
</div>
<div class="form-group">
<label for="_city">City</label>
<input type="text" class="form-control" id="_city" name="city"
value="<?php echo $row['city']; ?>">
</div>
<div class="form-group">
<label for="_postalcode">Postalcode</label>
<input type="text" class="form-control" id="_postalcode" name="postalcode"
value="<?php echo $row['postalcode']; ?>">
</div>
<button type="submit" name="submitPrivate" class="btn btn-info">Save</button>
<?php
if (isset($_POST['submitHardware'])) {
try {
$id = $_GET['pageID'];
$stmt = $conn->prepare("UPDATE clients.client SET
firstname = :firstname,
lastname = :lastname,
mobilephone = :mobilephone,
phone = :phone
WHERE clientID = $id");
$stmt->bindParam(':firstname', $firstname);
$stmt->bindParam(':lastname', $lastname);
$stmt->bindParam(':mobilephone', $mobilephone);
$stmt->bindParam(':phone', $phone);
$stmt->execute([':firstname' => $_POST['firstname'],
':lastname' => $_POST['lastname'],
':mobilephone' => $_POST['mobilephone'],
':phone' => $_POST['phone']
]);
} catch (PDOException $e) {
echo $e->getMessage() . ", ";
die("try to fix your inputs . If this doesn`t work, please contact your Admin.");
}
}
?>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-info" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
<div class="col-sm-3 col-md-3 col-xl-3 profileNames">
<p><h4>Hardware1:</h4><?php echo $row['hardware1']; ?></p>
<p><h4>Serialnumber1:</h4><?php echo $row['serialnumber']; ?></p>
<p><h4>Hardware2:</h4><?php echo $row['hardware2']; ?></p>
<p><h4>Serialnumber2:</h4><?php echo $row['serialnumber2']; ?></p>
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#editHardware">Edit data
</button>
<!-- Modal -->
<div id="editHardware" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Edit hardware data</h4>
</div>
<div class="modal-body">
<form action="userProfile.php?pageID=3" method="post">
<div class="form-group">
<label for="_hardware1">Hardware1</label>
<input type="text" class="form-control" id="_hardware" name="hardware1"
placeholder="<?php echo $row['hardware1']; ?>">
</div>
<div class="form-group">
<label for="serialnumber">Serialnumber1</label>
<input type="text" class="form-control" id="_serialnumber" name="serialnumber"
placeholder="<?php echo $row['serialnumber']; ?>">
</div>
<div class="form-group">
<label for="_hardware2">Hardware2</label>
<input type="text" class="form-control" id="_hardware2" name="hardware2"
placeholder="<?php echo $row['hardware2']; ?>">
</div>
<div class="form-group">
<label for="_serialnumber2">Serialnumber2</label>
<input type="text" class="form-control" id="_serialnumber2" name="serialnumber2"
placeholder="<?php echo $row['serialnumber2']; ?>">
</div>
<button type="submit" name="submitHardware" class="btn btn-info">Save</button>
<?php
if (isset($_POST['submitHardware'])) {
require("connector.php");
try {
$stmt = $conn->prepare("UPDATE clients.hardware SET
hardware1 = :hardware1,
serialnumber = :serialnumber,
hardware2 = :hardware2,
serialnumber2 = :serialnumber2,
WHERE hardware.clientID = '$id'");
$stmt->bindParam(':firstname', $firstname);
$stmt->bindParam(':lastname', $lastname);
$stmt->bindParam(':mobilephone', $mobilephone);
$stmt->bindParam(':phone', $phone);
$stmt->bindParam(':client_ID', $client_ID);
$stmt->execute([':hardware1' => $_POST['hardware1'],
':serialnumber' => $_POST['serialnumber'],
':hardware2' => $_POST['hardware2'],
':serialnumber2' => $_POST['seerialnumber2']
]);
} catch (PDOException $e) {
echo $e->getMessage() . ", ";
die("try to fix your inputs . If this doesn`t work, please contact your Admin.");
}
}
?>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-info" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
<div class="col-sm-3 col-md-3 col-xl-3 profileNames">
<p><h4>ClientID:</h4><?php echo $row['client_ID']; ?></p>
<p><h4>WorkID:</h4><?php echo $row['work_ID']; ?></p>
<button type="submit" name="submit" class="btn btn-info">Edit data</button>
</div>
</div>
<?php endwhile; ?>
As you can see, there is a while-loop to fetch the data and display them into some fields. In Between this loop, i created two Bootstrap-Modals, where the user can edit something. if the user clicks on the "save" button, nothing happens. i really don`t understand where is the problem.
and another small point: the userprofile is generated dynamically with the clientID(pageID). how do i temporary save this pageID, so i can return to the current page after the form action method ="POST"
The clientID is the primary key in the database for the clients.client table. The client_ID is an ID the user has to set by himself, he can set whatever he want to. The PageID is linked to the clientID is for generating dynamical pages. That means: on another page there is a table which displays some basically informations about all users. If the logged in User clicks on a user from the table, there will be the userprofile dynamically generated.
your code is really weird. Please tell us what the page ID and client ID are. They are obviously not the same. But you use them as such. I reduced your code to just one modal and used hidden fields for client and page ID values.
<?php
// TODO: filter all input
if (isset($_POST['submitHardware']) && $_POST['pageID'] == 3) {
try {
include("connector.php");
$client_id = $_GET['clientID'];
$stmt = $conn->prepare("UPDATE clients.client SET
firstname = :firstname,
lastname = :lastname,
mobilephone = :mobilephone,
phone = :phone
WHERE clientID = $client_id");
$stmt->bindParam(':firstname', $firstname);
$stmt->bindParam(':lastname', $lastname);
$stmt->bindParam(':mobilephone', $mobilephone);
$stmt->bindParam(':phone', $phone);
$stmt->execute([':firstname' => $_POST['firstname'],
':lastname' => $_POST['lastname'],
':mobilephone' => $_POST['mobilephone'],
':phone' => $_POST['phone']
]);
} catch (PDOException $e) {
echo $e->getMessage() . ", ";
die("try to fix your inputs . If this doesn`t work, please contact your Admin.");
}
}
if (isset($_GET['clientID'])) {
$client_id = $_GET['clientID']; // TODO: filter
include("connector.php");
$stmt = $conn->prepare(" SELECT
cl.clientID, cl.firstname, cl.lastname, cl.mobilephone, cl.phone, cl.client_ID,
ad.street, ad.city, ad.postalcode,
wo.work_ID,
ha.hardware1, ha.serialnumber, ha.hardware2, ha.serialnumber2
FROM client cl
JOIN adress ad ON cl.clientID = ad.clientID
LEFT JOIN work wo ON cl.clientID = wo.clientID
LEFT JOIN hardware ha ON ad.adressID = ha.adressID
WHERE cl.clientID = '$client_id'");
$stmt->execute();
}
?>
<?php while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) : ?>
<div class="container">
<div class="col-sm-12 col-md-12 col-xl-12">
<div class="col-sm-3 col-md-3 col-xl-3 profileNames">
<img src="Userfotos/jedi.jpg" class="img-responsive"><br>
<button type="submit" name="submit" class="btn btn-info">edit Foto</button>
</div>
<div class="col-sm-3 col-md-3 col-xl-3 profileNames">
<p><h4>Hardware1:</h4><?php echo $row['hardware1']; ?></p>
<p><h4>Serialnumber1:</h4><?php echo $row['serialnumber']; ?></p>
<p><h4>Hardware2:</h4><?php echo $row['hardware2']; ?></p>
<p><h4>Serialnumber2:</h4><?php echo $row['serialnumber2']; ?></p>
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#editHardware">Edit data
</button>
<!-- Modal -->
<div id="editHardware" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Edit hardware data</h4>
</div>
<div class="modal-body">
<form action="userProfile.php" method="post">
<input type="hidden" name="pageID" value="3">
<input type="hidden" name="clientID" value="<?php echo $client_id; ?>">
<div class="form-group">
<label for="_hardware1">Hardware1</label>
<input type="text" class="form-control" id="_hardware" name="hardware1"
placeholder="<?php echo $row['hardware1']; ?>">
</div>
<div class="form-group">
<label for="serialnumber">Serialnumber1</label>
<input type="text" class="form-control" id="_serialnumber" name="serialnumber"
placeholder="<?php echo $row['serialnumber']; ?>">
</div>
<div class="form-group">
<label for="_hardware2">Hardware2</label>
<input type="text" class="form-control" id="_hardware2" name="hardware2"
placeholder="<?php echo $row['hardware2']; ?>">
</div>
<div class="form-group">
<label for="_serialnumber2">Serialnumber2</label>
<input type="text" class="form-control" id="_serialnumber2" name="serialnumber2"
placeholder="<?php echo $row['serialnumber2']; ?>">
</div>
<button type="submit" name="submitHardware" class="btn btn-info">Save</button>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-info" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
</div>
<?php endwhile; ?>

insert multiple arrays to database

I am creating a multiple choice quiz and I don't know how to insert multiple arrays to database. I can insert the first 2 arrays using array_combine but cannot on the 3rd, 4th, 5th, and 6th array. I can insert questions and answers but not the options A, B, C, and D.
I'm trying to do something like
foreach(array_combine($_POST['inQuestion'], $_POST['inAnswer'], $_POST['inA'], $_POST['inB'], $_POST['inC'], $_POST['inD']) as $question => $answer => $A => $B => $C => $D) {
Please help. Here is my code.
<?php
if(isset($_POST['btnCreate'])) {
$inQuestion = array($_POST['inQuestion']);
$inAnswer = array($_POST['inAnswer']);
$inA = array($_POST['inA']);
$inB = array($_POST['inB']);
$inC = array($_POST['inC']);
$inD = array($_POST['inD']);
$inLesson = $_POST['inLesson'];
$inQuizNo = $_POST['inQuizNo'];
$sql = "SELECT * FROM lessons WHERE title='$inLesson'";
$query = mysql_query ($sql);
$row = mysql_fetch_assoc($query);
$lessonID = $row['lessonID'];
foreach(array_combine($_POST['inQuestion'], $_POST['inAnswer']) as $question => $answer) {
$sql = "INSERT INTO `test` (question, answer, A, B, C, D, lessonID, quizNo) VALUES ('$question', '$answer', '$A', '$B', '$C', '$D', '$lessonID', $inQuizNo)";
$query = mysql_query( $sql );
}
}
?>
<div class="panel panel-info">
<div class="panel-heading">
<h4>Create Assessment - Multiple Choice</h4>
</div>
<div class="panel-body">
<form method="post">
<br>
<div>
<div class="col-md-4">
<strong>Lesson</strong><br>
<select class="form-control" name="inLesson">
<option></option>
<option disabled></option>
<?php
$sql2 = "SELECT * FROM lessons WHERE courseID='$_GET[courseID]'";
$query2 = mysql_query ($sql2);
while ($row2 = mysql_fetch_assoc($query2)) {
?>
<option><?php echo $row2['title']; ?></option>
<?php } ?>
</select>
</div>
<div class="col-md-4">
<strong>Quiz No.</strong><br>
<input required type="number" class="form-control" name="inQuizNo" min="1" max="100">
</div>
<div class="col-md-4">
<div class="pull-right">
<br>
<input type="button" value="Add" id="addButton" class="btn btn-info">
<input type="button" value="Remove" id="removeButton" class="btn btn-warning">
</div>
</div>
</div>
<br><br><br><br><br>
<div id='TextBoxesGroup'>
<div id="TextBoxDiv1" class="form-group">
<h2><span class="label label-primary">No. 1</span></h2>
<br><br>
<div class="col-md-10">
<label>Question</label><input type='text' id='textbox1' name='inQuestion[]' class="form-control">
</div>
<div class="col-md-2">
<label>Answer</label><input type='text' id='textbox1' name='inAnswer[]' class="form-control">
<br>
</div>
<div class="col-md-3">
<label>A</label><input type='text' id='textbox1' name='inA[]' class="form-control">
</div>
<div class="col-md-3">
<label>B</label><input type='text' id='textbox1' name='inB[]' class="form-control">
</div>
<div class="col-md-3">
<label>C</label><input type='text' id='textbox1' name='inC[]' class="form-control">
</div>
<div class="col-md-3">
<label>D</label><input type='text' id='textbox1' name='inD[]' class="form-control">
<br><br>
</div>
</div>
</div>
<div class="col-md-12">
<button type="submit" name="btnCreate" class="btn btn-success pull-right">Create Quiz
</div>
</form>
</div>
From what I understood, you are trying to insert multible rows INTO your database.
First, when you use the [] in HTML forms you don't have to declare it as an array in php
Second, each question will have a question name, answer, A,B,C,D
When you trying to create the HTML form you could use this format to keep track the question_id or number.
<label>Question</label><input type='text' id='textbox1' name='inQuestion[1]' class="form-control">
Then you will be able to retrieve this item as $_POST['inQuestion'][1]
For Example, if you need 10 questions in your test page you can do it like this:
<?php for($i = 0; $i <= 10; $i++) : ?>
<form method="post">
<div class="col-md-10">
<label>Question</label><input type='text' id='textbox1' name='inQuestion[<?=$i;?>]' class="form-control">
</div>
<div class="col-md-2">
<label>Answer</label><input type='text' id='textbox1' name='inAnswer[<?=$i;?>]' class="form-control">
<br>
</div>
<div class="col-md-3">
<label>A</label><input type='text' id='textbox1' name='inA[<?=$i;?>]' class="form-control">
</div>
<div class="col-md-3">
<label>B</label><input type='text' id='textbox1' name='inB[<?=$i;?>]' class="form-control">
</div>
<div class="col-md-3">
<label>C</label><input type='text' id='textbox1' name='inC[<?=$i;?>]' class="form-control">
</div>
<div class="col-md-3">
<label>D</label><input type='text' id='textbox1' name='inD[<?=$i;?>]' class="form-control">
<br><br>
</div>
<input type="submit" value="go">
</form>
<?php endfor; ?>
Also, why do you put the content of the Answers A,B,C,D to an array while you have them is columns in your database. Also use these variables in your query too.
$inA = $_POST['inA'];
$inB = $_POST['inB'];
$inC = $_POST['inC'];
$inD = $_POST['inD'];
Finally Let's try to insert your values now.
if($_POST['btnCreate']){
// The form has been posted!
$inLesson = $_POST['inLesson'];
$inQuizNo = $_POST['inQuizNo'];
$questionsCount = count($_POST['inQuestion']);
$items = array();
// Since we should have the same size of arrays.
// Also the empty validation has been handled before this step!
for($i = 0; $i < $questionsCount; $i++){
$temp = array();
$temp['inQuestion'] = $_POST['inQuestion'][$i];
$temp['inAnswer'] = $_POST['inAnswer'][$i];
$temp['inA'] = $_POST['inA'][$i];
$temp['inB'] = $_POST['inB'][$i];
$temp['inC'] = $_POST['inC'][$i];
$temp['inD'] = $_POST['inD'][$i];
$items[] = $temp;
}
// Now items should have an array of all questions.
foreach($items as $item){
$SQL = 'INSERT INTO `test` (question, answer, A, B, C, D, lessonID, quizNo) VALUES (
"'.$item['inQuestion'].'",
"'.$item['inAnswer'].'",
"'.$item['inA'].'",
"'.$item['inB'].'",
"'.$item['inC'].'",
"'.$item['inD'].'",
"'.$inLesson.'",
"'.$inQuizNo.'")';
$query = mysql_query( $SQL );
} // End Foreach
}
Try This
<?php
if(isset($_POST['btnCreate'])) {
$inQuestion = array($_POST['inQuestion']);
$inAnswer = array($_POST['inAnswer']);
$inA = $_POST['inA'] ? $_POST['inA'] : null;
$inB = $_POST['inB'] ? $_POST['inB'] : null;
$inC = $_POST['inC'] ? $_POST['inC'] : null;
$inD = $_POST['inD'] ? $_POST['inD'] : null;
$inLesson = $_POST['inLesson'];
$inQuizNo = $_POST['inQuizNo'];
$sql = "SELECT * FROM lessons WHERE title='$inLesson'";
$query = mysql_query ($sql);
$row = mysql_fetch_assoc($query);
$lessonID = $row['lessonID'];
$sql = "INSERT INTO `test` (question, answer, A, B, C, D, lessonID, quizNo) VALUES ('$question', '$answer', '$inA', '$inB', '$inC', '$inD', '$lessonID', $inQuizNo)";
$query = mysql_query( $sql );
}
?>
<div class="panel panel-info">
<div class="panel-heading">
<h4>Create Assessment - Multiple Choice</h4>
</div>
<div class="panel-body">
<form method="post">
<br>
<div>
<div class="col-md-4">
<strong>Lesson</strong><br>
<select class="form-control" name="inLesson">
<option></option>
<option disabled></option>
<?php
$sql2 = "SELECT * FROM lessons WHERE courseID='$_GET[courseID]'";
$query2 = mysql_query ($sql2);
while ($row2 = mysql_fetch_assoc($query2)) {
?>
<option><?php echo $row2['title']; ?></option>
<?php } ?>
</select>
</div>
<div class="col-md-4">
<strong>Quiz No.</strong><br>
<input required type="number" class="form-control" name="inQuizNo" min="1" max="100">
</div>
<div class="col-md-4">
<div class="pull-right">
<br>
<input type="button" value="Add" id="addButton" class="btn btn-info">
<input type="button" value="Remove" id="removeButton" class="btn btn-warning">
</div>
</div>
</div>
<br><br><br><br><br>
<div id='TextBoxesGroup'>
<div id="TextBoxDiv1" class="form-group">
<h2><span class="label label-primary">No. 1</span></h2>
<br><br>
<div class="col-md-10">
<label>Question</label><input type='text' id='textbox1' name='inQuestion[]' class="form-control">
</div>
<div class="col-md-2">
<label>Answer</label><input type='text' id='textbox1' name='inAnswer[]' class="form-control">
<br>
</div>
<div class="col-md-3">
<label>A</label><input type='text' id='textbox1' name='inA' class="form-control">
</div>
<div class="col-md-3">
<label>B</label><input type='text' id='textbox1' name='inB' class="form-control">
</div>
<div class="col-md-3">
<label>C</label><input type='text' id='textbox1' name='inC' class="form-control">
</div>
<div class="col-md-3">
<label>D</label><input type='text' id='textbox1' name='inD' class="form-control">
<br><br>
</div>
</div>
</div>
<div class="col-md-12">
<button type="submit" name="btnCreate" class="btn btn-success pull-right">Create Quiz
</div>
</form>
</div>
rather than posting a detailed solution, play with how to insert multiple rows to get some ideas
Something like:
$_POST['inQuestion'] = array(
'q1','q2','q3' // from input array name 'inQuestion[]'
);
$_POST['inAnswer'] = array(
'a1','a2','a3' // from input array name 'inAnswer[]'
);
$_POST['other'] = 'sunshine';
$_POST['yet_another'] = 'nice weather';
foreach($_POST['inQuestion'] as $k => $v){
$inserts[] = "(".$v.",".$_POST['inAnswer'][$k].",".$_POST['other'].",".$_POST['yet_another'].")";
}
echo "INSERT INTO table col_1, col_2, col_3, col_4 VALUES ".implode(',',$inserts)."";
// INSERT INTO table col_1, col_2, col_3, col_4 VALUES (q1,a1,sunshine,nice weather),(q2,a2,sunshine,nice weather),(q3,a3,sunshine,nice weather)

Categories