一次性验证促销代码使用PHP / My SQLi [英] Validate Promo Code for one time use PHP/My SQLi

查看:116
本文介绍了一次性验证促销代码使用PHP / My SQLi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对整个MySQLi事情很陌生。我对PHP有基本的了解。无论如何,我在这里搜索,只是一直无法得到一个坚实的答案。

基本上,我有一个人可以输入名称,电子邮件和促销代码。表单验证名称和电子邮件,但是当涉及到促销代码时,这就是我遇到的问题。



我有一个有两列的数据库。一个用于代码,另一个用于已用列 - 最终,当需要使用唯一代码时,我需要能够为该列写入1,以便它不能再次使用。



以下是连接到数据库的PHP:

  if(isset($ _ POST ['sponsorcode'])&&!empty($ _ POST [sponsorcode])){
$ sponsorcode = mysqli_real_escape_string($ link,$ _ POST ['sponsorcode']);
$ query =SELECT'sponsorcode'FROM'teachercodes'WHERE sponsorcode ='$ sponsorcode';
$ result = mysqli_query($ link,$ query)或die(mysqli_error($ link));
$ option =;
if(mysqli_num_rows($ result)> 0){
while($ row = mysqli_fetch_array($ result)){
$ option =< option value ='{$ row [ '编码']} '> {$行[' 编码]}< /选项>中;
}

任何提示都将非常感谢!谢谢。

解决方案

没有理由将您的任务作为两个单独的步骤来执行。只需标记在 teachercodes 表格中使用的赞助商代码即可。如果更新影响到任何行(即 mysqli_affected_rows 返回1或更多),那么它之前没有被使用过,并且是有效的赞助商代码。就像这样:

  //确保提供的赞助商代码是
if(isset($ _ POST ['sponsorcode ')&&!empty($ _ POST ['sponsorcode'])){

//转义赞助商代码以防止SQL注入
$ code = mysqli_real_escape_string($ link, $ _POST [ 'sponsorcode']);

//将赞助商代码标记为可能的
$ sql ='UPDATE teachercodes SET used = 1 WHERE sponsorcode ='。$ code。'';
mysqli_query($ link,$ sql)或die(mysqli_error($ link));

if(mysqli_affected_rows($ link)){
//赞助商代码之前没有使用且有效
}
}
code>


I’m very new to the whole MySQLi thing. I have a basic understanding of PHP. Anyway, I’ve searched around here and just haven’t been able to get a solid answer.

Basically, I have a form where a person can enter name, email and promo code. The form validates the name and email but when it comes to the promo code, that’s where I’m getting stuck.

I have a database that has two columns. One is for the codes and the other is for a "used" column – eventually I need to be able to write a "1" to that column when a unique code has been used so it cannot be used again. I’m trying to use some code I found on here, FYI.

Here is the PHP (after connecting) to the database:

if(isset($_POST['sponsorcode']) && !empty($_POST["sponsorcode"])){
   $sponsorcode = mysqli_real_escape_string($link,$_POST['sponsorcode']);
   $query = "SELECT 'sponsorcode' FROM 'teachercodes' WHERE sponsorcode = '$sponsorcode'";
   $result = mysqli_query($link, $query) or die(mysqli_error($link));
   $option = "";
   if(mysqli_num_rows($result)>0){
       while($row=mysqli_fetch_array($result)) {
       $option = "<option value='{$row['codes']}'>{$row['codes']}</option>";
}

Any tips would be GREATLY appreciated! Thanks.

解决方案

No reason to perform your task as two separate steps. Simply mark the sponsor code as used in the teachercodes table. If the update affected any rows (i.e. mysqli_affected_rows returns 1 or more) then it hasn't been used before and is a valid sponsor code. Something like this:

// Make sure a sponsor code was provided
if (isset($_POST['sponsorcode']) && !empty($_POST['sponsorcode'])) {

    // Escape the sponsor code to prevent SQL injection
    $code = mysqli_real_escape_string($link, $_POST['sponsorcode']);

    // Mark sponsor code as used if possible 
    $sql = 'UPDATE teachercodes SET used=1 WHERE sponsorcode="' . $code . '"';
    mysqli_query($link, $sql) or die(mysqli_error($link));

    if (mysqli_affected_rows($link)) {
        // Sponsor code hasn't been used before and is valid
    }
}

这篇关于一次性验证促销代码使用PHP / My SQLi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