如何在php中验证ID(主键)?

问题描述:

我在一个php页面有一个字段输入注册号并检查详细信息。 Here is the ScreenShot. 注册号是表中的ID。 那么如何验证在数据库中使用寄存器编号在页面中输入的寄存器编号。如何在php中验证ID(主键)?

这里是代码:

<?php 
session_start(); 
include 'connect1.php'; 
if (isset($_POST['login'])) 
{ 
    $_SESSION['regno']= $_POST['regno']; 
    $regid = $_SESSION['regno']; 
    if (empty($regid)){ 
     echo "<p class='echo'> *The Field Should not be empty </P>"; 
    } 
    else if ($regid =)//WHAT SHOULD BE CHECKED HERE ?????? 
    { 
     header('location: details.php');// If not empty, Details page is loaded. 
    } 
} 
?> 
<html> 
<head> 
    <title> 
     Login 
    </title> 
    <link rel="stylesheet" type="text/css" href="style.css"> 
</head> 
<body> 
<div class="reg"> 
    <h1 class="h1">Registered User?</h1> 
    <form action="" method="POST" > 
    <input type="number" class="textbox" name="regno" placeholder="Registration Number"/><br/> 
    <input type="submit" class="login" name="login" value="Check"> 
</form> 
</div> 
</body> 
</html> 

我需要检查,如果在字段中输入的号码在数据库?

感谢提前:)

+0

是的,你应该检查它的数据库中。使用mysqli连接到你的数据库并选择id = registration_number的产品。 –

+0

使用where子句运行选择查询并检查num行。如果num行== 0,则无效输入。否则有效。 –

+0

“SELECT id FROM table_name where id ='$ regid'” –

答案就在你的模式中使用逻辑并实现它。

<?php 
$con=mysqli_connect("localhost","my_user","my_password","my_db"); 
// Check connection 
if (mysqli_connect_errno()) { 
echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 
// escape variables for security 
$_SESSION['regno'] = mysqli_real_escape_string($con, $_POST['regno']); 
$regid =$_SESSION['regno']; 
if (empty($regid)){ 
    echo "<p class='echo'> *The Field Should not be empty </P>"; 
} 
else 
{ 
    $sql="select regno from your_table where regno='".$regid."'"; 
    $result=mysqli_query($con,$sql); 
    $number=mysqli_num_rows($result); 
    if($number > 0){ 
    header('location: details.php');// If not empty, Details page is loaded. 
    }else{ 
    echo "<p class='echo'> *Not Found </P>"; 
    } 

} 

就作出这样的请求:

Select * From table_name Where ID = the_checked_ID 

如果没有返回值,该ID是不是在你的数据库

+1

没有安全检查!还需要在** PHP **中实现 –

试试这个..

<?php 
$current_path = $_SERVER['SCRIPT_NAME']; 
//include database connection code here :) 
if(isset($_POST['regno'])) 
{ 
    $regnumber = $_POST['regno']; 
    $query = "Select * From your_table_name Where ID = '$regnumber'"; 
    if($query_run = mysql_query($query)) 
    { 
     $num_rows = mysql_num_rows($query_run); 
     if($num_rows == 1) 
     { 
      echo "This is registered User"; 
     } 
     else 
     { 
      echo "Please Register"; 
     } 
    } 
} 
?> 
<html> 
<head> 
    <title> 
     Login 
    </title> 
    <link rel="stylesheet" type="text/css" href="style.css"> 
</head> 
<body> 
<div class="reg"> 
    <h1 class="h1">Registered User?</h1> 
    <form action="<?php echo $current_path ?>" method="POST" > 
    <input type="number" class="textbox" name="regno" id="regno" placeholder="Registration Number"/><br/> 
    <input type="submit" class="login" name="login" value="Check"> 
</form> 
</div> 
</body> 
</html>