从网站上的MYSQL表中查看促销代码

时间:2018-12-06 04:12:59

标签: php html mysql


我正在尝试在HTML网站上创建一个系统,您可以在其中输入代码,然后通过php文件对其进行检查。
php将代码与我的MYSQL表中的代码进行比较。 php还应检查代码是否已全部使用。
检查完成后,网站上会出现一条消息,告知用户其密码是否有效。
如果该密码有效,它将以某种方式通知我有人输入了密码。
我已经写了一些代码,但是当我按下按钮时根本不起作用。
这里是我的HTML代码:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11/jquery.min.js">
</script>

<script>
$(document).ready(function() {  

    //the min chars for promo-code
    var min_chars = 6;  

    //result texts  
    var checking_html = 'Checking...';  

    //when keyup  
    $('#code').keyup(function(event){ 
        //run the character number check  
        if($('#code').val().length == min_chars){  

            //show the checking_text and run the function to check  
            $('#Promo_code_status').html(checking_html);  
            check_code();  
        }  
    });  

});  

//function to check the promo code  
function check_code(){  

    //get code  
    var code = $('#code').val();  

    //use ajax to run the check  
    $.post("check_code.php", { code: code },  
        function(result){  

        //if the result is 0  
        if(result == 0){  
            //show that the code is correct  
            $('#Promo_code_status').html(code + ' is correct.');  
        }else if(result == 1){  
            //show that the code is correct, but already has been used 
            $('#Promo_code_status').html(code + ' has allready been used.');  
        }else{
            //show that the code is not correct 
            $('#Promo_code_status').html(code + ' is not correct.');  
        }
    });  
} 
</script>
</head>
<body>

<center><h1>Enter Promo Code</h1>
<form method="post" action="check_code.php">
    <input type="text" id="code" name="code" maxlength="6" />
    <div id="promo_code_status"></div>
    <br>
    <input type="submit" value="Let's go"></center>

</form>

</body>
</html>

这是我的PHP文件:

<?php

//connect to database  
$user = "***";  //Username
$pass = "***";  //Password
$host = "localhost";  //Host
$dbdb = "***";  //Database name

$connect = mysqli_connect($host, $user, $pass, $dbdb);
if(!$connect)
{
trigger_error('Error connection to database: '.mysqli_connect_error());
}

//get the code
mysqli_real_escape_string($connect, $_POST['code']);  

//mysql query to select field code if it's equal to the code that we checked '  
$result = mysqli_query($connect, 'select Code from Codes where Code = "'. $code .'"');  
$record = mysqli_fetch_array($result);

//if number of rows fields is bigger them 0 that means the code in the database'  
if(mysqli_num_rows($result) > 0){  
 if($record['used'] == 0) {
     //and we send 0 to the ajax request  
     echo 0;
} else{
    //and we send 1 to the ajax request  
    echo 1;  
}
}else{  
//else if it's not bigger then 0, then the code is not in the DB'  
//and we send 2 to the ajax request  
echo 2;  
}  
?>

通过按“ Let's go”按钮,PHP将完成其工作,然后它将向用户提供有关其代码的信息。 我必须更改什么?

2 个答案:

答案 0 :(得分:1)

这是我没有在$ code中分配帖子值的原因。

SampleID    Chrom   Start       End         Genes
HSB275      chr1    243216377   243219494   ENSG00000143702,ENSG00000143702
HSB274      chr10   952208      979839      ENSG00000205740
HSB272      chr10   1046378     1047984     ENSG00000067064,ENSG00000067064,ENSG00000067064
HSB481      chr11   654157      655184      ENSG00000177030

答案 1 :(得分:0)

  1. 确保您正在使用的jquery CDN可以访问。因为它看起来像是错误的CDN。
  2. checkcode()函数中,您使用#Promo_code_statusP大写,而在html中,您将promo_code_statusp小写。
  3. 在php文件中,您忘记为$code分配值了

我希望这对您有帮助