如何使用php将有限记录插入mysql

如何使用php将有限记录插入mysql,php,Php,有没有办法注册有限的用户。这意味着我使用insert查询注册了10个用户。现在我想检查数据库中是否有10个注册用户。停止注册更多用户。并显示消息“抱歉,我们无法创建您的帐户”10个用户已注册 <?php require('connection.php'); if($_SERVER['REQUEST_METHOD']=='POST') { $user_name = $_POST['username']; $father_nam

有没有办法注册有限的用户。这意味着我使用insert查询注册了10个用户。现在我想检查数据库中是否有10个注册用户。停止注册更多用户。并显示消息“抱歉,我们无法创建您的帐户”10个用户已注册

<?php
require('connection.php');
    if($_SERVER['REQUEST_METHOD']=='POST')
    {

            $user_name = $_POST['username'];
            $father_name = $_POST['fname'];
            $password = $_POST['password'];
            $confirm_password = $_POST['confirmpassword'];
            if($password==$confirm_password){
            $insert = mysql_query("insert into user values('','$user_name','$father_name','$password','$confirm_password')");
            }
            else
            {
                $passwordmsg = "Password Must Be Matched";
            }
            if($insert)
            {
                $msg = "Registration Succesfull";   
            }
            else
            {
                $msg = "Registration failled please Try Again Later !";
            }

    }
?>

插入新用户之前,请使用select命令检查用户数并返回行数。如果行数大于10,则显示消息。 一个简单的if条件可以解决您的问题

<?php
 require('connection.php');
 if($_SERVER['REQUEST_METHOD']=='POST')
  {
    $no_of_rows = mysql_query("count(*) FROM user");

    if($no_of_rows <10){
    $user_name = $_POST['username'];
    $father_name = $_POST['fname'];
    $password = $_POST['password'];
    $confirm_password = $_POST['confirmpassword'];
    if($password==$confirm_password){
    $insert = mysql_query("insert into user          values('','$user_name','$father_name','$password','$confirm_password')");
    }
    else
    {
        $passwordmsg = "Password Must Be Matched";
    }
    if($insert)
    {
        $msg = "Registration Succesfull";   
    }
    else
    {
        $msg = "Registration failled please Try Again Later !";
    }
  }
  else{
    $msg = "Number of users reached limit. Please try again later!"
   }
 }
?>

浏览文档。

require('connection.php');
如果($\u服务器['REQUEST\u METHOD']=='POST'){
$str_sql=mysql_查询(“从用户选择*);
$int\u rows=mysql\u num\u rows($str\u sql);
如果($int_行<10){
$user\u name=$\u POST['username'];
$father_name=$_POST['fname'];
$password=$_POST['password'];
$confirm\u password=$\u POST['confirmpassword'];
如果($password==$confirm\u password){
$insert=mysql_查询(“插入到用户值中(“”,$user_name’、“$father_name’、“$password’、“$confirm_password”);
}否则{
$passwordmsg=“密码必须匹配”;
}
如果($插入){
$msg=“注册成功”;
}否则{
$msg=“注册失败,请稍后再试!”;
}
}
}

有多种方法。你尝试过哪些选项?你在哪里被卡住了?@Dainis Abols我尝试了mysql\u num\u rows来计算记录,但它不起作用。听说过吗?请写代码。。。!谢谢Amith RajShetty@Khan:你试过了吗?@Khan:你在干什么?你接受了三个答案?只接受一个对你有用的答案。谢谢克里斯托的帮助
$mysqli = new mysqli($host,$user, $password, $database);
$sql = "SELECT * FROM table";
$result = $mysqli->query($sql) or trigger_error($mysqli->error." [$sql]"); 
if($result->num_rows == 10) {
   //no more users can be registered
} else {
   //insert and register new user
}
$mysqli->close() ;
require('connection.php');
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $str_sql = mysql_query("SELECT * FROM user");
    $int_rows = mysql_num_rows($str_sql);
    if ($int_rows < 10) {
        $user_name = $_POST['username'];
        $father_name = $_POST['fname'];
        $password = $_POST['password'];
        $confirm_password = $_POST['confirmpassword'];
        if ($password == $confirm_password) {
            $insert = mysql_query("insert into user values('','$user_name','$father_name','$password','$confirm_password')");
        } else {
            $passwordmsg = "Password Must Be Matched";
        }
        if ($insert) {
            $msg = "Registration Succesfull";
        } else {
            $msg = "Registration failled please Try Again Later !";
        }

    }
}