-1

I would like to insert random values from different arrays into MySQL tabs (see bellow) For example: ARRAYS

$st = array(a,b,c,d);
$ci = array(x,y,z,q);
$zi = array(0,1,2,3);
$st = array(00,01,02,03);

TABLES

$sql = "CREATE TABLE IF NOT EXISTS us_tAddress (
 id INT(6) UNSIGNED   AUTO_INCREMENT PRIMARY KEY,
 country VARCHAR(100),
 city VARCHAR(100),
 zip VARCHAR(100),
 street VARCHAR(100),
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
 updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
 ) "

I've started with loop like this:

foreach ($st as $value)  {$sql = "INSERT INTO us_tAddress( street ) VALUE('".$value."')"; mysqli_query($conn,$sql);}

But It was not working at all. Anyway I do not have clue, how add RANDOM function...

If you have any suggestions, how do it, I will be very thankful for it. Thank you.

  • [Little Bobby](http://bobby-tables.com/) says [you may be at risk for SQL Injection Attacks](https://stackoverflow.com/q/60174/). Learn about [Prepared Statements](https://en.wikipedia.org/wiki/Prepared_statement) with [parameterized queries](https://stackoverflow.com/a/4712113/5827005). – GrumpyCrouton May 22 '20 at 11:35

1 Answers1

0

You specified in a comment you need to insert all values from the array, but in a random order. All you need for this is the shuffle() function.

$st = array('a','b','c','d');
$ci = array('x','y','z','q');
$zi = array(0,1,2,3);
$st2 = array('00','01','02','03');

shuffle($st);
shuffle($ci);
shuffle($zi);
shuffle($st2);

foreach($st as $key => $value) {

    $column_a = $value; //random value from $st array
    $column_b = $ci[$key]; //random value from $ci array
    $column_c = $zi[$key]; //random value from $zi array
    $column_d = $st2[$key]; //random value from $st2 array

    //insert values, which will all be in a random order.
}

Example Results

GrumpyCrouton
  • 8,486
  • 7
  • 32
  • 71
  • I suggest I need lope anyway because I would like to get random value from array and, that value insert into MySQL tab. I wan to use all values in array. – Random2020 May 22 '20 at 11:41
  • Yes, I do . It is just a Homework. Let's start from beginning. I'm looking for function (script), which will be picking up random values from array. Those values another function will be inserting into MySQL table until first function will be running out of values from array . – Random2020 May 22 '20 at 11:58
  • Thank you for solving half of my problem. But, the hardest part is still unsolved. Now, I am struggling with inserting values into table. For example, values from one array will be in one column in table with random values. In conclusion table will have 25 rows. I need do that wilh all arrays at same time. Please, could you help me with that? – Random2020 May 22 '20 at 13:06
  • Yes, ALL arrays at once until will be running out of values. For example: a=array(0,1,2) - random values from 'a' array will be first column b=array(a,b,c) - random values from 'b' array will be second column – Random2020 May 22 '20 at 13:30
  • Yes, they will. – Random2020 May 22 '20 at 13:46
  • @Random2020 Check out my update. Is this what you needed? Also added a link to a PHP sandbox so you can see the results. – GrumpyCrouton May 22 '20 at 13:51