Fin del Mundo
Fin del Mundo

Reputation: 156

for() and mysql statement

I use the Paypal IPN. When the user buys articles, I use this code to generate codes they can redeem. However, it's not working:

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 is the number of products I don't know if I'm using the for() statement correctly.

Upvotes: 0

Views: 32

Answers (1)

M0rtiis
M0rtiis

Reputation: 3774

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;
}

OR little better

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;
}

Upvotes: 1

Related Questions