检查wp数据库中是否已经存在数据

时间:2020-12-29 07:14:19

标签: html css wordpress

如果数据已存在于数据库中,但它似乎不起作用,我正尝试以红色显示一条消息。这是我正在使用的代码。

PHP 代码:

<?php
/**
 * Template Name: Add IP Address
 */
get_header(); 
?>
<?php

$sql="SELECT * FROM `wp_5wbtdj_ip_based_login` where (start='$start' or end='$end');";

      $res=mysqli_query($mysqli,$sql);

      if (mysqli_num_rows($res) > 0) {
        
        $row = mysqli_fetch_assoc($res);
        if($end==isset($row['end']))
        {
                echo " End IP already exists";
        }
        if($start==isset($row['start']))
        {
            echo "Start IP already exists";
        }
        }
else{
    
//do your insert code here or do something (run your code)
}

If($_POST['Submit']){
            
    global $wpdb;
    
            
    $username = $_POST['username'];
    $start = ip2long($_POST['start']);
    $end = ip2long($_POST['end']);
If($wpdb ->insert('wp_5wbtdj_ip_based_login', 
         array(
            'username' => $username,
            'start' => $start,
            'end' => $end
)
)== false) wp_die ('<div class="errormessage">Database insertion failed </div>');

else echo '<div class="successmessage"> Database insertion successfull </div>';

?>

<?php
}

else{
}

?>

HTML 代码:

<main role="main">
 <div class="section section_white section_nopadding_top">
  <div class="container">
   <div class="row">
    <div class="col-md-12">
    <br><br>
     <div class="wrap">
      <form action="" id="postjob" method="post">
       <h3 style="margin-left:50px;">IP Based Login</h3>
       <hr />
        <h4 style="margin-left:50px;">Add IP Range</h4>
        <table class="form-table" style="margin-left:50px;">
         <tr>
          <th scope="row" valign="top">
           <label for="username">Username</label><br />
          </th>
         <td>
         &nbsp;&nbsp;&nbsp;&nbsp; 
          <input type="text" size="25" name="username" value="<?php echo do_shortcode('[show_loggedin_as]'); ?>"><br /><br>
         </td>
        </tr>
        <tr>
         <div class="wrapper">   
          <th scope="row" valign="top">
           <label for="start_ip">Start IP </label>
          </th>
          <td>
          &nbsp;&nbsp;&nbsp;&nbsp; 
          <input type="text" minlength="7" maxlength="15" size="25" pattern="^((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$" name="start" valign="top" required>
         </div>
         <br><br>
        <tr>
         <div class="wrapper">   
          <th scope="row" valign="top">
           <label for="start_ip">End IP</label>
          </th>
          <td>
          &nbsp;&nbsp;&nbsp;&nbsp; 
            <input type="text" minlength="7" maxlength="15" size="25" pattern="^((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$" name="end" valign="top" required>
         </div>
          </td>
        </tr> 
        <br>
        </table><br />
<input name="Submit" class="button" value="Add IP range" type="Submit" style="margin-left:50px;"  />    
</form>
    </div>
<br /><br />
               </div> <!-- /col-md-12 -->
               
            </div> <!-- /row -->
            
         </div><!-- /.container -->
      </div> <!-- /section -->
   </main><!-- /.container -->

</div>
<?php
get_footer();

谁能帮忙指出我在这里做错了什么?

0 个答案:

没有答案