如何提示用户是否存在于数据库中

时间:2017-07-26 08:56:21

标签: php mysqli

<?php 
    include('dbcon.php');
    $code = $_POST['code']; 
    $result=mysqli_query($dbcon,"INSERT into attendance(firstname,lastname,type,year_level,date_added)Select firstname,lastname,type,year_level,CURRENT_TIMESTAMP() FROM member WHERE code='$code'") or die("Error ".mysqli_error());
    if(isset($_POST['submit']))
    {  
        if(!$result) {
            echo "Database NOT Found.";
        } else {
            $result;
            header('location:add_member_attendance.php');
        }
    } else {           
    }  
?>

如果成功输入了数据,我想显示提示,如果在数据库中找不到用户,但我不知道我的代码有什么问题。谢谢你的帮助

2 个答案:

答案 0 :(得分:0)

$query = mysqli_query("INSERT into attendance(firstname,lastname,type,year_level,date_added)Select firstname,lastname,type,year_level,CURRENT_TIMESTAMP() FROM member WHERE code='$code'");
$result = ($dbcon,$query);

$num_rows = mysqli_num_rows($result);
if(isset($_POST['submit'])){

if ($num_rows > 0) {

  // do something
}
else {
die("Error ".mysqli_error($dbcon));
  // do something else
}

}

答案 1 :(得分:0)

你可以试试这个

<?php 
include('dbcon.php');
$code = $_POST['code']; 


if(isset($_POST['submit']))
{  
$insert=mysqli_query($dbcon,"Select firstname,lastname,type,year_level,CURRENT_TIMESTAMP() FROM member WHERE code='$code'") or die("There was an error checking due to".mysql_error());

if(mysql_num_rows($insert)>0){
// this means that the code already exist;
$result="INSERT into attendance(firstname,lastname,type,year_level,date_added)Select firstname,lastname,type,year_level,CURRENT_TIMESTAMP() FROM member WHERE code='$code'";

//check if the query run
if(mysqli_query($dbcon,$result)){//or you can do mysql_query($result)
header('location:add_member_attendance.php');
//you can do anything you want here 
}else{
echo "The insert query did not run due to ".mysql_error();
}
}else{

echo "The code doesnt exist"
}
}
?>