0

I have 2 tables

tbl_patient 
pid       sample_id    fullname             test_ivd
 1        AA01         John Vick            test 1; test 2; test 3

and

tbl_test
tid         sample_id    name_test
 1          AA01         test 1
 2          AA01         test 2
 3          AA01         test 3     

When I add a new record (new patient) into tbl_patient, I stored one string separated by ; (list of test names) in test_ivd field. And in same time, I want to split the test_ivd by ; and add to the 3 new records in tbl_test. So please give me some suggestion.

Thank for any suggestion.

  • check this : https://stackoverflow.com/q/45478226/2815635 – Niklesh Raut Aug 17 '17 at 10:11
  • 2
    Why would you want to store the data in such a redundant way? That only increases the potential for stuff to go wrong, if updating the one succeeds and the other fails ... You should rather use proper JOINs to get this data from the tbl_test table when needed. – CBroe Aug 17 '17 at 10:13
  • @user2486 How is that question related? He doesn't need to alternate delimiters. – Barmar Aug 17 '17 at 10:14

4 Answers4

0

use explode: explode ( string $delimiter , string $string [, int $limit = PHP_INT_MAX ] ) http://php.net/manual/es/function.explode.php

caryarit ferrer
  • 326
  • 3
  • 12
0

I would suggest handling that with php.

  1. Insert patient into database
  2. retrieve inserted id
  3. Insert tests into test table for retrieved id (you can write your query to insert sample_id of your patient instead of its id)
chilly
  • 304
  • 2
  • 13
0

Example :

$string = "chicken;lamb;beef";
$stuff = explode(";", $string);
print_r($stuff);

//access by $stuff[0], $stuff[1] etc
AvadaKedavra
  • 29
  • 1
  • 3
0

You can do this using PHP explode function.

$ivdString = 'test 1; test 2; test 3';
$strArr = explode(";", $ivdString);

For insert record.

foreach ($strArr as $key => $value) {
      $sql = INSERT into table(sample_id,name_test)values('', '$value');
      Your insert here ...
      ......
}
Pravin Vavadiya
  • 3,195
  • 1
  • 17
  • 34