473,395 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Problem storing array in database

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.  
Mar 16 '08 #1
17 1821
ronverdonk
4,258 Expert 4TB
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
Mar 16 '08 #2
Sorry and thanks a lot. Each and every advice, hint and solution helps me alot in implementin my project.
Thanks a lot.
Mar 16 '08 #3
ronverdonk
4,258 Expert 4TB
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.
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
Mar 16 '08 #4
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.
Mar 16 '08 #5
Markus
6,050 Expert 4TB
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.
Mar 16 '08 #6
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.  
Mar 16 '08 #7
Everything is working fine now.
Thanks a lot for your interest.
Mar 16 '08 #8
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.
Mar 16 '08 #9
ronverdonk
4,258 Expert 4TB
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
Mar 16 '08 #10
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?
Mar 17 '08 #11
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.  
Mar 17 '08 #12
ronverdonk
4,258 Expert 4TB
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
Mar 17 '08 #13
So possibly what could be the solution for it. I tried many ways but not getting the desired answer.
Mar 17 '08 #14
ronverdonk
4,258 Expert 4TB
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
Mar 17 '08 #15
I wanted to know how can i display all the results.
Mar 17 '08 #16
ronverdonk
4,258 Expert 4TB
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
Mar 17 '08 #17
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
Mar 17 '08 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: cleveralex1212 | last post by:
Sorry to disturb. I'm truly a beginner in C++ and my knowledge of it is really not much. I've been facing a serious problem during these few days. Let me describe my situation as clearly as I can...
7
by: jsale | last post by:
I'm currently using ASP.NET with VS2003 and SQL Server 2003. The ASP.NET app i have made is running on IIS v6 and consists of a number of pages that allow the user to read information from the...
5
by: sdettmers | last post by:
Hi, Database: SQL Server Session: SQL Server Language: C# Application: ASP.Net I have created a login page which attempts to retrieve the users record from the database and I...
7
by: paladin.rithe | last post by:
Is it possible to store classes in an array? I am fairly new to PHP, and haven't found anything either way yet. I have a program that where you can have multiple notes attached to a ticket, which...
22
by: guitarromantic | last post by:
Hey everyone, I run a site with staff-submitted reviews, and most of them are written by one author. However, we also do "multiple" reviews. Up until now I just had a userid for a 'Multiple'...
22
by: roadrunner | last post by:
Hi, Our website has recently been experiencing some problems under load. We have pinpointed a particular function which slows dramatically when we have these problems. Normally it should execute...
6
by: Kyle Teague | last post by:
What would give better performance, serializing a multidimensional array and storing it in a single entry in a table or storing each element of the array in a separate table and associating the...
11
by: aljaber | last post by:
hi, i am facing a problem with my program output here is the program /*********************************************\ * CD Database * * ...
17
by: Stubert | last post by:
I have a training module db that stores information about employees and what training they have carried our or need to carry out. One table in this database stores what training needs to be carried...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.