0

People enter values into a form and on one entry of the form I have multiple values.

Eg: One Entry for Name and Multiple entries for hobbies.

I could enter into the db by running a for loop but then that would be multiple entries for the same name for each different hobby.

How can I enter one name and all hobbies with 'space' into one DB field 'TWIG'. I could use arrays but it shows up as ARRAY but then its back to FOR loop.

for ($t=0; $t<=$_POST['tc']-1; $t++) { 
    echo "<BR> ts:".$_POST[$t]."<BR>"; 
    $ths[]=$_POST[$t];
}
print_r ($ths);

$statement = $link->prepare("INSERT INTO quest(cnos, dte, twig)
    VALUES(:q, :d, :t )");
$statement->execute(array(
    ":q" => htmlspecialchars ($_POST['iht']),
    ":d" => $_SERVER['REQUEST_TIME'],
    ":t"=> $ths
));

5 Answers 5

4

One possibility is to implode your hobbies / concatinate your string into one...

for ($t=0; $t<=$_POST['tc']-1; $t++) {
    $ths = $_POST[$t] . " "; //Concatinate string, do no use array!
}

//Cut off last character " " to avoid ugly space at the end:
$ths = substr($ths, 0, strlen($ths) - 1);

However a more clean solution is to make a more clear database structure if you want for atomic values.

This is an 1:n relation (Each of your entries in table A relates to n instances in table B).

Here is an example that can be adapted into your schema very easy:

Table User(id[PK], name, age);

Table User_Hobbies: (user_id, hobby_descr);

--

INSERT INTO User(2, "Chris", 19);

INSERT INTO USER_User_Hobbies(2, "videogames");
INSERT INTO USER_User_Hobbies(2, "music");


--

Example query:

SELECT u.name, u.age, GROUP_CONCAT(uh.hobby_descr) AS hobbies
FROM User u
LEFT JOIN User_Hobbies uh ON u.id = uh.user_id
WHERE u.id = 123 /*Where is optional, if you want to filter for a specific user*/
GROUP BY u.id;

Possible result:
| name  | age  | hobbies                      | 
 chris    18   videogames, music
 steve    22   computer, programming, php      
Sign up to request clarification or add additional context in comments.

8 Comments

@itachi That would require a loop query to display values in a table. Can you do a join of two tables in a mysql/php? steini
@X10nD yup it will need a loop. what's the harm in that? and yes, you'l need to join too.
Yes you can use a join query combined with group_concat and get all results with a single query. I will write an example...
INSERT INTO tbl (col1,col2,col3) VALUES(a,b,c),(d,e,f),(g,h,i); instead running 3 similiar queries, barch insert is just one query where it creates multiple entries with respective values.
Added one sample query
|
2

Use the implode function for this as follows:

":t"=> implode(" ", $ths);

2 Comments

This is just like buffering on youtube!
@X10nD your database schema isn't good. you will have too much headaches later on.
2

I am not sure, if I understand your question right, but if you want to insert an array as a comma-seperated string (or separated by whatever), why don't use the php implode function: http://php.net/manual/de/function.implode.php

For example:

$myHobbies = array("football", "basketball", "running");
$statement = $link->prepare("INSERT INTO quest(cnos, dte, twig)
    VALUES(:q, :d, :t )");
$statement->execute(array(
    ":q" => htmlspecialchars ($_POST['iht']),
    ":d" => $_SERVER['REQUEST_TIME'],
    ":t"=> implode(" ", $myHobbies)
));

I think to use comma's or semicolons as separator, is better than whitespaces, since some of the hobbies could consists of two words (for example "pc gaming").

1 Comment

+1 for the suggestion of using comma instead of space
0

I think that better solution is to use json_encode and not implode, since it provides more robust structure.

":t" => json_encode($myHobbies)

1 Comment

how does it provide better solution?
0

You can use join function to store multiple value in same field in database may be it will works:-

   ":t"=>join(",", $ths);//if you want ,(coma) between two string 

         or 
   ":t"=>join(" ", $ths);//if you want space between two string 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.