Problem storing array in database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • agarwalsrushti
    New Member
    • Feb 2008
    • 63

    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.


    [code=php]
    $arrSkills = explode(",",$Ke ySkills);
    while($arrSkill s != 0){
    $query1 = mysql_query("SE LECT TagId FROM tagmaster WHERE TagName = '$arrSkills'") or die(mysql_error ());
    $numrow = mysql_num_rows( $query1);
    if($numrow == 0){
    mysql_query( "INSERT INTO $db_table3(TagN ame)values('$ar rSkills')") or die(mysql_error ());
    $query2 = mysql_query("SE LECT TagId FROM tagmaster WHERE TagName = '$arrSkills'") or die(mysql_error ());
    $info = mysql_fetch_arr ay($query2);
    mysql_query( "INSERT INTO $db_table4(TagI d,UserName)valu es('$info[TagId]','$_POST[UserName]')") or die(mysql_error ());
    } //end of if
    else{
    $info = mysql_fetch_arr ay($query1);
    mysql_query( "INSERT INTO $db_table4(TagI d,UserName)valu es('$info[TagId]','$_POST[UserName]')") or die(mysql_error ());
    } //end of else
    } //end of while
    [/code]
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    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

    Comment

    • agarwalsrushti
      New Member
      • Feb 2008
      • 63

      #3
      Sorry and thanks a lot. Each and every advice, hint and solution helps me alot in implementin my project.
      Thanks a lot.

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        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.
        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

        Comment

        • agarwalsrushti
          New Member
          • Feb 2008
          • 63

          #5
          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.

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            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.

            Comment

            • agarwalsrushti
              New Member
              • Feb 2008
              • 63

              #7
              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.

              [code=php]
              $arrSkills = explode(",",$_P OST['KeySkills']);
              foreach($arrSki lls as $skill){
              $query1 = mysql_query("SE LECT TagId FROM tagmaster WHERE TagName = '$skill'") or die(mysql_error ());
              $numrow = mysql_num_rows( $query1);
              if($numrow == 0){
              mysql_query( "INSERT INTO $db_table3(TagN ame)values('$sk ill')") or die(mysql_error ());
              $query2 = mysql_query("SE LECT TagId FROM tagmaster WHERE TagName = '$skill'") or die(mysql_error ());
              $info = mysql_fetch_arr ay($query2);
              mysql_query( "INSERT INTO $db_table4(TagI d,UserName)valu es('$info[TagId]','$_POST[UserName]')") or die(mysql_error ());
              } //end of if
              else{
              $info = mysql_fetch_arr ay($query1);
              mysql_query( "INSERT INTO $db_table4(TagI d,UserName)valu es('$info[TagId]','$_POST[UserName]')") or die(mysql_error ());
              } //end of else
              } //end of while
              [/code]

              Comment

              • agarwalsrushti
                New Member
                • Feb 2008
                • 63

                #8
                Everything is working fine now.
                Thanks a lot for your interest.

                Comment

                • agarwalsrushti
                  New Member
                  • Feb 2008
                  • 63

                  #9
                  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.

                  [code=php]
                  $result8= " SELECT TagId FROM usertags WHERE UserName= '$uname'";
                  $data8 = mysql_query($re sult8) or die(mysql_error ());
                  while($info8 = mysql_fetch_arr ay( $data8)){
                  $result9 = "SELECT TagName FROM tagmaster WHERE TagId= ".$info8['TagId'].""
                  $data9 = mysql_query($re sult9) or die(mysql_error ());
                  $info9 = implode(",",$da ta9);
                  }
                  [/code]

                  Plz what is the error in this code.

                  Comment

                  • ronverdonk
                    Recognized Expert Specialist
                    • Jul 2006
                    • 4259

                    #10
                    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($re sult9) or die(mysql_error ());
                    $info9 = implode(",",$da ta9);[/php]you MUST fetch the row before you can implode a value string into $info9

                    Ronald

                    Comment

                    • agarwalsrushti
                      New Member
                      • Feb 2008
                      • 63

                      #11
                      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++,Jav a,Java

                      [code=php]
                      $result8= " SELECT TagId FROM usertags WHERE UserName= '$uname'";
                      $data8 = mysql_query($re sult8) or die(mysql_error ());
                      while($info8 = mysql_fetch_arr ay( $data8)){
                      $result9 = "SELECT TagName FROM tagmaster WHERE TagId= ".$info8['TagId']."" ;
                      $data9 = mysql_query($re sult9) or die(mysql_error ());
                      $info9 = mysql_fetch_arr ay( $data9);
                      //$data = $info9['TagName'];
                      echo $info9['TagName'];
                      $skills = implode(",",$in fo9);
                      //echo $skills;
                      }
                      [/code]

                      Why is it taking the values twice?

                      Comment

                      • agarwalsrushti
                        New Member
                        • Feb 2008
                        • 63

                        #12
                        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.

                        [code=php]
                        $result8= " SELECT TagId FROM usertags WHERE UserName= '$uname'";
                        $data8 = mysql_query($re sult8) or die(mysql_error ());
                        while($info8 = mysql_fetch_arr ay( $data8)){
                        $result9 = "SELECT TagName FROM tagmaster WHERE TagId= ".$info8['TagId']."" ;
                        $data9 = mysql_query($re sult9) or die(mysql_error ());
                        $row = mysql_fetch_ass oc($data9);
                        $skills = implode(",",$ro w);
                        echo $skills;
                        }
                        [/code]

                        Comment

                        • ronverdonk
                          Recognized Expert Specialist
                          • Jul 2006
                          • 4259

                          #13
                          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

                          Comment

                          • agarwalsrushti
                            New Member
                            • Feb 2008
                            • 63

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

                            Comment

                            • ronverdonk
                              Recognized Expert Specialist
                              • Jul 2006
                              • 4259

                              #15
                              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

                              Comment

                              Working...