繁体   English   中英

如何在PHP中检查表是否为空

[英]How to check table for empty in php

这是我的php代码,用于检查用户名是否存在。

$link = mysql_connect($server,$user,$pass);
mysql_select_db($db);
$response = 'no';
if(isset($_POST['username']) && trim($_POST['username']) != ''){
 $query = mysql_query("SELECT * FROM prochatrooms_users WHERE username = '".mysql_escape_string(trim($_POST['username']))."'", $link);
 if(mysql_num_rows($query) > 0){
  $response = 'yes';
 }
}
echo json_encode(array('exists' => $response));

这是javascript

$.customPOST = function(data,callback){
    $.post('templates/default/check_username.php',data,callback,'json');
}
//when typing, the script checks if the username exists
function checkUsername() {
  $.customPOST({username: $('#username').val()},function(r){
    //username exists
    if(r.exists == 'yes'){
     $( "#lay_pw" ).toggle(400,function(){});
    }
    else{
     $( "#lay_pw" ).css("display", "none");
    }

      if(r.exists == 'yes'){
     $('#check').attr('checked', false);
    }
    else{
     $('#check').attr('checked', true);
    }
  });
}

此代码检查用户是否在用户名表密码字段中打开现在我要检查密码表是否为空密码字段从未打开过,这也是我的html表单

   <form id="login" action="index.php" method="post" name="doLogin" onsubmit="return loginguest();">
<input  type="hidden" name="login" value="1">
<input  id="check" checked="checked" type="checkbox" name="isGuest" value="1"  >
       <div>نام خود را در کادر زير وارد کنيد:</div>
 <input name="userName" id="username" value="فقط حروف انگليسي " type="text" autocomplete="off" maxlength="12" onkeypress="return restrict(event)" onkeyup="checkUsername();" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;">
       <div id="lay_pw" style="display:none">
        <div>رمز:</div>
        <input name="userPass" id="password" value="" type="password" autocomplete="off">
      </div>
  $password = $_POST['password'];

  if($password == "")
    {
        echo 'Password should not be empty';
        die();
    }

另一种方法是

if(empty($password))
    {
        echo 'Password field should not be empty';
        die();
    }

只需在loginguest函数中添加验证即可。

funciont loginguest (){
  if(document.getElementById('username').value().length === 0){
    alert('Username cannot be empty');
    return false;
  }
  if(document.getElementById('password').value().length === 0){
    alert('password cannot be empty');
    return false;  // to prevent submitting
  }
// ... whatever is in the function
  return true; // to submit form
}

试试看(未测试):

$link = mysql_connect($server,$user,$pass);
mysql_select_db($db);
$response = 'no';
$response_pass = 'no'; //flag for password field
if(isset($_POST['username']) && trim($_POST['username']) != ''){
$query = mysql_query("SELECT * FROM prochatrooms_users WHERE username = '".mysql_escape_string(trim($_POST['username']))."'", $link);
 if(mysql_num_rows($query) > 0){
  $response = 'yes';
  $row = mysql_fetch_array($query); 
  //checking if password exists for the username. Replace 'password' with the name of the password column from your table
  $response_pass = ($row['password']!=NULL || $row['password']!='')? 'yes' : 'no' ; 

 }
}

echo json_encode(array('exists' => $response, 'pass_exist'=>$response_pass));

在您的JavaScript中:

  function checkPassword() {
    $.customPOST({username: $('#username').val()},function(r){
       //password exists
       if(r.pass_exist == 'yes'){
        // the table contains a password value for this username so do what you need to here
       }
       else{
         // the table DOES NOT contain a password value for this username
       }
   });
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM