【问题标题】:for() and mysql statementfor() 和 mysql 语句
【发布时间】:2015-09-02 01:56:04
【问题描述】:

我使用 Paypal IPN。当用户购买文章时,我使用此代码生成他们可以兑换的代码。但是,它不起作用:

public function clave(){
       $Valores = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
       $ValorTemporal = "";
        for($i=0;$i<10;$i++) {
        $ValorTemporal .= substr($Valores,rand(0,37),1);

        }
        return $ValorTemporal;
    }

    public function cupon($items){
                $mysqli = $this->connection();
            for ($i = 1; $i <= $items; $i++) {
        $yz= $this->clave();
    $sqli.$i = $mysqli->query("INSERT INTO ventas (id_venta, id_usuario,id_producto ,used,cupon) VALUES ('$txn_id','$username','$item_name','$used','$yz')");

    }

            return true;
    }

$items是产品数量 我不知道我是否正确使用了for() 语句。

【问题讨论】:

  • 这是什么? $sqli.$i$txn_id $username $item_name $used 在你中未定义 cupon 函数
  • $sql 只是一个标识符,其他的都是全局定义的

标签: php mysql for-loop


【解决方案1】:
public function cupon($items){
    global $txn_id, $username, $item_name, $used;

    $mysqli = $this->connection();
    for ($i = 1; $i <= $items; $i++) {
        $yz = $this->clave();
        $mysqli->query("
            INSERT INTO `ventas` 
                (`id_venta`, `id_usuario`, `id_producto`, `used`, `cupon`) 
            VALUES 
                ('$txn_id', '$username', '$item_name', '$used', '$yz')
        ");
    }
    return true;
}

或者好一点

public function cupon($items, $txn_id, $username, $item_name, $used){
    $mysqli = $this->connection();
    for ($i = 1; $i <= $items; $i++) {
        $yz = $this->clave();
        $mysqli->query("
            INSERT INTO `ventas` 
                (`id_venta`, `id_usuario`, `id_producto`, `used`, `cupon`) 
            VALUES 
                ('$txn_id', '$username', '$item_name', '$used', '$yz')
        ");
    }
    return true;
}

【讨论】:

    猜你喜欢
    • 2011-07-31
    • 1970-01-01
    • 2013-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-22
    • 2021-03-13
    相关资源
    最近更新 更多