Connecting Tech Pros Worldwide Forums | Help | Site Map

Problem storing array in database

Member
 
Join Date: Feb 2008
Posts: 63
#1: Mar 16 '08
Hi,
I have taken skills form the user in a text box using comma separated values. For eg: Key Skills: C,C++,Java
Now i have stored this vaues into array named arrSkills using explode function. Now before entering them into database i first check in the tagmaster table if already a tagnamed arrSkills exists in the table. If it exists i select the tagId of the corresponding tagname. If it doesnt exists then i first insert that arrSkills into tagmaster and then select its tagId. After the TagId is selected i enter the tagId and username into usertags table. The problem here is that it enters the tagId as the number of elements entered in KeySkill i.e. 3 for above example and tagname as Array in tagmaster table and makes the usertags table go to infinite value.
This is the code that i have used. Plz check out the error and let me know.


Expand|Select|Wrap|Line Numbers
  1. $arrSkills = explode(",",$KeySkills);      
  2. while($arrSkills != 0){   
  3. $query1 = mysql_query("SELECT TagId FROM tagmaster WHERE TagName = '$arrSkills'") or die(mysql_error());
  4. $numrow = mysql_num_rows($query1);
  5. if($numrow == 0){
  6. mysql_query( "INSERT INTO $db_table3(TagName)values('$arrSkills')") or die(mysql_error());  
  7. $query2 = mysql_query("SELECT TagId FROM tagmaster WHERE TagName = '$arrSkills'") or die(mysql_error());
  8. $info = mysql_fetch_array($query2);
  9. mysql_query( "INSERT INTO $db_table4(TagId,UserName)values('$info[TagId]','$_POST[UserName]')") or die(mysql_error());  
  10. } //end of if
  11. else{
  12. $info = mysql_fetch_array($query1);
  13. mysql_query( "INSERT INTO $db_table4(TagId,UserName)values('$info[TagId]','$_POST[UserName]')") or die(mysql_error());  
  14. } //end of else
  15. } //end of while
  16.  
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#2: Mar 16 '08

re: Problem storing array in database


It would be nice if you would let us know, every now and then, that the advice and the code that was given to you in this forum, was actually used or implemented or that a solution or hint was implemented.

Our members help you out on a lot of occasions, doing this freely, in their spare time. Some problems cost a lot of time to investigate.

All I see is that our members are good enough to enter numerous advices/solutions and even sets of code, but they never hear the result of their efforts. That makes people of bit wary.

Ronald
Member
 
Join Date: Feb 2008
Posts: 63
#3: Mar 16 '08

re: Problem storing array in database


Sorry and thanks a lot. Each and every advice, hint and solution helps me alot in implementin my project.
Thanks a lot.
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#4: Mar 16 '08

re: Problem storing array in database


Ok and thank you. As to your problem:

The explode function constructs an array, so when you assign that the result is an array. nAnd you cannot insert an array in a db field.
Quote:

Originally Posted by explode

Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter

So what is your intention with that array from your first statement?

Ronald
Member
 
Join Date: Feb 2008
Posts: 63
#5: Mar 16 '08

re: Problem storing array in database


Thankyou.
I want the values in $arrSkills to be stored in the database.
So do i need to change the asssignment of $arrSkills.
Plz let me know.
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#6: Mar 16 '08

re: Problem storing array in database


Quote:

Originally Posted by agarwalsrushti

Thankyou.
I want the values in $arrSkills to be stored in the database.
So do i need to change the asssignment of $arrSkills.
Plz let me know.

You should just store the user submitted string into the database without exploding it. Then, when you extract the string, explode it.

Regards.

p.s. Sorry for butting in and also sorry if i missed the problem.
Member
 
Join Date: Feb 2008
Posts: 63
#7: Mar 16 '08

re: Problem storing array in database


It is working now. i made a small mistake. But now the problem is if there are 3 values in key skills eg:C,C++,Java then also it iterates 4 times. So what could possibly done for this. The code that is working now is below.

Expand|Select|Wrap|Line Numbers
  1. $arrSkills = explode(",",$_POST['KeySkills']);      
  2. foreach($arrSkills as $skill){   
  3. $query1 = mysql_query("SELECT TagId FROM tagmaster WHERE TagName = '$skill'") or die(mysql_error());
  4. $numrow = mysql_num_rows($query1);
  5. if($numrow == 0){
  6. mysql_query( "INSERT INTO $db_table3(TagName)values('$skill')") or die(mysql_error());  
  7. $query2 = mysql_query("SELECT TagId FROM tagmaster WHERE TagName = '$skill'") or die(mysql_error());
  8. $info = mysql_fetch_array($query2);
  9. mysql_query( "INSERT INTO $db_table4(TagId,UserName)values('$info[TagId]','$_POST[UserName]')") or die(mysql_error());  
  10. } //end of if
  11. else{
  12. $info = mysql_fetch_array($query1);
  13. mysql_query( "INSERT INTO $db_table4(TagId,UserName)values('$info[TagId]','$_POST[UserName]')") or die(mysql_error());  
  14. } //end of else
  15. } //end of while
  16.  
Member
 
Join Date: Feb 2008
Posts: 63
#8: Mar 16 '08

re: Problem storing array in database


Everything is working fine now.
Thanks a lot for your interest.
Member
 
