Postingan lainnya
Kelas Premium!
Belajar Javascript untuk website
Gunakan kupon "mulaijs" untuk diskon 75 ribu!
1 Input text dengan fungsi query, insert dan update dgn inputan no id karyawan
Salam kenal,
Saya baru di php programming, saya lagi buat project untuk query, insert dan update data dgn inputan text.
inputan text tersebut menggunakan barcode yg berupa no id karyawan.
semua data-data karyawan ada di dalam table ms_employee.
jadi ketika karyawan scan id card yg ada barcode maka akan melakukan query pada database ms_employee (master database karyawan), setelah itu data2 yg berhasil di query berdasarkan no id karyawan maka di insert ke database towel.
untuk saat ini program php mysql nya berhasil untuk query dan insert data - data karyawan.
saya ingin menambahkan proses update, tetapi dgn input text yg sama dgn query. bagaimna cara supaya bisa juga proses update dgn 1 input text yg sama query.
untuk update hanya field date_out, time_out, qty, qty_towel.
berikut script saya
<div class="form-row">
<div class="form-group col-md-6 input-group-lg">
<label for="idno">Scan ID </label>
<p></p>
<input class="form-control idn mb-5" type="text" name="idno" onmouseover="this.focus();" autofocus="idno" id="idno" placeholder="Scan Employee ID Required" required>
</div>
</div>
<div class="form-button" hidden>
<button id="submit" type="submit" class="ibtn" onClick="refreshPage()" >Login</button>
</div>
<script type="text/javascript">
$(document).ready(function() {
var input = document.getElementById("idno");
$('.idn').focus();
input.addEventListener("keyup", function(event) {
event.preventDefault();
if (event.which === 13 || event.keyCode === 13) {
var idno = document.getElementById('idno').value;
var dataString = "idno=" + idno;
$.ajax({
type: "GET",
url: "get.php",
contentType: "application/json; charset=utf-8",
headers: {
"X-My-Custom-Header": "some value",
"X-Requested-With": "XMLHttpRequest"
},
data: dataString,
dataType: 'json',
cache: false,
success: function(data) {
//console.log(data);
var b = $("#idno").focus().val('');
var txtBox = document.getElementById("idno");
$('.idn').focus();
$("#namea").text(data[0]['name']);
$("#idnom").text(data[0]['id_no']);
$("#departement").text(data[0]['departement']);
$("#division").text(data[0]['division']);
$("#property").text(data[0]['property']);
$("#property").text(data[0]['property']);
$("#date_in").text(data[0]['date_in']);
$("#time_in").text(data[0]['time_in']);
b.focus();
}
});
}
});
});
</script>
<?php
header('Content-Type: application/json');
$hostname = "localhost";
$username = "root";
$password = "";
$dbname = "db";
//connection string with database
$con=mysqli_connect($hostname,$username,$password,$dbname);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$idno = $_GET['idno'];
$date_in = gmdate("d-m-Y", time()+60*60*8);
$time_in = gmdate("H:i:s", time()+60*60*8);
$myArray = array();
if ($result = $con->query("SELECT * FROM ms_employees where idno = '$idno'")) {
if(mysqli_num_rows($result)!='0'){
while($row = $result->fetch_assoc()) {
//$myArray[] = $row;
$name = $row["name_e"];
$idno = $row["idno"];
$departement = $row["departement_e"];
$division = $row["division_e"];
$property = $row["property_e"];
}
// attempt insert query execution
$sql = "INSERT INTO towel (name, id_no, departement, division, property, date_in, time_in, qty, qty_towel, status)
VALUES ('$name', '$idno', '$departement', '$division', '$property','$date_in','$time_in','1','$qty_towel','$status')";
mysqli_query($con, $sql);
}
}
if ($result = $con->query("SELECT * FROM towel where id_no = '$idno' ORDER BY id DESC LIMIT 1")) {
while($row = $result->fetch_assoc()) {
$myArray[] = $row;
$name = $row["name"];
$idno = $row["id_no"];
$departement = $row["departement"];
$division = $row["division"];
$property = $row["property"];
$date_in = $row["date_in"];
$time_in = $row["time_in"];
}
echo json_encode($myArray,TRUE);
}
?>
Terimakasih atas informasi dan bantuannya.
1 Jawaban:
<div>Hai<br><br>Kalau ingin mengupdate sekaligus, <br>berarti setelah melakukan</div><pre>INSERT INTO towel... </pre><div><br>Kamu tinggal menambahkan query update yang kamu mau disana</div>