drfu80954
drfu80954
2018-07-25 10:04

获取数据到数据库并重新插入(如果匹配)

  • php
  • mysql
  • javascript
  • jquery

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">&times;</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">&times;</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:

enter image description here

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答