【问题标题】:PHP, check if user exists in database on table? [closed]PHP,检查用户是否存在于表的数据库中? [关闭]
【发布时间】:2015-11-15 03:32:56
【问题描述】:

我正在尝试检查var a=document.forms["reg"]["matricula"].value; 是否存在于我的数据库中,但用户将使用此值!

如何检查我的数据库中是否存在 var "a"?

function validateForm()
{
var a=document.forms["reg"]["matricula"].value;
var b=document.forms["reg"]["nome"].value;
var c=document.forms["reg"]["cargo"].value;

if ((a==null || a=="") && (b==null || b=="") && (c==null || c==""))
  {
  alert("Todos os campos devem ser preenchidos!");
  return false;
  }

if (a==null || a=="")
  {
  alert("A matrícula deve ser preenchida!");
  return false;
  }
if (b==null || b=="")
  {
  alert("O nome deve ser preenchido!");
  return false;
  }
if (c==null || c=="")
  {
  alert("O cargo deve ser selecionado!");
  return false;
  }
}

【问题讨论】:

  • 带有选择查询?
  • 创建一个单独的 php 文件,将您的 php 代码放在那里,然后通过 ajax 从您的 js 中提取该页面值。
  • @Dagon,是的!查询
  • 嗯,写一个?你在问我们什么?

标签: javascript php html mysql sql


【解决方案1】:

如何检查我的数据库中是否存在 var "a"?

有很多方法可以实现它。

对于您的情况,因为您正在标记 javascript。 验证表单后,将表单值发送到单独的查询并将其返回到同一页面。在本例中,查询结果将返回到一个警告框。

function validateForm() {
//.......if(...) form validation starts....

    if (c==null || c=="") {
        alert("O cargo deve ser selecionado!");
        return false;
    }

//Submit matricula and or other value to query page and return result to an alert box example
    var xhttp = new XMLHttpRequest();
    var querypage = "yourquerypagelocation.php";
    var form = document.getElementById("reg");
    var formData = new FormData(form);
    xhttp.onreadystatechange = function() {
        if (xhttp.readyState == 4 && xhttp.status == 200) {
            alert(xhttp.responseText);//<- this will open a alert box with your query response
            }
        }
    xhttp.open("POST",querypage,true);
    xhttp.send(formData);
}

因为我们只返回一个警告框,所以请确保您的结果尽可能简单,除非您考虑将结果返回到页面内的 div,这与您可能要问的内容无关现在。

yourquerypage.php

<?php
//whatever your other code might be (session, connection or whatsoever)
....
....
$matricula = $_POST['matricula'];
$query = ("SELECT matricula from [TABLE] where matricula LIKE '$matricula'")

if ($query){
    echo "Exists";//This will appear in alert box if query return true with the argument WHERE matricula LIKE $matricula.
    }else{
    echo "Not exists";
    }
?>

希望这个例子有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-02
    • 2013-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-09
    • 2021-12-15
    相关资源
    最近更新 更多