Problem storing array in database | Member | | Join Date: Feb 2008
Posts: 63
| |
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. -
$arrSkills = explode(",",$KeySkills);
-
while($arrSkills != 0){
-
$query1 = mysql_query("SELECT TagId FROM tagmaster WHERE TagName = '$arrSkills'") or die(mysql_error());
-
$numrow = mysql_num_rows($query1);
-
if($numrow == 0){
-
mysql_query( "INSERT INTO $db_table3(TagName)values('$arrSkills')") or die(mysql_error());
-
$query2 = mysql_query("SELECT TagId FROM tagmaster WHERE TagName = '$arrSkills'") or die(mysql_error());
-
$info = mysql_fetch_array($query2);
-
mysql_query( "INSERT INTO $db_table4(TagId,UserName)values('$info[TagId]','$_POST[UserName]')") or die(mysql_error());
-
} //end of if
-
else{
-
$info = mysql_fetch_array($query1);
-
mysql_query( "INSERT INTO $db_table4(TagId,UserName)values('$info[TagId]','$_POST[UserName]')") or die(mysql_error());
-
} //end of else
-
} //end of while
-
|  | Moderator | | Join Date: Jul 2006 Location: The Netherlands
Posts: 4,139
| | | 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
| | | 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.
|  | Moderator | | Join Date: Jul 2006 Location: The Netherlands
Posts: 4,139
| | | 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
| | | 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.
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,936
| | | 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
| | | 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. -
$arrSkills = explode(",",$_POST['KeySkills']);
-
foreach($arrSkills as $skill){
-
$query1 = mysql_query("SELECT TagId FROM tagmaster WHERE TagName = '$skill'") or die(mysql_error());
-
$numrow = mysql_num_rows($query1);
-
if($numrow == 0){
-
mysql_query( "INSERT INTO $db_table3(TagName)values('$skill')") or die(mysql_error());
-
$query2 = mysql_query("SELECT TagId FROM tagmaster WHERE TagName = '$skill'") or die(mysql_error());
-
$info = mysql_fetch_array($query2);
-
mysql_query( "INSERT INTO $db_table4(TagId,UserName)values('$info[TagId]','$_POST[UserName]')") or die(mysql_error());
-
} //end of if
-
else{
-
$info = mysql_fetch_array($query1);
-
mysql_query( "INSERT INTO $db_table4(TagId,UserName)values('$info[TagId]','$_POST[UserName]')") or die(mysql_error());
-
} //end of else
-
} //end of while
-
| | Member | | Join Date: Feb 2008
Posts: 63
| | | re: Problem storing array in database
Everything is working fine now.
Thanks a lot for your interest.
| | Member | | Join Date: Feb 2008
Posts: 63
| | | 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. -
$result8= " SELECT TagId FROM usertags WHERE UserName= '$uname'";
-
$data8 = mysql_query($result8) or die(mysql_error());
-
while($info8 = mysql_fetch_array( $data8)){
-
$result9 = "SELECT TagName FROM tagmaster WHERE TagId= ".$info8['TagId'].""
-
$data9 = mysql_query($result9) or die(mysql_error());
-
$info9 = implode(",",$data9);
-
}
-
Plz what is the error in this code.
|  | Moderator | | Join Date: Jul 2006 Location: The Netherlands
Posts: 4,139
| | | 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
| | | 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 -
$result8= " SELECT TagId FROM usertags WHERE UserName= '$uname'";
-
$data8 = mysql_query($result8) or die(mysql_error());
-
while($info8 = mysql_fetch_array( $data8)){
-
$result9 = "SELECT TagName FROM tagmaster WHERE TagId= ".$info8['TagId']."" ;
-
$data9 = mysql_query($result9) or die(mysql_error());
-
$info9 = mysql_fetch_array( $data9);
-
//$data = $info9['TagName'];
-
echo $info9['TagName'];
-
$skills = implode(",",$info9);
-
//echo $skills;
-
}
-
Why is it taking the values twice?
| | Member | | Join Date: Feb 2008
Posts: 63
| | | 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. -
$result8= " SELECT TagId FROM usertags WHERE UserName= '$uname'";
-
$data8 = mysql_query($result8) or die(mysql_error());
-
while($info8 = mysql_fetch_array( $data8)){
-
$result9 = "SELECT TagName FROM tagmaster WHERE TagId= ".$info8['TagId']."" ;
-
$data9 = mysql_query($result9) or die(mysql_error());
-
$row = mysql_fetch_assoc($data9);
-
$skills = implode(",",$row);
-
echo $skills;
-
}
-
|  | Moderator | | Join Date: Jul 2006 Location: The Netherlands
Posts: 4,139
| | | 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
| | | 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.
|  | Moderator | | Join Date: Jul 2006 Location: The Netherlands
Posts: 4,139
| | | 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
| | | re: Problem storing array in database
I wanted to know how can i display all the results.
|  | Moderator | | Join Date: Jul 2006 Location: The Netherlands
Posts: 4,139
| | | 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
| | | 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
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,392 network members.
|