0

I have a MySQL database with 3 tables:

**articles:**

id - articletitle - articleorganization - articleurl - articledate

**tags:**

id - articletag

articles_tags

**id:**
article_id - tag_id

On my form I have text input boxes to fill in the information for Title, Organization, URL, and Date and I have checkboxes to capture Tag information.

This works very well but the problem comes when I need to edit an entry in the database. With that I have a similar form to the entry form but I can't get the checkboxes to work. After some research and much help from Stackoverflow contributors, I've learned that what I need is a different type of database structure (which is what I now have, I originally only had 1 table, now I have the 3 listed above).

This leads me to redeveloping the entry form so now I am attempting to insert the Title, Organization, URL, and Date information into into the articles table and the Tag information into the tags table while also creating a relationship in the articles_tags table.

So far I've been unsuccessful in this attempt. Here is what I've been working with so far:

    <?php
     function renderForm($articletitle, $articleorganization, $articledate, $articleurl, $articletags )
     {
     ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    . . .
    </head>

    <body>

    <div class="container">
      <div class="header">
      . . .
      </div>
        <div class="sidebar1">
        . . .
        </div>
      <div class="content">
            <div id="stylized" class="myform">
          <form id="form" name="form" action="" method="post">
            <h1>Create a new entry in the database</h1>
            <table width="100%" border="0" cellpadding="6">
              <tr>
                <td colspan="2"><legend>Article details</legend></td>
              </tr>
              <tr>
                <td width="20%" align="right"><span class="field">Article Title:</span></td>
                <td width="80%" align="left"><span class="field">
                  <input name="articletitle" type="text" value="<?php echo $articletitle; ?>" size="50"/>
                </span></td>
              </tr>
              <tr>
                <td align="right"><span class="field">Article Author:</span></td>
                <td align="left"><span class="field">
                  <input name="articleorganization" type="text" value="<?php echo $articleorganization; ?>" size="50"/>
                </span></td>
              </tr>
              <tr>
                <td align="right"><span class="field">Access Date:</span></td>
                <td align="left"><span class="field">
                  <input name="articledate" type="text" value="MM/DD/YYYY" size="50"/>
                </span></td>
              </tr>
              <tr>
                <td align="right"><span class="field">Article URL:</span></td>
                <td align="left"><span class="field">
                <input name="articleurl" type="text" value="<?php echo $articleurl; ?>" size="50"/>
                </span></td>
              </tr>
              <tr>
                <td align="right"><span class="field">Article Tags:</span></td>
                <td align="left"><span class="field">
                        <input type="checkbox" name="articletags[]" value="geology" id="articletags_0" />
                        <input type="checkbox" name="articletags[]" value="astronomy" id="articletags_1" />
                </span></td>
              </tr>
            </table>
            <footer><input type="submit" name="submit" value="Add this Article"></footer>
            </form>
        </div>
      <div class="footer">
        . . .
        </div>
    </body>
    </html>
    <?php 
     }

     include('settings.php');

     if(count($articletags) > 0)
    {
     $articletags_string = implode(",", $articletags);
    }

     if($_SERVER['REQUEST_METHOD'] == 'POST')
    { 

     $articletitle = mysql_real_escape_string(htmlspecialchars($_POST['articletitle']));
     $articleorganization = mysql_real_escape_string(htmlspecialchars($_POST['articleorganization']));
     $articledate = mysql_real_escape_string(htmlspecialchars($_POST['articledate']));
     $articleurl = mysql_real_escape_string(htmlspecialchars($_POST['articleurl']));
     $articletags = implode(',', $_POST['articletags']);

     mysql_query("INSERT articles SET articletitle='$articletitle',
          articleorganization='$articleorganization',
          articledate='$articledate',
          articleurl='$articleurl',
          articletags='$articletags' ")

    mysql_query2("INSERT tags SET articletags='$articletags' ")


     header("Location:addsuccess.php"); 
     }
     }
     else

     {
     renderForm('','','','','');
     }
    ?>

2 Answers 2

1

You are missing a keyword INTO in your SQL queries - INSERT INTO mytable... You can also merge the last 2 tables into one - "tag_id, article_id, article_tags". And one advice - please read what PDO is and get rid of mysql_*.

Sign up to request clarification or add additional context in comments.

3 Comments

so far my forms have been working without the INTO keyword. Is this required when using multiple tables or is it about standards? (I'm just learning :-) )
More likely standards. As for your other question about editing - I see you are using comma separator when inserting. So you get all the tags, and they are more than one, you can use function like explode(). Then loop through all of them, matching the value of the ones in your form(checkbox) and if there is a match, add checked="checked" to that checkbox.
could you post some sample code by what you mean explode() and "loop" through them?
1

below is to retrive the tag values from db according to the article id.

<?php

    $checked=array();
    $sql = $dbh->prepare('
            SELECT t.articletag from tags t INNER JOIN article_tag a 
            ON t.id=a.tag_id 
            WHERE article_id=:article_id
            ');
    $sql->bindParam(':article_id', $articleid);        
    $sql->execute();
    $result = $sql->fetchAll(); <----- fetch All the data

    $vals=explode(',',$result['articletag']);


    foreach ($vals as $val)
    {
        if ($val !='')
            $checked[$val]='checked';
    }

    function set_checked($value)
    {            
        if (isset($checked[$value]))
            return 'checked';
        else
            return '';
    }

    ?>

HTML code to display whether the checkbox is checked or unchecked according to the value retrieved.

<input type="checkbox" name="articletags[]" value="geology" <? echo set_checked('geology');?>>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.