如何让用户在php中选择不同的颜色模式

时间:2017-10-29 15:35:06

标签: php html colors

<form action="" method="post" id="customer-register">
<table align="center">
 <div id="container">
    <h1>Welcome to Registration Page!</h1>

       <td>Full Name:</td>
       <td><input type="text" name="fname" placeholder="enter your name">
</td>
    </tr>

    <tr>
       <td>Username:</td>
       <td><input type="text" name="uname" placeholder="enter your 
username"></td>
    </tr>

    <tr>
       <td>Email:</td>
       <td><input type="text" name="email" placeholder="enter your email">
</td>
    </tr>

    <tr>
       <td>Select your Password:</td>
       <td><input type="color" name="color1" value="#ff0000"><input 
type="color" name="color2" value="#ff0000"><input type="color" name="color3" 
value="#ff0000"><input type="color" name="color4" value="#ff0000"><input 
type="color" name="color5" value="#ff0000"></td>
    </tr>
       <td><input type="submit" name="submit" value="Register"></td>
 </tr>    
</body>
</html>

这是我的register.php项目的代码。正如你所看到的,我没有使用文本密码,而是我允许用户选择5种颜色来注册。我的问题是我有一个源代码,以确保用户不会输入与其他用户相同的颜色模式已输入并保存在数据库中?

 if(isset($_POST['submit'])){
   $fname=$_POST['fname'];
   $uname=$_POST['uname'];
   $email=$_POST['email'];
   $pass1=$_POST['color1'];
   $pass2=$_POST['color2'];
   $pass3=$_POST['color3'];
   $pass4=$_POST['color4'];
   $pass5=$_POST['color5'];
   $register_query = "INSERT INTO `cust`(`fname`, `uname`, `email`, 
`color1`, `color2`, `color3`, `color4`, `colo5`) VALUES 
('$fname','$uname','$email','$pass1', '$pass2', '$pass3', '$pass4', 
'$pass5')";
   try{
       $register_result = mysqli_query($conn,$register_query);
       if($register_result){
           if(mysqli_affected_rows($conn)>0){
               header("location: success.html");
           }else{
               echo("error in registeration");
           }


       }

    }catch(Exception $ex){
       echo("error" .$ex->getMessage());
    }
 }else {

 }
   this is my code for handling the register.

1 个答案:

答案 0 :(得分:0)

您需要在INSERT查询之前进行查询。

$check_query = "SELECT COUNT(*) AS `total` FROM `cust` WHERE `color1`='$pass1' AND `color2`='$pass2' AND `color3`='$pass3' AND `color4`='$pass4' AND `color5`='$pass5'";
if ($check_res = mysqli_query($conn, $check_query)) {
    $row = mysql_fetch_assoc($check_res);
    if ($row['total'] > 0) {
        // this means you shouldn't do the insert as a customer exists with this exact sequence of colours.
    }
    else {
        // do your insert here.
    }
}

如果total的值为&gt; 0,那么你知道有一个客户有这些确切的颜色组合。这假定了一种特定的颜色顺序。