How to concatenate a string to an existing field(column) in mysql

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sdsuresh
    New Member
    • Aug 2013
    • 1

    How to concatenate a string to an existing field(column) in mysql

    This is my code

    Code:
    <?php
    include 'template/header.php';
    
    if(logged_in() === true)
    { 
    if(empty($_POST['keyword_submit']) === false)
    {
    $required_fields = array('keywords');
    foreach($_POST as $key => $value)
    
     {
          if(empty($value) && in_array($key, $required_fields) === true)
            {               
                $errors[] = '<font color="red">Fields marked with an asterisk are required</font>';
                break 1;
            }
     }} ?>
    
     <?php
    
    if(isset($_GET['success']) && empty($_GET['success']))
    {
        echo "<font color='green'><b>You are successfully registered! You can <a  style='color: orange;' href='index.php'>Login</a> Now</b></font>"; 
    }
       else
       {
    if(empty($_POST['keyword_submit']) === false && empty($errors) === true)
        {
    
    
          $data_keyword = array(
            'user_id'           => $session_user_id,
            'username'          => $user_data['username'],
            'keywords'          => $_POST['keywords']
          ); 
            data_keyword($data_keyword);
            header('Location: keyword.php');
            exit();
    
       }
    
      else if(empty($errors) === false)
      {
          echo output_errors($errors);
      }
        }
     ?>
    
    <form method="post" id="mainformkey">
      <table id="main-content" width="100%" style="padding: 5px;">
    <tr>
        <td>
            <table width="100%">
                <tr>
                   <td width="17%" style="border: 1px solid #DEDEDE; padding: 3px;">
                        <?php include 'left_menu.php';?>
                    </td>
    
                    <td style="border: 1px solid #DEDEDE; padding: 3px;" valign="top">
                        <table width="100%">
                            <tr>
                                <td style="background: #EDEDED; padding: 5px;">
    
                                    <span style="font-weight: bold; font-size: 15px;">Keywords</span>
                                </td>
                            </tr>
    
                            <tr>
                                <td style="padding: 5px;">
    
                                    <table id="keyword_table" width="100%" border="1" >
                                        <tr>
                                            <th width="60%">
                                                 Keywords
                                             </th>
    
                                            <th  width="10%">
                                                  Keyword ID
                                             </th>
    
                                            <th  width="9%">
                                                 Words<?php
    
                                                  ?>
                                            </th>
                                        </tr>
    
                                        <?php
                                        $keyword_sql1 = mysql_query("SELECT * FROM keyword");
                                        while($row_keyword_sql1 = mysql_fetch_array($keyword_sql1)){
                                        $id1 = $row_keyword_sql1[id];
                                        }
    
                                        if(isset($_POST[keyword_submit_add])){
                                            $values_keyword = $_POST['keywords_extra'];
                                            $ds = mysql_query("UPDATE keyword SET keywords = CONCAT(keywords,$values_keyword) WHERE id = '$id1'");  
    
                                    }
    
                                          $keyword_sql = mysql_query("SELECT * FROM keyword WHERE user_id='$session_user_id'");
                                        while($row_keyword_sql = mysql_fetch_array($keyword_sql)){
                                        $id = $row_keyword_sql[id];
                                        $get_keyword = $row_keyword_sql[keywords];
    
    
                                        ?>
                                        <tr>
                                            <td>
                                                <?php echo $get_keyword;
                                                ?>
                                            </td>
    
                                            <td align="center">
                                                <?php echo '@key-0'.$id;?>
                                            </td>
    
                                            <td align="center">
                                                <form id="addnew" method="post">
                                                    <input type="text" name="keywords_extra">
                                                    <input type="submit" name="keyword_submit_add" value="Add keyword">
                                                </form>
                                            </td>
                                        </tr>
                                        <?php
                                        }
                                        ?>
                                    </table>
    
                                </td>
                            </tr>
    
    
                            <tr>
                                <td style="background: #EDEDED; padding: 5px;">
                                    <input type="text"  name="keywords">
                                    <input  type="submit" name="keyword_submit" value="Main Keyword">
                                </td>
                            </tr>
                        </table>
                 </td>
                </tr>
            </table>
        </td>
    </tr>
     </table>
     </form>
     <?php
     } else {
    protect_page();
     }
    
    
     function data_keyword($data_keyword)
       {
      array_walk($data_keyword, 'array_sanitize');
    
    
      $fields = '`' . implode('`, `', array_keys($data_keyword)) . '`';
      $data = '\'' . implode('\', \'', $data_keyword) . '\'';
    
      mysql_query("INSERT INTO `keyword` ($fields) VALUES ($data)");
       }
     ?>
    Here there are two submit buttons 1. First submit button: User can enter one keyword and submit (Name: Main keyword). 2. Second submit button: Second they can enter the keywords to a specific row which may append by a comma separated.

    I already tried that using CONCAT in mysql but only comma is getting appending and that too in the all the rows, but i need in specific row according to the id.
    Attached Files
    Last edited by Rabbit; Aug 24 '13, 04:10 PM. Reason: Please use code tags when posting code.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    simple answer, you don’t do that. stuffing 2 (or more) separate data into a row leads to more problems finding that data again. you would simply add another row with the same key and on retrieval you concat all values belonging to a key together (using GROUP_CONCAT()).

    Comment

    Working...