I'm making a php search form that the user will type their id and if has match it will re-insert the data to database.
I manage get the data and display the value under my submit button.
My problem now is how to insert data after search. Is it possible to re-insert the data in same table if has a match?
Here is my code:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js">
</script>
<a class="btn btn-outline-info" href="#exampleModal" data-toggle="modal" data-target="#exampleModal" target="_blank" role="button">Time-in<i class="fa fa-sign-in ml-2"></i>
</a>
<?php print $output; ?>
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true" data-backdrop="false">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header blue accent-1">
<h5 class="modal-title" style="color:white;" id="exampleModalLabel">Login</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<center>
<h5>
<p> Click Continue </p>
</h5>
</center>
</div>
<div class="modal-footer">
<a href="test.html" class="btn" data-dismiss="modal" style="margin-right:220px; color:#fff; background:#00bcd4" href="#exampleModal2" data-toggle="modal" data-target="#exampleModal2" target="_blank" role="button">Yes</a>
</div>
</div>
</div>
</div>
<div class="modal fade " id="exampleModal2" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true" data-backdrop="false">
<div class="modal-dialog modal-dialog-centered " role="document">
<div class="modal-content">
<div class="modal-header blue accent-1">
<h3 class="mt-2" style="color:white;"><i class="fa fa-user-circle"></i> Confirm Identity:</h3>
<a href="sample.php" button type="btn" class="close" aria-label="Close">
<span aria-hidden="true">×</span>
</a>
</div>
<div class="modal-body">
<form method="post">
<div class="md-form input-group">
<input type="search" class="form-control added-padding-2" placeholder="Search for ID..." name="search" onKeyPress="return isNumberKey(event)" maxlength="11" id="textsend" onKeyUp="success()">
<span class="input-group-btn">
<button class="btn btn-outline-info waves-effect my-0" type="submit" href="#exampleModal3" data-toggle="modal" data-target="#exampleModal3" target="_blank" role="button" id="button" disabled>Time-in</button>
</span>
</div>
</form>
</div>
</div>
</div>
</div>
<?php
include_once('connection.php');
$output = '';
if(isset($_POST['search'])){
$searchq = $_POST['search'];
$sql = "SELECT id, fname, lname FROM test WHERE id LIKE '$searchq'" ;
$result = mysqli_query($conn, $sql);
$count = mysqli_num_rows($result);
if($count == 0){
$output ='No results!';
}else{
while($row = mysqli_fetch_array($result))
{
$fname = $row['fname'];
$lname = $row['lname'];
$id = $row['id'];
$output .= '<div>'.$fname.''.$lname.'</div>';
}
}
}
?>
function success() {
if(document.getElementById("textsend").value==="") {
document.getElementById('button').disabled = true;
} else {
document.getElementById('button').disabled = false;
}
}
function isNumberKey(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
Database:
Just add the Insert query at the end of else loop, as below and try:
<?php
include_once('connection.php');
$output = '';
if(isset($_POST['search'])) {
$searchq = $_POST['search'];
$sql = "SELECT id, fname, lname FROM test WHERE id LIKE '$searchq'" ;
$result = mysqli_query($conn, $sql);
$count = mysqli_num_rows($result);
if ($count == 0) {
$output ='No results!';
} else {
while ($row = mysqli_fetch_array($result)) {
$fname = $row['fname'];
$lname = $row['lname'];
$id = $row['id'];
$output .= '<div>'.$fname.''.$lname.'</div>';
$InsertSql = "INSERT INTO test(fname,lname,id) VALUES ('$fname','$lname','$id')";
$res = mysqli_query($conn, $InsertSql);
}
}
}
?>
Here you go
include_once('connection.php');
$output = '';
if (isset($_POST['search'])) {
$searchq = $_POST['search'];
$sql = "SELECT id, fname, lname FROM test WHERE id LIKE '$searchq'" ;
$result = mysqli_query($conn, $sql);
$count = mysqli_num_rows($result);
if ($count == 0) {
$output ='No results!';
} else {
while ($row = mysqli_fetch_array($result)) {
$fname = $row['fname'];
$lname = $row['lname'];
$id = $row['id'];
$output .= '<div>'.$fname.''.$lname.'</div>';
$id1 = $row['id']
$sql1 = "UPDATE test SET col_name=value WHERE id='$id1'";
if ($conn->query($sql1) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql1 . "<br>" . $conn->error;
}
}
}
}
Remove quotation from $id1
if it's an integer.