Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing a PHP form to communicate checkbox information to a MySQL database
    primarykey
    data
    text
    <p>I have a MySQL database with 3 tables:</p> <pre><code>**articles:** id - articletitle - articleorganization - articleurl - articledate **tags:** id - articletag articles_tags **id:** article_id - tag_id </code></pre> <p>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.</p> <p>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).</p> <p>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 <strong>articles</strong> table and the Tag information into the <strong>tags</strong> table while also creating a relationship in the <strong>articles_tags</strong> table.</p> <p>So far I've been unsuccessful in this attempt. Here is what I've been working with so far:</p> <pre><code> &lt;?php function renderForm($articletitle, $articleorganization, $articledate, $articleurl, $articletags ) { ?&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; . . . &lt;/head&gt; &lt;body&gt; &lt;div class="container"&gt; &lt;div class="header"&gt; . . . &lt;/div&gt; &lt;div class="sidebar1"&gt; . . . &lt;/div&gt; &lt;div class="content"&gt; &lt;div id="stylized" class="myform"&gt; &lt;form id="form" name="form" action="" method="post"&gt; &lt;h1&gt;Create a new entry in the database&lt;/h1&gt; &lt;table width="100%" border="0" cellpadding="6"&gt; &lt;tr&gt; &lt;td colspan="2"&gt;&lt;legend&gt;Article details&lt;/legend&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td width="20%" align="right"&gt;&lt;span class="field"&gt;Article Title:&lt;/span&gt;&lt;/td&gt; &lt;td width="80%" align="left"&gt;&lt;span class="field"&gt; &lt;input name="articletitle" type="text" value="&lt;?php echo $articletitle; ?&gt;" size="50"/&gt; &lt;/span&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td align="right"&gt;&lt;span class="field"&gt;Article Author:&lt;/span&gt;&lt;/td&gt; &lt;td align="left"&gt;&lt;span class="field"&gt; &lt;input name="articleorganization" type="text" value="&lt;?php echo $articleorganization; ?&gt;" size="50"/&gt; &lt;/span&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td align="right"&gt;&lt;span class="field"&gt;Access Date:&lt;/span&gt;&lt;/td&gt; &lt;td align="left"&gt;&lt;span class="field"&gt; &lt;input name="articledate" type="text" value="MM/DD/YYYY" size="50"/&gt; &lt;/span&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td align="right"&gt;&lt;span class="field"&gt;Article URL:&lt;/span&gt;&lt;/td&gt; &lt;td align="left"&gt;&lt;span class="field"&gt; &lt;input name="articleurl" type="text" value="&lt;?php echo $articleurl; ?&gt;" size="50"/&gt; &lt;/span&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td align="right"&gt;&lt;span class="field"&gt;Article Tags:&lt;/span&gt;&lt;/td&gt; &lt;td align="left"&gt;&lt;span class="field"&gt; &lt;input type="checkbox" name="articletags[]" value="geology" id="articletags_0" /&gt; &lt;input type="checkbox" name="articletags[]" value="astronomy" id="articletags_1" /&gt; &lt;/span&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;footer&gt;&lt;input type="submit" name="submit" value="Add this Article"&gt;&lt;/footer&gt; &lt;/form&gt; &lt;/div&gt; &lt;div class="footer"&gt; . . . &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; &lt;?php } include('settings.php'); if(count($articletags) &gt; 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('','','','',''); } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload