save random numbers

Asked

Viewed 49 times

0

I’m making a site to generate promotional coupons, but at the time of saving in the database is giving this error. Notice: Array to string Conversion in. would like to know if anyone can help me. follow the code below:

// comes from the form

$preco = $_POST['preco'];
$promo = $_POST['promo'];


// conta
    $resultado = $preco / $promo;
   

// generates the random number ]

for ($i = 1; $i <= $resultado; $i++) { $n[] = str_pad(rand(10000000, 99999999), 2, '0', STR_PAD_LEFT); }


sort($n);

echo implode('<br/>', $n);

Registration fee($connection, $price, $promo, $n){

  $inserir = "insert into cupons (preco, promo, cupons) values ({$preco}, {$promo}, '{$n}')";

  $resultadoDaInsercao = mysqli_query($conexao, $inserir);
  return $resultadoDaInsercao;

}

if(cadastracaixa($connexion, $preco, $promo, $n)){ echo "Alert('successfully entered data');"; }Else{ $msg = mysqli_error($connected); echo "Alert('data not entered. Try again');"; }

  • $n[] is an array, you need to convert it to string.

2 answers

0

I believe you could use:

$n = str_pad(rand(10000000, 99999999), 2, '0', STR_PAD_LEFT);

Since the str_pad returns a string.

If the table column is a varchar or an int and you are passing $n[] it will be a problem because it is an array

0

its variable $n is an array, Voce would have to take the position $n[x] or use the column of type JSON and save the array with JSON_ARRAY($n)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.