-1

I’m new to PHP and I need help with a few things. First I need to merge two arrays together and separate them using a comma and then I need to be able to update my database to insert the result of the merged arrays into one row. To further explain I have a database table called contents. What I want to do in this table is so I need the result of the merge array to be inserted into tierString this is a varchar and not an int but I need it to take in two numbers:

id | name     | desc     | tierString 
1     name1      blah         1,2

This is my function to update the database:

function updateTierString($tier_array){
    $result = false;
    foreach($tier_array as $content_id => $tier){
     
    $update = “UPDATE CONTENTS
               SET 
               tier_string = $tier,
               updated = NOW()
               WHERE
               id = ‘$content_id’”;
   $update = db_query($update);
   }
return $result;

So my issue is I want to merge two arrays together and then separate them using a comma and then insert it into the database. The two arrays would contain integers but I want to turn it into a string. Is it possible?

Lochness
  • 11
  • 3
  • your code is **vulnerable** to **sql injection** so use **prepared statements with parameterts** see https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – nbk Aug 24 '21 at 19:18
  • You would generally avoid doing this and look at how to normalise your database design - https://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad – Nigel Ren Aug 24 '21 at 19:46

1 Answers1

1
$arr1 = [1, 2, 3];
$arr2 = [4, 5, 6];

$merged_arr = array_merge($arr1, $arr2);

$comma_seprated = implode(',', $merged_arr);

Now you can use the $comma_seprated var and put in the tierString column.

Abbas
  • 1,118
  • 1
  • 11
  • 25