Join Date: Feb 2008
Posts: 63
#9: Mar 16 '08

re: Problem storing array in database


Now i m stuck up with a new problem in it.
Im not able to retrieve the skills to be displayed it for the users.

Expand|Select|Wrap|Line Numbers
  1. $result8= " SELECT TagId FROM usertags WHERE UserName= '$uname'";
  2. $data8 = mysql_query($result8) or die(mysql_error()); 
  3. while($info8 = mysql_fetch_array( $data8)){
  4. $result9 = "SELECT TagName FROM tagmaster WHERE TagId= ".$info8['TagId']."" 
  5. $data9 = mysql_query($result9) or die(mysql_error()); 
  6. $info9 = implode(",",$data9);
  7. }
  8.  
Plz what is the error in this code.
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#10: Mar 16 '08

re: Problem storing array in database


Quote:

Originally Posted by agarwalsrushti

Now i m stuck up with a new problem in it.
Im not able to retrieve the skills to be displayed it for the users.
Plz what is the error in this code.

After a query like in [php]$data9 = mysql_query($result9) or die(mysql_error());
$info9 = implode(",",$data9);[/php]you MUST fetch the row before you can implode a value string into $info9

Ronald
Member
 
Join Date: Feb 2008
Posts: 63
#11: Mar 17 '08

re: Problem storing array in database


I fetched the row but now its displaying the last value twice. Eg. Java,Java.
I want it to echo somewhere else in my form so there it displays twice the last value. Whereas if i echoes $skills in the loop as shown below it echoes all the values twice. Eg. C,C,C++,C++,Java,Java

Expand|Select|Wrap|Line Numbers
  1. $result8= " SELECT TagId FROM usertags WHERE UserName= '$uname'";
  2. $data8 = mysql_query($result8) or die(mysql_error()); 
  3. while($info8 = mysql_fetch_array( $data8)){
  4. $result9 = "SELECT TagName FROM tagmaster WHERE TagId= ".$info8['TagId']."" ;
  5. $data9 = mysql_query($result9) or die(mysql_error());
  6. $info9 = mysql_fetch_array( $data9);
  7. //$data = $info9['TagName'];
  8. echo $info9['TagName'];
  9. $skills = implode(",",$info9);
  10. //echo $skills;
  11. }
  12.  
Why is it taking the values twice?
Member
 
Join Date: Feb 2008
Posts: 63
#12: Mar 17 '08

re: Problem storing array in database


Now it is properly taking the values just once. But when i echo it in my form it just displays the last value in the array.
Moreover i tried echoing the value of implode here itself, it displays the value properly but doesnt shows comma between the values.

Expand|Select|Wrap|Line Numbers
  1. $result8= " SELECT TagId FROM usertags WHERE UserName= '$uname'";
  2. $data8 = mysql_query($result8) or die(mysql_error()); 
  3. while($info8 = mysql_fetch_array( $data8)){
  4. $result9 = "SELECT TagName FROM tagmaster WHERE TagId= ".$info8['TagId']."" ;
  5. $data9 = mysql_query($result9) or die(mysql_error());
  6. $row = mysql_fetch_assoc($data9);
  7. $skills = implode(",",$row);
  8. echo $skills;
  9. }
  10.  
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#13: Mar 17 '08

re: Problem storing array in database


Tagname is the only result in the Selected fetched row, you selected that one nyourself. So of course the $row array contains 1 value only. [php]result9 = "SELECT TagName FROM tagmaster WHERE TagId= ".$info8['TagId']."" ;[/php]Ronald
Member
 
Join Date: Feb 2008
Posts: 63
#14: Mar 17 '08

re: Problem storing array in database


So possibly what could be the solution for it. I tried many ways but not getting the desired answer.
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#15: Mar 17 '08

re: Problem storing array in database


Quote:

Originally Posted by agarwalsrushti

So possibly what could be the solution for it. I tried many ways but not getting the desired answer.

Now you have lost me! What solution do you want?

You select only one field from a database and show it onscreen. That is what you programmed. There are no more fields in your result row because of that select. One field select -> one field displayed.

So what is the solution you want?

Ronald
Member
 
Join Date: Feb 2008
Posts: 63
#16: Mar 17 '08

re: Problem storing array in database


I wanted to know how can i display all the results.
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#17: Mar 17 '08

re: Problem storing array in database


This discussion leads nowhere. I would appreciate it when you specified exactly what you wanted to show, and not just "I want to display it all", What is all? From which table?

As I said, you select only one field from the db so you can only display one field. When you want to display all fields from the 'tagmaster' table, you must select everyting, i.e. replace field 'Tagmaster' by an asterisk (*)[php]$result9 = "SELECT * FROM tagmaster WHERE TagId= ".$info8['TagId']."" ;
$data9 = mysql_query($result9) or die(mysql_error());
$row = mysql_fetch_assoc($data9);
$skills = implode(",",$row);
echo $skills;
}[/php]Ronald
Member
 
Join Date: Feb 2008
Posts: 63
#18: Mar 17 '08

re: Problem storing array in database


That to dint helped. Is it possible that the comma separated values that i take from the user can be stored as it is in the database in a separate field.
For eg if user enters Java,PHP as key skills. Then can i make it appear in database as Java,PHP
Reply